Python pandas.to_datetime ValueError:无法组合日期时间

Python pandas.to_datetime ValueError:无法组合日期时间,python,pandas,time,Python,Pandas,Time,我正在尝试使用3个pandas列创建一个pandas日期时间序列。当我调用to_datetime()方法时,它会抛出一个错误,即使我正在以适当的年、月、日格式向它提供int64数据类型的系列输入 pd.to_datetime(df[['year', 'month', 'day']]) 错误堆栈的最后一行是最奇怪的,因为它似乎显示我的值是%Y%m%d格式的,但它不会接受它们 -------------------------------------------------------------

我正在尝试使用3个pandas列创建一个pandas日期时间序列。当我调用to_datetime()方法时,它会抛出一个错误,即使我正在以适当的年、月、日格式向它提供int64数据类型的系列输入

pd.to_datetime(df[['year', 'month', 'day']])
错误堆栈的最后一行是最奇怪的,因为它似乎显示我的值是%Y%m%d格式的,但它不会接受它们

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)

    302             try:

--> 303                 values, tz = tslib.datetime_to_datetime64(arg)

    304                 return DatetimeIndex._simple_new(values, name=name, tz=tz)



pandas/_libs/tslib.pyx in pandas._libs.tslib.datetime_to_datetime64()



TypeError: Unrecognized value type: <class 'int'>



During handling of the above exception, another exception occurred:



ValueError                                Traceback (most recent call last)

/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in _assemble_from_unit_mappings(arg, errors)

    476     try:

--> 477         values = to_datetime(values, format='%Y%m%d', errors=errors)

    478     except (TypeError, ValueError) as e:



/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin)

    372         from pandas import Series

--> 373         values = _convert_listlike(arg._values, True, format)

    374         result = Series(values, index=arg.index, name=arg.name)



/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)

    305             except (ValueError, TypeError):

--> 306                 raise e

    307



/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)

    272                         result = array_strptime(arg, format, exact=exact,

--> 273                                                 errors=errors)

    274                     except tslib.OutOfBoundsDatetime:



pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()



ValueError: time data 20170200 does not match format '%Y%m%d' (match)



During handling of the above exception, another exception occurred:



ValueError                                Traceback (most recent call last)

<ipython-input-118-cd147256a618> in <module>()


----> 2 pd.to_datetime(df[['year', 'month', 'day']])



/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin)

    374         result = Series(values, index=arg.index, name=arg.name)

    375     elif isinstance(arg, (ABCDataFrame, MutableMapping)):

--> 376         result = _assemble_from_unit_mappings(arg, errors=errors)

    377     elif isinstance(arg, ABCIndexClass):

    378         result = _convert_listlike(arg, box, format, name=arg.name)



/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in _assemble_from_unit_mappings(arg, errors)

    478     except (TypeError, ValueError) as e:

    479         raise ValueError("cannot assemble the "

--> 480                          "datetimes: {error}".format(error=e))

    481

    482     for u in ['h', 'm', 's', 'ms', 'us', 'ns']:



ValueError: cannot assemble the datetimes: time data 20170200 does not match format '%Y%m%d' (match)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in_convert_listlike(arg,box,format,name,tz)
302试试看:
-->303个值,tz=tslib.datetime_到_datetime64(arg)
304返回日期时间索引。\u简单\u新(值,name=name,tz=tz)
pandas/_libs/tslib.pyx在pandas中。_libs.tslib.datetime_到_datetime64()
TypeError:无法识别的值类型:
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py从单元映射(arg,错误)
476试试:
-->477 values=to_datetime(值,格式='%Y%m%d',errors=errors)
478(类型错误、值错误)除外,如e:
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg,errors,dayfirst,yearfirst,utc,box,format,exact,unit,expert_datetime,origin)
熊猫进口系列372只
-->373 values=\u convert\u listlike(arg.\u values,True,format)
374结果=系列(值,索引=arg.index,名称=arg.name)
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in_convert_listlike(arg,box,format,name,tz)
305除外(ValueError,TypeError):
-->306升e
307
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in_convert_listlike(arg,box,format,name,tz)
272结果=数组\u strtime(参数,格式,精确=精确,
-->273错误=错误)
274除tslib.OutOfBoundsDatetime外:
pandas/_-libs/tslibs/strtime.pyx在pandas中。_-libs.tslibs.strtime.array_-strtime()
ValueError:时间数据20170200与格式“%Y%m%d”不匹配(匹配)
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
在()
---->2 pd.截止日期时间(df['年','月','日]]
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg,errors,dayfirst,yearfirst,utc,box,format,exact,unit,expert_datetime,origin)
374结果=系列(值,索引=arg.index,名称=arg.name)
375 elif isinstance(arg,(ABCDATA帧,可变映射)):
-->376结果=\u从\u单元\u映射中组装\u(arg,errors=errors)
377 elif isinstance(arg,abss):
378 result=\u convert\u listlike(arg,box,format,name=arg.name)
/opt/python/python35/lib/python3.5/site-packages/pandas/core/tools/datetimes.py从单元映射(arg,错误)
478(类型错误、值错误)除外,如e:
479提升值错误(“无法组装”
-->480“日期时间:{error}”。格式(error=e))
481
482在[h]、[m]、[s]、[ms]、[us]、[ns]中表示u:
ValueError:无法汇编日期时间:时间数据20170200与格式“%Y%m%d”不匹配(匹配)
如错误所示

ValueError: cannot assemble the datetimes: time data 20170200 does not match format '%Y%m%d' (match)

您的日期为00,这不是有效的日期输入。有效日期范围介于1和31之间。再次检查数据,确保所有列都在允许范围内。

如果没有解决方案,您的答案是不完整的。