3

I am trying to read a date-time in microsecond format.

1500909283.955000

The expected output should be something like

July 24, 2017 3:14:43.955 PM 

But when I use pandas to_datetime function I got

1970-01-01 00:00:01.500909283

I tried all possible format with no success.

Any hints

2 Answers 2

4

You need unit='s' param for to_datetime:

In[4]:
pd.to_datetime(1500909283.955000, unit='s')

Out[4]: Timestamp('2017-07-24 15:14:43.955000')

the timestamp is seconds since the epoch

The default unit value is nanoseconds:

In[5]:
pd.to_datetime(1500909283.955000, unit='ns')

Out[5]: Timestamp('1970-01-01 00:00:01.500909283')

which is what you observed

Sign up to request clarification or add additional context in comments.

Comments

2

Need to_datetime with parameter unit, data are in seconds, not in miliseconds:

df = pd.DataFrame({'col':['1500909283.955000','1500909283.955000']})
df['col'] = pd.to_datetime(df['col'], unit='s')
print (df)
                      col
0 2017-07-24 15:14:43.955
1 2017-07-24 15:14:43.955

For milliseconds:

df['col'] = pd.to_datetime(df['col'], unit='ms')
print (df)
                         col
0 1970-01-18 08:55:09.283955
1 1970-01-18 08:55:09.283955

If need another format use Series.dt.strftime:

df['col1'] = df['col'].dt.strftime('%b %d, %Y %I:%M:%S.%f %p')
print (df)
                      col                             col1
0 2017-07-24 15:14:43.955  Jul 24, 2017 03:14:43.955000 PM
1 2017-07-24 15:14:43.955  Jul 24, 2017 03:14:43.955000 PM

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.