Python 3.x 如何在pandas中转换不同的日期格式?
我有两列不同的日期格式。在每一行中,字符串日期的格式都不同。Python 3.x 如何在pandas中转换不同的日期格式?,python-3.x,pandas,Python 3.x,Pandas,我有两列不同的日期格式。在每一行中,字符串日期的格式都不同。 我想将列转换为日期类型。但是,我想知道是否有任何内置方法可以为我进行解析: 我尝试的 from datetime import datetime newFrame = newDF.assign(Effective_Date=newDF['Effective_Date'].apply(lambda element: datetime.strptime(element,'%b %d %Y %H %M %S')), Paid_Off_
我想将列转换为日期类型。但是,我想知道是否有任何内置方法可以为我进行解析: 我尝试的
from datetime import datetime
newFrame = newDF.assign(Effective_Date=newDF['Effective_Date'].apply(lambda element: datetime.strptime(element,'%b %d %Y %H %M %S')), Paid_Off_Time=newDF['Paid_Off_Time'].apply(lambda element: datetime.strptime(element,'%b %d %Y %H %M %S')))
运行上述代码时出错
line 359, in _strptime
(data_string, format))
ValueError: time data '09/08/2016' does not match format '%b %d %Y %H %M %S'
以.csv格式显示的日期格式示例:
10/07/2016
2016年7月10日09:00数据
newDF=pd.DataFrame({'Effective_Date':['10/07/2016','10/07/2016 09:00','09 August 2016'],'Paid_Off_Time':['10 July 2016','10/08/2016','10/09/2016 01:00:30']})
Effective_Date Paid_Off_Time
0 10/07/2016 10 July 2016
1 10/07/2016 09:00 10/08/2016
2 09 August 2016 10/09/2016 01:00:30
解决方案
newDF.assign(Effective_Date=pd.to_datetime(newDF['Effective_Date']).dt.date,Paid_Off_Time=pd.to_datetime(newDF['Paid_Off_Time']).dt.date)
Effective_Date Paid_Off_Time
0 2016-10-07 2016-07-10
1 2016-10-07 2016-10-08
2 2016-08-09 2016-10-09
有关更多详细信息,请查阅有关数据功能的熊猫文档: 请尝试
pd.to\u datetime(newDF['Effective\u Date'])