Converting date from int64 to datetime

I have an int64 object in a pandas dataframe that needs to represent a date.

>>> df.dtypes
CreatedDate              int64

      

Obviously I want to convert this to a date, 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 leads to dates that date back to the 1970s, which shouldn't be true. Can anyone tell me how to convert int64 to datetime in pandas framework. I thought this was the right way.

+3


source to share


2 answers


Use the unit

in parameter to_datetime

to 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

      

+3


source


You need to get past unit='ms'

, since these are milliseconds since the 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 parameter unit

is 'ns'

as it takes on values datetime64[ns]

that are nanoseconds since unix time if passed int64

dtype values

+3


source







All Articles