Данные времени за 5 января 2017 г. не соответствуют формату в блокноте Jupyter ⇐ Python

Программы на Python
Anonymous
Данные времени за 5 января 2017 г. не соответствуют формату в блокноте Jupyter

Сообщение Anonymous »

Код: Выделить всё

import pandas as pd
dates=['2017-01-05','Jan 5,2017','20170105']
pd.to_datetime(dates)
Я ожидаю, что дата будет
5 января 2017 г. (ГГ/мм/дд). Но я обнаружил ошибку значения:

Код: Выделить всё

Traceback (most recent call last):
File "", line 1, in 
File "C:\dev\Python312\Lib\site-packages\pandas\core\tools\datetimes.py", line 1099, in to_datetime
result = convert_listlike(argc, format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\Python312\Lib\site-packages\pandas\core\tools\datetimes.py", line 433, in _convert_listlike_datetimes
return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\Python312\Lib\site-packages\pandas\core\tools\datetimes.py", line 467, in _array_strptime_with_fallback
result, tz_out = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "strptime.pyx", line 501, in pandas._libs.tslibs.strptime.array_strptime
File "strptime.pyx", line 451, in pandas._libs.tslibs.strptime.array_strptime
File "strptime.pyx", line 583, in pandas._libs.tslibs.strptime._parse_with_format
ValueError: time data "Jan 5,2017" doesn't match format "%Y-%m-%d", at position 1. You might want to try:
- passing `format` if your strings have a consistent format;
- passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
- passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.
Пожалуйста, дайте мне решение. Подскажите, пожалуйста, как правильно это сделать. Я хочу, чтобы дата была такой же.

Подробнее здесь: https://stackoverflow.com/questions/790 ... r-notebook

Вернуться в «Python»