^{}可以与参数格式一起使用。 s=PD.series(['01apr20176336059 ',' 01APR2017 6:59'] ) )。
是打印(s )
0 01APR2017 6:59
1 01APR2017 6:59
dtype :对象
print(PD.to_datetime(s,format='%d%b%Y %H:%M ' ) )
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00
dtype: datetime64[ns]
另一种可能的解决方案是在^{}中使用date_parser:import pandas as pd
from pandas.compatimportstringio
temp=u'''date
01APR2017 6:59
01APR2017 6:59 ' ' '
# aftertestingreplace ' stringio ' temp ' to ' filename.CSV '
parser=lambdax : PD.datetime.strp time (x,' %d%b%Y %H:%M ) )
df=PD.read_CSV(stringio(temp ),parse_dates=[0],date_parser=parser ) )。
是打印(df )
日期
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00
打印(df.date.dtype )。
datetime64[ns]
使用注释编辑:
如果无法将值解析为datetime,请添加参数errors='coerce '以获取NAT:s=PD.series ([ ' 01 apr 2017 6:59 ',' 01apr20176336059 ',' a
是打印(s )
0 01APR2017 6:59
1 01APR2017 6:59
2安
dtype :对象
print(PD.to_datetime(s,format='%d%b%Y %H:%M ',errors='coerce ' ) )
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00
2 NaT
dtype: datetime64[ns]