ValueError:时间数据 - 与格式不匹配 - Pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45922767/
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
ValueError: time data- does not match format - Pandas
提问by jezrael
I know this question has been asked many times and there are many answers. I followed the previous threads, still not getting my answer.
When I did this, without errors= coerce
我知道这个问题已经被问过很多次,也有很多答案。我遵循了以前的线程,仍然没有得到我的答案。当我这样做时,没有errors= coerce
df['DATE'] = pd.to_datetime(df['Date'], format= "%d-%b-%Y %H:%M:%S")
I get an error:
我收到一个错误:
ValueError: time data '26-Aug-17 10:11:29' does not match format '%d-%b-%Y %H:%M:%S' (match)
ValueError: 时间数据 '26-Aug-17 10:11:29' 不匹配格式 '%d-%b-%Y %H:%M:%S'(匹配)
As you can see, I think I have the right format. But still it gives error.
如您所见,我认为我有正确的格式。但它仍然给出错误。
When I do with errors= coerce
according to thispost,
当我errors= coerce
按照这篇文章做的时候,
df['DATE'] = pd.to_datetime(df['Date'], format= "%d-%b-%Y %H:%M:%S", `errors= coerce`)
This runs fine, but I get NaT
in my DATE
column. For example, like this,
这运行良好,但我进入NaT
了我的DATE
专栏。比如像这样,
Date X DATE
0 26-Aug-17 10:11:29 95.617378 NaT
1 26-Aug-17 11:11:29 93.617378 NaT
2 26-Aug-17 12:11:29 91.617378 NaT
3 26-Aug-17 13:11:29 90.000000 NaT
I have the format correct and still can not find why am I getting this error. Any thoughts will be helpful.
我的格式正确,但仍然无法找到为什么会出现此错误。任何想法都会有所帮助。
回答by jezrael
You need change Y
to y
, because Y
required full year, but y
only last 2 digits, see http://strftime.org/:
您需要更改Y
为y
,因为Y
需要全年,但y
只有最后 2 位数字,请参阅http://strftime.org/:
df['DATE'] = pd.to_datetime(df['Date'], format= "%d-%b-%y %H:%M:%S")
print (df)
Date X DATE
0 26-Aug-17 10:11:29 95.617378 2017-08-26 10:11:29
1 26-Aug-17 11:11:29 93.617378 2017-08-26 11:11:29
2 26-Aug-17 12:11:29 91.617378 2017-08-26 12:11:29
3 26-Aug-17 13:11:29 90.000000 2017-08-26 13:11:29