Reputation: 397
I have tried to solve this problem with a lot of methods but none of them worked.
I would like to convert a pandas
series to datetime
.
I think I may have miss some small but important part.
print test["order_date"][3]
print type(test["order_date"][3])
test["order_date"] = pd.to_datetime(test["order_date"], format="%Y-%m-%d %H:%M:%S")
##have also tried
##test["order_date"] = pd.to_datetime(test["order_date"], infer_datetime_format=True)
##test["order_date"] = test["order_date"].apply(pd.to_datetime)
##all turn out to be the same result
print test["order_date"][3]
print type(test["order_date"][3])
The outcome turns out to be like below:
20150731000001
<type 'numpy.int64'>
1970-01-01 05:35:50.731000001
<class 'pandas.tslib.Timestamp'>
I cannot figure out why the result
becomes 1970-01-01
If any further information is needed please kindly let me know.
Thanks!
Upvotes: 1
Views: 3569
Reputation: 394051
It's because the dtype is int64
so it assumes nanosecond unit value:
In[51]:
pd.to_datetime(20150731000001, unit='ns')
Out[51]: Timestamp('1970-01-01 05:35:50.731000001')
If it was a string then it would be able to parse correctly:
In[54]:
pd.to_datetime('20150731000001')
Out[53]: Timestamp('2015-07-31 00:00:01')
So you can either explicitly pass a format string (like in @MaxU's answer) or convert the dtype of the column to str
and then pass this:
test["order_date"] = pd.to_datetime(test["order_date"].astype(str))
Upvotes: 2
Reputation: 210842
You have specified a wrong format.
Try this:
In [34]: pd.to_datetime(['20150731000001'], format="%Y%m%d%H%M%S")
Out[34]: DatetimeIndex(['2015-07-31 00:00:01'], dtype='datetime64[ns]', freq=None)
Upvotes: 4