简体   繁体   中英

Convert Pandas time series: UNIX epoch to datetime

I'm tying to convert the following series of UNIX epochs to regular datetime objects:

>> val = pd.Series(["1440643875", "1440644191", "1440645638", "1440998720"])
>> val
0    1440643875
1    1440644191
2    1440645638
3    1440998720
Name: obj, dtype: object

There appears to be two ways of doing this. The first is:

>> pd.to_datetime(val, unit='s')
ValueError: year is out of range

And the second:

val.astype("datetime64[s]")
TypeError: Cannot parse "1445124547" as unit 's' using casting rule 'same_kind'

What seems to be the problem here?

I also tried checking these timestamps with the "Online Epoch Calculator" tools, and they give out reasonable answers..

The issue was that the elements were strings, and not ints. Apparently, pd.to_datetime() isn't smart enough to convert from strings to datetime.

My solution was this:

>> val.astype('int').astype("datetime64[s]")
0   2015-08-27 02:51:15
1   2015-08-27 02:56:31
2   2015-08-27 03:20:38
3   2015-08-31 05:25:20
dtype: datetime64[ns]

EDITED

datetime.datetime.utcfromtimestamp could get only integer as paramer:

In [510]: datetime.datetime.utcfromtimestamp('1440643875')
TypeError: an integer is required (got type str)

So first you need to convert your Series to int then you could use these methods:

import pandas as pd
import datetime 

s = pd.Series(["1440643875", "1440644191", "1440645638", "1440998720"], dtype=object)

s = pd.to_numeric(s)

In [50]: s
Out[50]:
0    1440643875
1    1440644191
2    1440645638
3    1440998720
dtype: int64

In [51]: pd.to_datetime(s, unit='s')
Out[51]:
0   2015-08-27 02:51:15
1   2015-08-27 02:56:31
2   2015-08-27 03:20:38
3   2015-08-31 05:25:20
dtype: datetime64[ns]

Also datetime.datetime.utcfromtimestamp as @Adam Smith pointed out in comment:

In [52]: s.apply(datetime.datetime.utcfromtimestamp)
Out[52]:
0   2015-08-27 02:51:15
1   2015-08-27 02:56:31
2   2015-08-27 03:20:38
3   2015-08-31 05:25:20
dtype: datetime64[ns]

We can directly convert the epoch time to datetime. By default it will be in %Y-%m-%d %I:%M:%S format by using pd.to_datetime. By using dt.strftime complete column can be formatted in the required format.

from datetime import datetime as dt
import pandas as pd
input_data_df['timestamp']=pd.to_datetime(input_data_df['epoch'],unit='ms')
input_data_df['timestamp'] = input_data_df['timestamp'].dt.strftime('%d-%m-%Y %I:%M:%S')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM