Sharek
Sharek

Reputation: 81

python pandas reading a timestamp

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

Upvotes: 3

Views: 776

Answers (2)

jezrael
jezrael

Reputation: 863611

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

Upvotes: 2

EdChum
EdChum

Reputation: 394419

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

Upvotes: 4

Related Questions