pd.to_datetime 格式参数被拒绝
pd.to_datetime format argument is rejected
我无法理解这个异常:
date = '01/01 24:00:00'
pd.to_datetime(date, format = '%m/%d %H:%M:%S')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
431 try:
--> 432 values, tz = conversion.datetime_to_datetime64(arg)
433 return DatetimeIndex._simple_new(values, name=name, tz=tz)
pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-30-1c006106355f> in <module>
----> 1 pd.to_datetime(date, format = '%m/%d %H:%M:%S')
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
754 result = convert_listlike(arg, format)
755 else:
--> 756 result = convert_listlike(np.array([arg]), format)[0]
757
758 return result
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
433 return DatetimeIndex._simple_new(values, name=name, tz=tz)
434 except (ValueError, TypeError):
--> 435 raise e
436
437 if result is None:
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
398 try:
399 result, timezones = array_strptime(
--> 400 arg, format, exact=exact, errors=errors
401 )
402 if "%Z" in format or "%z" in format:
pandas\_libs\tslibs\strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: time data '01/01 24:00:00' does not match format '%m/%d %H:%M:%S' (match)
我该如何补救代码?
简答:24 小时无效。
小时数应在 0-23 范围内,如 %H
格式指令的 datetime
包中所指定:
%H Hour (24-hour clock) as a zero-padded decimal number. <b>00, 01, …, 23</b>
所以 24
不在那个范围内。如果时钟设置为01/01 24:00:00
,那么这是01/02 00:00:00
。
我无法理解这个异常:
date = '01/01 24:00:00'
pd.to_datetime(date, format = '%m/%d %H:%M:%S')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
431 try:
--> 432 values, tz = conversion.datetime_to_datetime64(arg)
433 return DatetimeIndex._simple_new(values, name=name, tz=tz)
pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-30-1c006106355f> in <module>
----> 1 pd.to_datetime(date, format = '%m/%d %H:%M:%S')
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
754 result = convert_listlike(arg, format)
755 else:
--> 756 result = convert_listlike(np.array([arg]), format)[0]
757
758 return result
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
433 return DatetimeIndex._simple_new(values, name=name, tz=tz)
434 except (ValueError, TypeError):
--> 435 raise e
436
437 if result is None:
~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
398 try:
399 result, timezones = array_strptime(
--> 400 arg, format, exact=exact, errors=errors
401 )
402 if "%Z" in format or "%z" in format:
pandas\_libs\tslibs\strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: time data '01/01 24:00:00' does not match format '%m/%d %H:%M:%S' (match)
我该如何补救代码?
简答:24 小时无效。
小时数应在 0-23 范围内,如 %H
格式指令的 datetime
包中所指定:
%H Hour (24-hour clock) as a zero-padded decimal number. <b>00, 01, …, 23</b>
所以 24
不在那个范围内。如果时钟设置为01/01 24:00:00
,那么这是01/02 00:00:00
。