简体   繁体   English

使用strptime,一天超出了一个月的范围

[英]day is out of range for month using strptime

I have long list of date in dataframe that need to be converted into datetime, the date is in the form "%d%m%Y" , and I apply datetime.strptime(x,'%d%m%Y') , which works fine until meet the date "3122012" , which should be datetime.datetime(2012, 12, 3, 0, 0) , but instead it throw the error message: 我在数据框中有很长的日期列表,需要转换为datetime,日期格式为“%d%m%Y” ,我应用datetime.strptime(x,'%d%m%Y') ,哪个工作正常,直到满足日期“3122012” ,应该是datetime.datetime( 2012,12,3,0,0 ,但它会抛出错误消息:

day is out of range for month

which is obviously because the program consider it as 31/2/2012, which doesn't exist. 这显然是因为该计划将其视为31/2/2012,这是不存在的。 any suggestions to avoid such problems?Thanks! 有什么建议可以避免这些问题吗?谢谢!

This is just part of a much larger problem: Your format is inherently ambiguous. 这只是一个更大问题的一部分:您的格式本质上是模棱两可的。 * *

For example, both 1-11-2012 and 11-1-2012 will format as 1112012 . 例如,无论是1-11-201211-1-2012将作为格式化1112012 So, how are you going to parse that? 那么,你打算怎么解析呢? You can invent and implement a disambiguation rule for that, but whichever one you choose, the other date can no longer be represented by your system. 您可以为此创建并实施消歧规则,但无论您选择哪一个,您的系统都无法再表示另一个日期。

If that's OK with you, then whatever rule you implement for disambiguating 1112012 will automatically handle 3122012 as well. 如果您对此没有问题,那么您为消除歧义1112012而实施的任何规则也将自动处理3122012 For example: 例如:

def parse_dmy(s):
    if len(s) == 6: s = '0' + s[0] + '0' + s[1:]
    elif len(s) == 7: s = '0' + s # or s[0] + '0' + s[1:]
    return datetime.strptime(x, '%d%m%Y')

I picked the '0' + s rule rather than the s[0] + '0' + s[1:] rule because the former gives you 03-12-2012 for this example, while the latter gives you 31-02-2012 . 我选择了'0' + s规则而不是s[0] + '0' + s[1:]规则,因为前者给你03-12-2012这个例子,而后者给你31-02-2012 But, again, neither rule works for all values. 但是,同样,这两条规则都不适用于所有价值观。


* Also, %d and %m explicitly mean "0-padded", so your strings are technically invalid… but most platforms are happy with non-padded numbers in, eg, %d-%m-%Y —I believe POSIX requires them to be happy, and Windows is happy, and what other non-POSIX platform do you care about? *此外, %d%m明确表示“0-padded”,因此您的字符串在技术上无效...但大多数平台都对非填充数字感到满意,例如%d-%m-%Y我相信POSIX需要他们很开心,Windows很开心,你还关心其他非POSIX平台吗? Anyway, that would just be a minor problem if not for the larger problem that there is no way to unambiguously convert your strings into a valid format. 无论如何,如果不是因为没有办法明确地将字符串转换为有效格式的大问题,那只会是一个小问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM