I have an int64 object in a pandas data frame and that is supposed to represent a date.
>>> df.dtypes
CreatedDate int64
Obviously, I want to convert this into a date time, so I did the following
df["CreatedDate2"] = pd.to_datetime(pd.Series(df["CreatedDate"]))
>>> df[["CreatedDate","CreatedDate2"]].head()
CreatedDate CreatedDate2
0 1466461661000 1970-01-01 00:24:26.461661
1 1464210703000 1970-01-01 00:24:24.210703
2 1423576093000 1970-01-01 00:23:43.576093
3 1423611903000 1970-01-01 00:23:43.611903
4 1423617600000 1970-01-01 00:23:43.617600
>>>
However, this is producing dates that are in the 1970s, which shouldn't be true. Can anyone tell me how to convert int64 to datetime in a pandas data frame. I thought this was the right way.
You need to pass unit='ms'
as they are milliseconds since Unix Epoch:
In[51]:
df['CreatedDate2'] = pd.to_datetime(df['CreatedDate'], unit='ms')
df
Out[51]:
CreatedDate CreatedDate2
0 1466461661000 2016-06-20 22:27:41
1 1464210703000 2016-05-25 21:11:43
2 1423576093000 2015-02-10 13:48:13
3 1423611903000 2015-02-10 23:45:03
4 1423617600000 2015-02-11 01:20:00
by default the unit
param is 'ns'
as it assumes datetime64[ns]
values which are nanoseconds since the unix epoch if the passed values are int64
dtype
Use parameter unit
in to_datetime
for convert unix epoch time:
df["CreatedDate2"] = pd.to_datetime(df["CreatedDate"], unit='ms')
print (df)
CreatedDate CreatedDate2
0 1466461661000 2016-06-20 22:27:41
1 1464210703000 2016-05-25 21:11:43
2 1423576093000 2015-02-10 13:48:13
3 1423611903000 2015-02-10 23:45:03
4 1423617600000 2015-02-11 01:20:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With