Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date from int64 to datetime

Tags:

python

pandas

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.

like image 599
ATMA Avatar asked Jul 20 '17 14:07

ATMA


2 Answers

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

like image 114
EdChum Avatar answered Nov 11 '22 18:11

EdChum


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
like image 24
jezrael Avatar answered Nov 11 '22 19:11

jezrael