Skip to content

Commit 4c00b86

Browse files
author
MarcoGorelli
committed
BUG: to_datetime raising on invalid offsets with errors=coerce and infer_datetime_format
1 parent e493323 commit 4c00b86

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v1.6.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Categorical
162162
Datetimelike
163163
^^^^^^^^^^^^
164164
- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`)
165+
- Bug in :func:`to_datetime` was raising on invalid offsets with ``errors='coerce'`` and ``infer_datetime_format=True`` (:issue:`48633`)
165166
-
166167

167168
Timedelta

pandas/_libs/tslibs/parsing.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ def format_is_iso(f: str) -> bint:
943943
return False
944944

945945

946-
def guess_datetime_format(dt_str, bint dayfirst=False):
946+
def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
947947
"""
948948
Guess the datetime format of a given datetime string.
949949

@@ -1026,7 +1026,12 @@ def guess_datetime_format(dt_str, bint dayfirst=False):
10261026
# This separation will prevent subsequent processing
10271027
# from correctly parsing the time zone format.
10281028
# So in addition to the format nomalization, we rejoin them here.
1029-
tokens[offset_index] = parsed_datetime.strftime("%z")
1029+
try:
1030+
tokens[offset_index] = parsed_datetime.strftime("%z")
1031+
except ValueError:
1032+
# Invalid offset might not have raised in du_parse
1033+
# https://github.com/dateutil/dateutil/issues/188
1034+
return None
10301035
tokens = tokens[:offset_index + 1 or None]
10311036

10321037
format_guess = [None] * len(tokens)

pandas/tests/tools/test_to_datetime.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,10 +1143,13 @@ def test_to_datetime_coerce(self):
11431143
)
11441144
tm.assert_index_equal(result, expected)
11451145

1146-
def test_to_datetime_coerce_malformed(self):
1146+
@pytest.mark.parametrize("infer_datetime_format", [True, False])
1147+
def test_to_datetime_coerce_malformed(self, infer_datetime_format):
11471148
# GH 28299
11481149
ts_strings = ["200622-12-31", "111111-24-11"]
1149-
result = to_datetime(ts_strings, errors="coerce")
1150+
result = to_datetime(
1151+
ts_strings, errors="coerce", infer_datetime_format=infer_datetime_format
1152+
)
11501153
expected = Index([NaT, NaT])
11511154
tm.assert_index_equal(result, expected)
11521155

0 commit comments

Comments
 (0)