Closed
Description
Pandas version checks
- I have checked that the issue still exists on the latest versions of the docs on
main
here
Location of the documentation
https://pandas.pydata.org/docs/dev/reference/api/pandas.to_datetime.html
Documentation problem
The sentence
"%S" without "%f" will capture all the way up to nanoseconds if present as decimal places, and will also handle the case where the number of seconds is an integer.
isn't generally true - it's only true for the ISO8601 fastpath, and that's because of a bug (which I'm looking at addressing)
Demonstration of the issue:
>>> import pandas as pd
>>> pd.to_datetime('2000-01-01 00:00:00.123456', format='%Y-%m-%d %H:%M:%S') # iso8601 fastpath
Timestamp('2000-01-01 00:00:00.123456')
>>> pd.to_datetime('2000|01|01 00:00:00.123456', format='%Y|%m|%d %H:%M:%S') # no fastpath
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/megorelli/.venv/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 1102, in to_datetime
result = convert_listlike(np.array([arg]), format)[0]
File "/home/megorelli/.venv/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 430, in _convert_listlike_datetimes
res = _to_datetime_with_format(
File "/home/megorelli/.venv/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 538, in _to_datetime_with_format
res = _array_strptime_with_fallback(
File "/home/megorelli/.venv/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 473, in _array_strptime_with_fallback
result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
File "pandas/_libs/tslibs/strptime.pyx", line 156, in pandas._libs.tslibs.strptime.array_strptime
ValueError: unconverted data remains: .123456
Suggested fix for documentation
- remove this from the
format : str, default None
section
"%S" without "%f" will capture all the way up to nanoseconds if present as decimal places, and will also handle the case where the number of seconds is an integer.
- remove this from the "Differences with strptime behavior" section
"%S" without "%f" will capture all the way up to nanoseconds if present as decimal places.