简体   繁体   English

ValueError:日期超出月份的范围

[英]ValueError: day is out of range for month

I want to convert a string from a dataframe to datetime.我想将字符串从 dataframe 转换为日期时间。

dfx = df.ix[:,'a']
dfx = pd.to_datetime(dfx)

But it gives the following error:但它给出了以下错误:

ValueError: day is out of range for month ValueError:日期超出月份的范围

Can anyone help?谁能帮忙?

Maybe help add parameter dayfirst=True to to_datetime , if format of datetime is 30-01-2016 :如果日期30-01-2016格式是30-01-2016 ,也许可以帮助将参数dayfirst=True添加到to_datetime

dfx = df.ix[:,'a']
dfx = pd.to_datetime(dfx, dayfirst=True)

More universal is use parameter format with errors='coerce' for replacing values with other format to NaN :更通用的是使用带有errors='coerce'参数format将其他format值替换为NaN

dfx = '30-01-2016'

dfx = pd.to_datetime(dfx, format='%d-%m-%Y', errors='coerce')
print (dfx)
2016-01-30 00:00:00

Sample:样品:

dfx = pd.Series(['30-01-2016', '15-09-2015', '40-09-2016'])
print (dfx)
0    30-01-2016
1    15-09-2015
2    40-09-2016
dtype: object

dfx = pd.to_datetime(dfx, format='%d-%m-%Y', errors='coerce')
print (dfx)
0   2016-01-30
1   2015-09-15
2          NaT
dtype: datetime64[ns]

If format is standard (eg 01-30-2016 or 01-30-2016 ), add only errors='coerce' :如果格式是标准的(例如01-30-201601-30-2016 ),只添加errors='coerce'

dfx = pd.Series(['01-30-2016', '09-15-2015', '09-40-2016'])
print (dfx)
0    01-30-2016
1    09-15-2015
2    09-40-2016
dtype: object

dfx = pd.to_datetime(dfx, errors='coerce')
print (dfx)
0   2016-01-30
1   2015-09-15
2          NaT
dtype: datetime64[ns]

Well in my case以我为例

year = 2023
month = 2
date = datetime.date(year, month, 30)

got me this error because February month has 29 or 28 days in it.给我这个错误是因为二月份有 29 或 28 天。 Maybe that point helps someone也许这一点可以帮助某人

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

相关问题 ValueError:日期超出月份日期时间的范围 - ValueError: day is out of range for month datetime datetime: ValueError: day is out of range for month - datetime : ValueError: day is out of range for month ValueError:南部模式迁移和数据迁移的日期超出月份范围 - ValueError: day is out of range for month on south schemamigration and datamigration Python dateutil.parser抛出“ValueError:day超出范围的月份” - Python dateutil.parser throws “ValueError: day is out of range for month” “ ValueError:日期超出了月份范围”,因为数据中的所有月份中的所有月份共有31天 - 'ValueError: day is out of range for month' as all the days have 31 days in all month in the data python的日期超出范围 - day is out of range for month python ValueError: day is out of range for month - 如何对不同月份的日期进行数学运算 - ValueError: day is out of range for month - How to do math with dates that are in different months 使用strptime,一天超出了一个月的范围 - day is out of range for month using strptime 一天超出范围误差怀疑其闰年的原因 - day is out of range for month error suspecting its cause of leap year 获取明天(元旦)的时间戳失败,日期超出月份范围 - Getting Tomorrow's (New Years Day) Timestamp fails with day is out of range for month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM