pandas 将熊猫列转换为日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51103686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:45:53  来源:igfitidea点击:

Converting pandas Column to datetime

pythonpandasdatetime

提问by rhanly

I am trying to convert a pandas column to datetime. This is my error message.

我正在尝试将 Pandas 列转换为日期时间。这是我的错误信息。

ValueError: time data '01-JUN-17 00:00:00' does not match format '%d-%b-%y %H.%M.%S' (match)

ValueError: 时间数据 '01-JUN-17 00:00:00' 不匹配格式 '%d-%b-%y %H.%M.%S'(匹配)

This is my code :

这是我的代码:

df['dayofservice'] =  pd.to_datetime(df['dayofservice'], format = '%d-%b-%y %H.%M.%S')

I have read this documentation to ensure my format is correct : https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

我已阅读此文档以确保我的格式正确:https: //docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

It's still not working for me.

它仍然不适合我。

回答by EdChum

pandasis man/woman enough to parse this without a format field:

pandas男人/女人足以在没有格式字段的情况下解析这个:

In[90]:
pd.to_datetime('01-JUN-17 00:00:00')

Out[90]: Timestamp('2017-06-01 00:00:00')

So this should work:

所以这应该有效:

df['dayofservice'] =  pd.to_datetime(df['dayofservice'])

回答by jezrael

Change .to :in times like:

更改.为以下:时间:

df['dayofservice'] =  pd.to_datetime(df['dayofservice'], format = '%d-%b-%y %H:%M:%S')