Convert unix time to readable date in pandas dataframe
These appear to be seconds since epoch.
In [20]: df = DataFrame(data['values'])In [21]: df.columns = ["date","price"]In [22]: dfOut[22]: <class 'pandas.core.frame.DataFrame'>Int64Index: 358 entries, 0 to 357Data columns (total 2 columns):date 358 non-null valuesprice 358 non-null valuesdtypes: float64(1), int64(1)In [23]: df.head()Out[23]: date price0 1349720105 12.081 1349806505 12.352 1349892905 12.153 1349979305 12.194 1350065705 12.15In [25]: df['date'] = pd.to_datetime(df['date'],unit='s')In [26]: df.head()Out[26]: date price0 2012-10-08 18:15:05 12.081 2012-10-09 18:15:05 12.352 2012-10-10 18:15:05 12.153 2012-10-11 18:15:05 12.194 2012-10-12 18:15:05 12.15In [27]: df.dtypesOut[27]: date datetime64[ns]price float64dtype: object
If you try using:
df[DATE_FIELD]=(pd.to_datetime(df[DATE_FIELD],***unit='s'***))
and receive an error :
"pandas.tslib.OutOfBoundsDatetime: cannot convert input with unit 's'"
This means the DATE_FIELD
is not specified in seconds.
In my case, it was milli seconds - EPOCH time
.
The conversion worked using below:
df[DATE_FIELD]=(pd.to_datetime(df[DATE_FIELD],unit='ms'))
Assuming we imported pandas as pd
and df
is our dataframe
pd.to_datetime(df['date'], unit='s')
works for me.