Python 时间数据与格式不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25015711/
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
time data does not match format
提问by Stephane Rolland
I get the following error:
我收到以下错误:
time data '07/28/2014 18:54:55.099000' does not match format '%d/%m/%Y %H:%M:%S.%f'
But I cannot see what parameter is wrong in %d/%m/%Y %H:%M:%S.%f?
但我看不出是什么参数有问题%d/%m/%Y %H:%M:%S.%f?
This is the code I use.
这是我使用的代码。
from datetime import datetime
time_value = datetime.strptime(csv_line[0] + '000', '%d/%m/%Y %H:%M:%S.%f')
I have added and removed the 000but I get the same error.
我已经添加和删除了,000但我得到了同样的错误。
采纳答案by Martijn Pieters
You have the month and day swapped:
您交换了月份和日期:
'%m/%d/%Y %H:%M:%S.%f'
28will never fit in the range for the %mmonth parameter otherwise.
28否则永远不会适合%m月份参数的范围。
With %mand %din the correct order parsing works:
随着%m并%d以正确的顺序解析的工作原理:
>>> from datetime import datetime
>>> datetime.strptime('07/28/2014 18:54:55.099000', '%m/%d/%Y %H:%M:%S.%f')
datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
You don't need to add '000'; %fcan parse shorter numbers correctly:
你不需要添加'000'; %f可以正确解析较短的数字:
>>> datetime.strptime('07/28/2014 18:54:55.099', '%m/%d/%Y %H:%M:%S.%f')
datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
回答by Basant Kumar
To compare date time, you can try this. Datetime format can be changed
要比较日期时间,您可以试试这个。日期时间格式可以更改
from datetime import datetime
>>> a = datetime.strptime("10/12/2013", "%m/%d/%Y")
>>> b = datetime.strptime("10/15/2013", "%m/%d/%Y")
>>> a>b
False
回答by Fábio
While the above answer is 100% helpful and correct, I'd like to add the following since only a combination of the above answer and reading through the pandas doc helped me:
虽然上述答案 100% 有帮助且正确,但我想添加以下内容,因为只有上述答案和通读大熊猫文档的结合才能帮助我:
2-digit / 4-digit year
2 位数/4 位数年份
It is noteworthy, that in order to parse through a 2-digit year, e.g. '90' rather than '1990', a %yis required instead of a %Y.
值得注意的是,为了解析2位数的年份,例如'90'而不是'1990',%y需要a而不是a %Y。
Infer the datetime automatically
自动推断日期时间
If parsing with a pre-defined format still doesn't work for you, try using the flag infer_datetime_format=True, for example:
如果使用预定义格式解析仍然不适合您,请尝试使用 flag infer_datetime_format=True,例如:
yields_df['Date'] = pd.to_datetime(yields_df['Date'], infer_datetime_format=True)
Be advised that this solution is slower than using a pre-defined format.
请注意,此解决方案比使用预定义格式慢。
回答by Anatoly Alekseev
I had a case where solution was hard to figure out. This is not exactly relevant to particular question, but might help someone looking to solve a case with same error message when strptime is fed with timezone information. In my case, the reason for throwing
我有一个解决方案很难弄清楚的情况。这与特定问题并不完全相关,但可能会帮助那些希望在 strptime 提供时区信息时解决具有相同错误消息的案例的人。就我而言,抛出的原因
ValueError: time data '2016-02-28T08:27:16.000-07:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
ValueError: 时间数据 '2016-02-28T08:27:16.000-07:00' 与格式 '%Y-%m-%dT%H:%M:%S.%f%z' 不匹配
was presence of last colon in the timezone part. While in some locales (Russian one, for example) code was able to execute well, in another (English one) it was failing. Removing the last colon helped remedy my situation.
是时区部分中最后一个冒号的存在。虽然在某些语言环境(例如俄语)中代码能够很好地执行,但在另一种语言环境(英语)中却失败了。去除最后一个冒号有助于纠正我的情况。
回答by Amit Kumar
No need to use datetime library. Using the dateutil library there is no need of any format:
无需使用日期时间库。使用 dateutil 库不需要任何格式:
>>> from dateutil import parser
>>> s= '25 April, 2020, 2:50, pm, IST'
>>> parser.parse(s)
datetime.datetime(2020, 4, 25, 14, 50)
回答by UdonN00dle
I had the exact same error but with slightly different format and root-cause, and since this is the first Q&A that pops up when you search for "time data does not match format", I thought I'd leave the mistake I made for future viewers:
我有完全相同的错误,但格式和根本原因略有不同,而且由于这是您搜索“时间数据与格式不匹配”时弹出的第一个问答,我想我会留下我犯的错误未来的观众:
My initial code:
我的初始代码:
start = datetime.strptime('05-SEP-19 00.00.00.000 AM', '%d-%b-%y %I.%M.%S.%f %p')
Where I used %Ito parse the hours and %pto parse 'AM/PM'.
我用来%I解析小时数和%p解析“AM/PM”的地方。
The error:
错误:
ValueError: time data '05-SEP-19 00.00.00.000000 AM' does not match format '%d-%b-%y %I.%M.%S.%f %p'
ValueError:时间数据“05-SEP-19 00.00.00.000000 AM”与格式“%d-%b-%y %I.%M.%S.%f %p”不匹配
I was going through the datetime docsand finally realized in 12-hour format %I, there is no 00... once I changed 00.00.00to 12.00.00, the problem was resolved.
我正在浏览日期时间文档,最终以 12 小时格式实现,%I没有 00...一旦我更改00.00.00为12.00.00,问题就解决了。
So it's either 01-12 using %Iwith %p, or 00-23 using %H.
所以它要么是 01-12 使用%Iwith %p,要么是 00-23 使用%H.

