首页 > 编程知识 正文

pandas匹配字符串,pandas时间戳转换日期格式

时间:2023-05-04 07:32:45 阅读:16697 作者:1174

^{}可以与参数格式一起使用。 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]

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。