Skip to main content
 首页 » 编程设计

python pandas datetime datetime 不能正常工作

2025年05月04日17bhlsheji

我尝试了很多方法来解决这个问题,但都没有用。

我想将 pandas 系列转换为 datetime。

我想我可能漏掉了一些小而重要的部分。

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]) 

结果如下所示:

20150731000001 
<type 'numpy.int64'> 
 
1970-01-01 05:35:50.731000001  
<class 'pandas.tslib.Timestamp'> 

我不明白为什么 result 变成了 1970-01-01

如果需要任何进一步的信息,请告诉我。

谢谢!

请您参考如下方法:

您指定了错误的格式。

试试这个:

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)