Skip to content

Commit

Permalink
"Backport PR #53257 on branch 2.0.x (BUG: Add AM/PM token support on …
Browse files Browse the repository at this point in the history
…guess_datetime_format)" (#53277)

Backport PR #53257: BUG: Add AM/PM token support on guess_datetime_format

---------

Co-authored-by: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com>
(cherry picked from commit f2de598)

Co-authored-by: Julian Badillo <j.badillo@numat-tech.com>
  • Loading branch information
MarcoGorelli and juli4nb4dillo committed May 17, 2023
1 parent 8983c5d commit 456a041
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Bug fixes
- Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`)
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
- Bug in :func:`merge` when merging on datetime columns on different resolutions (:issue:`53200`)
- Bug in :func:`to_datetime` was inferring format to contain ``"%H"`` instead of ``"%I"`` if date contained "AM" / "PM" tokens (:issue:`53147`)
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
- Bug in :meth:`DataFrame.sort_values` raising for PyArrow ``dictionary`` dtype (:issue:`53232`)
- Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:

output_format.append(tokens[i])

# if am/pm token present, replace 24-hour %H, with 12-hour %I
if "%p" in output_format and "%H" in output_format:
i = output_format.index("%H")
output_format[i] = "%I"

guessed_format = "".join(output_format)

try:
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,9 @@ class TestDatetimeParsingWrappers:
("2005-11-09 10:15", datetime(2005, 11, 9, 10, 15)),
("2005-11-09 08H", datetime(2005, 11, 9, 8, 0)),
("2005/11/09 10:15", datetime(2005, 11, 9, 10, 15)),
("2005/11/09 10:15:32", datetime(2005, 11, 9, 10, 15, 32)),
("2005/11/09 10:15:32 AM", datetime(2005, 11, 9, 10, 15, 32)),
("2005/11/09 10:15:32 PM", datetime(2005, 11, 9, 22, 15, 32)),
("2005/11/09 08H", datetime(2005, 11, 9, 8, 0)),
("Thu Sep 25 10:36:28 2003", datetime(2003, 9, 25, 10, 36, 28)),
("Thu Sep 25 2003", datetime(2003, 9, 25)),
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,10 @@ def test_parsers_month_freq(date_str, expected):
("2011-12-30T00:00:00.000000+9:0", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+09:", None),
("2011-12-30 00:00:00.000000", "%Y-%m-%d %H:%M:%S.%f"),
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %H:%M:%S %p"),
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %H:%M:%S %p"),
("Tue 24 Aug 2021 01:30:48", "%a %d %b %Y %H:%M:%S"),
("Tuesday 24 Aug 2021 01:30:48", "%A %d %b %Y %H:%M:%S"),
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %I:%M:%S %p"),
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %I:%M:%S %p"),
("27.03.2003 14:55:00.000", "%d.%m.%Y %H:%M:%S.%f"), # GH50317
],
)
Expand Down

0 comments on commit 456a041

Please sign in to comment.