Skip to content

Commit 82b35da

Browse files
juli4nb4dilloMarcoGorelli
authored and
im-vinicius
committed
BUG: Add AM/PM token support on guess_datetime_format (pandas-dev#53257)
* 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]>
1 parent 44b98be commit 82b35da

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

doc/source/whatsnew/v2.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ 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`)
3233
- Bug in :func:`to_timedelta` was raising ``ValueError`` with ``pandas.NA`` (:issue:`52909`)
3334
- Bug in :meth:`DataFrame.__getitem__` not preserving dtypes for :class:`MultiIndex` partial keys (:issue:`51895`)
3435
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)

pandas/_libs/tslibs/parsing.pyx

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

10201020
output_format.append(tokens[i])
10211021

1022+
# if am/pm token present, replace 24-hour %H, with 12-hour %I
1023+
if "%p" in output_format and "%H" in output_format:
1024+
i = output_format.index("%H")
1025+
output_format[i] = "%I"
1026+
10221027
guessed_format = "".join(output_format)
10231028

10241029
try:

pandas/tests/tools/test_to_datetime.py

+3
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,9 @@ class TestDatetimeParsingWrappers:
29422942
("2005-11-09 10:15", datetime(2005, 11, 9, 10, 15)),
29432943
("2005-11-09 08H", datetime(2005, 11, 9, 8, 0)),
29442944
("2005/11/09 10:15", datetime(2005, 11, 9, 10, 15)),
2945+
("2005/11/09 10:15:32", datetime(2005, 11, 9, 10, 15, 32)),
2946+
("2005/11/09 10:15:32 AM", datetime(2005, 11, 9, 10, 15, 32)),
2947+
("2005/11/09 10:15:32 PM", datetime(2005, 11, 9, 22, 15, 32)),
29452948
("2005/11/09 08H", datetime(2005, 11, 9, 8, 0)),
29462949
("Thu Sep 25 10:36:28 2003", datetime(2003, 9, 25, 10, 36, 28)),
29472950
("Thu Sep 25 2003", datetime(2003, 9, 25)),

pandas/tests/tslibs/test_parsing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ def test_parsers_month_freq(date_str, expected):
208208
("2011-12-30T00:00:00.000000+9:0", "%Y-%m-%dT%H:%M:%S.%f%z"),
209209
("2011-12-30T00:00:00.000000+09:", None),
210210
("2011-12-30 00:00:00.000000", "%Y-%m-%d %H:%M:%S.%f"),
211-
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %H:%M:%S %p"),
212-
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %H:%M:%S %p"),
211+
("Tue 24 Aug 2021 01:30:48", "%a %d %b %Y %H:%M:%S"),
212+
("Tuesday 24 Aug 2021 01:30:48", "%A %d %b %Y %H:%M:%S"),
213+
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %I:%M:%S %p"),
214+
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %I:%M:%S %p"),
213215
("27.03.2003 14:55:00.000", "%d.%m.%Y %H:%M:%S.%f"), # GH50317
214216
],
215217
)

0 commit comments

Comments
 (0)