[英]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-2016
或01-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.