将pandas中的字符串转换为datetime时出现问题

2024-05-03 19:27:09 发布

您现在位置:Python中文网/ 问答频道 /正文

In  [67]:

offline['CREATEDDATE'].head()  
Out[67]:  
0    01/05/06  
1    11/22/03   
2    10/31/07  
3    02/05/10  
4    05/16/05  
Name: CREATEDDATE, dtype: object  
In [68]:

offline['CREATEDDATE'] = pd.to_datetime(offline['CREATEDDATE'])  
In [70]:

offline['CREATEDDATE'].head()  
Out[70]:  
0    01/05/06  
1    11/22/03  
2    10/31/07  
3    02/05/10  
4    05/16/05  
Name: CREATEDDATE, dtype: object

Tags: tonameindatetimeobjectoutheadpd
1条回答
网友
1楼 · 发布于 2024-05-03 19:27:09

我测试了你的代码,它运行得很好。我建议你检查一下Pandas的版本,如果需要的话升级一下。在

#Check versions. I used 'pandas: 0.16.1', 'numpy: 1.9.2'
pd.show_versions()
df=pd.DataFrame(['01/05/06','11/22/03'], columns=['dates'])
#the format option can be omitted, but I include this for clarity
df['dates']=pd.to_datetime(df['dates'],format='%m/%d/%y')
df['dates']
#out: Name: dates, dtype: datetime64[ns]

相关问题 更多 >