Skip to content

DOC: how to_datetime %S differs from strptime #48376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,11 +777,19 @@ def to_datetime(
#time-zone-handling>`_.

format : str, default None
The strftime to parse time, e.g. :const:`"%d/%m/%Y"`. Note that
:const:`"%f"` will parse all the way up to nanoseconds. See
The strftime to parse time, e.g. :const:`"%d/%m/%Y"`. See
`strftime documentation
<https://docs.python.org/3/library/datetime.html
#strftime-and-strptime-behavior>`_ for more information on choices.
#strftime-and-strptime-behavior>`_ for more information on choices, though
note the following differences:

- :const:`"%f"` will parse all the way
up to nanoseconds;

- :const:`"%S"` without :const:`"%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.

exact : bool, default True
Control how `format` is used:

Expand Down Expand Up @@ -950,6 +958,21 @@ def to_datetime(
DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'],
dtype='datetime64[ns]', freq=None)

**Differences with strptime behavior**

:const:`"%f"` will parse all the way up to nanoseconds.

>>> pd.to_datetime('2018-10-26 12:00:00.0000000011',
... format='%Y-%m-%d %H:%M:%S.%f')
Timestamp('2018-10-26 12:00:00.000000001')

:const:`"%S"` without :const:`"%f"` will capture all the way
up to nanoseconds if present as decimal places.

>>> pd.to_datetime('2017-03-22 15:16:45.433502912',
... format='%Y-%m-%d %H:%M:%S')
Timestamp('2017-03-22 15:16:45.433502912')

**Non-convertible date/times**

If a date does not meet the `timestamp limitations
Expand Down