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

时间:2018-03-16 20:13:03

标签: python pandas time

我正在尝试使用3个pandas列创建一个pandas datetime系列。当我调用to_datetime()方法时,它会抛出一个错误,即使我正在以正确的年,月,日格式提供int64 dtypes的系列输入。

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)

1 个答案:

答案 0 :(得分:3)

如错误所示,

git add -u app\styles\*

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