Reputation: 15
I'm trying to create a pandas datetime series using 3 pandas columns. When I call the to_datetime() method, it throws an error, even though I'm feeding it series inputs of int64 dtypes in proper year, month, day format.
pd.to_datetime(df[['year', 'month', 'day']])
The last line of the error stack is the weirdest, because it seems to show my values are of %Y%m%d format, yet it won't accept them.
---------------------------------------------------------------------------
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)
Upvotes: 1
Views: 2560
Reputation: 184
As the error shows,
ValueError: cannot assemble the datetimes: time data 20170200 does not match format '%Y%m%d' (match)
your day is 00, which is not a valid input for day. The valid day range is between 1 and 31. Check your data again make sure all the columns are within allowable range.
Upvotes: 3