Skip to content

Commit e456a96

Browse files
juli4nb4dilloMarcoGorelli
authored andcommitted
'Backport PR pandas-dev#53257: BUG: Add AM/PM token support on guess_datetime_format'
* BUG: Add am/pm parsing support on guess_format * ⛏️ add unit tests * more unit tests * ⛏️ not guess am/pm mark * 🪲 bug fix parsing am/pm tokens * 📝 whatsnew * 📝 whats new doc * isort on whatsnew entry * move whatsnew note to 2.0.1 * 2.0.2, not 2.0.1 * clarify --------- Co-authored-by: MarcoGorelli <[email protected]> (cherry picked from commit f2de598)
1 parent 340346c commit e456a96

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

doc/source/whatsnew/v2.0.2.rst

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Bug fixes
2929
- Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`)
3030
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
3131
- Bug in :func:`merge` when merging on datetime columns on different resolutions (:issue:`53200`)
32+
- Bug in :func:`to_datetime` was inferring format to contain ``"%H"`` instead of ``"%I"`` if date contained "AM" / "PM" tokens (:issue:`53147`)
33+
- Bug in :func:`to_timedelta` was raising ``ValueError`` with ``pandas.NA`` (:issue:`52909`)
34+
- Bug in :meth:`DataFrame.__getitem__` not preserving dtypes for :class:`MultiIndex` partial keys (:issue:`51895`)
3235
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
3336
- Bug in :meth:`DataFrame.sort_values` raising for PyArrow ``dictionary`` dtype (:issue:`53232`)
3437
- Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`)

pandas/_libs/tslibs/parsing.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
990990

991991
output_format.append(tokens[i])
992992

993+
# if am/pm token present, replace 24-hour %H, with 12-hour %I
994+
if "%p" in output_format and "%H" in output_format:
995+
i = output_format.index("%H")
996+
output_format[i] = "%I"
997+
993998
guessed_format = "".join(output_format)
994999

9951000
try:

pandas/tests/tools/test_to_datetime.py

+3
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,9 @@ class TestDatetimeParsingWrappers:
29532953
("2005-11-09 10:15", datetime(2005, 11, 9, 10, 15)),
29542954
("2005-11-09 08H", datetime(2005, 11, 9, 8, 0)),
29552955
("2005/11/09 10:15", datetime(2005, 11, 9, 10, 15)),
2956+
("2005/11/09 10:15:32", datetime(2005, 11, 9, 10, 15, 32)),
2957+
("2005/11/09 10:15:32 AM", datetime(2005, 11, 9, 10, 15, 32)),
2958+
("2005/11/09 10:15:32 PM", datetime(2005, 11, 9, 22, 15, 32)),
29562959
("2005/11/09 08H", datetime(2005, 11, 9, 8, 0)),
29572960
("Thu Sep 25 10:36:28 2003", datetime(2003, 9, 25, 10, 36, 28)),
29582961
("Thu Sep 25 2003", datetime(2003, 9, 25)),

pandas/tests/tslibs/test_parsing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,10 @@ def test_parsers_month_freq(date_str, expected):
201201
("2011-12-30T00:00:00.000000+9:0", "%Y-%m-%dT%H:%M:%S.%f%z"),
202202
("2011-12-30T00:00:00.000000+09:", None),
203203
("2011-12-30 00:00:00.000000", "%Y-%m-%d %H:%M:%S.%f"),
204-
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %H:%M:%S %p"),
205-
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %H:%M:%S %p"),
204+
("Tue 24 Aug 2021 01:30:48", "%a %d %b %Y %H:%M:%S"),
205+
("Tuesday 24 Aug 2021 01:30:48", "%A %d %b %Y %H:%M:%S"),
206+
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %I:%M:%S %p"),
207+
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %I:%M:%S %p"),
206208
("27.03.2003 14:55:00.000", "%d.%m.%Y %H:%M:%S.%f"), # GH50317
207209
],
208210
)

0 commit comments

Comments
 (0)