Skip to content

Commit 11d856f

Browse files
authored
BUG: catch decimal.InvalidOperation exception from dateutil (#51157)
* catch InvalidOperation exception from dateutil * add tests to verify decimal.InvalidOperation is caught
1 parent 7f2f465 commit 11d856f

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ Datetimelike
11361136
- Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with datetime or timedelta dtypes incorrectly raising ``ValueError`` (:issue:`11312`)
11371137
- Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`)
11381138
- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`51162`)
1139+
- Bug in :func:`to_datetime` was raising ``decimal.InvalidOperation`` when parsing date strings with ``errors='coerce'`` (:issue:`51084`)
11391140
-
11401141

11411142
Timedelta

pandas/_libs/tslibs/parsing.pyx

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ cnp.import_array()
4040

4141
# dateutil compat
4242

43+
from decimal import InvalidOperation
44+
4345
from dateutil.parser import (
4446
DEFAULTPARSER,
4547
parse as du_parse,
@@ -646,7 +648,10 @@ cdef datetime dateutil_parse(
646648
str reso = None
647649
dict repl = {}
648650

649-
res, _ = DEFAULTPARSER._parse(timestr, dayfirst=dayfirst, yearfirst=yearfirst)
651+
try:
652+
res, _ = DEFAULTPARSER._parse(timestr, dayfirst=dayfirst, yearfirst=yearfirst)
653+
except InvalidOperation:
654+
res = None
650655

651656
if res is None:
652657
raise DateParseError(
@@ -891,7 +896,7 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
891896

892897
try:
893898
parsed_datetime = du_parse(dt_str, dayfirst=dayfirst)
894-
except (ValueError, OverflowError):
899+
except (ValueError, OverflowError, InvalidOperation):
895900
# In case the datetime can't be parsed, its format cannot be guessed
896901
return None
897902

pandas/tests/tools/test_to_datetime.py

+10
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,16 @@ def test_to_datetime_overflow(self):
25012501
with pytest.raises(OutOfBoundsTimedelta, match=msg):
25022502
date_range(start="1/1/1700", freq="B", periods=100000)
25032503

2504+
def test_string_invalid_operation(self, cache):
2505+
invalid = np.array(["87156549591102612381000001219H5"], dtype=object)
2506+
# GH #51084
2507+
2508+
with pytest.raises(ValueError, match="Unknown datetime string format"):
2509+
with tm.assert_produces_warning(
2510+
UserWarning, match="Could not infer format"
2511+
):
2512+
to_datetime(invalid, errors="raise", cache=cache)
2513+
25042514
def test_string_na_nat_conversion(self, cache):
25052515
# GH #999, #858
25062516

pandas/tests/tslibs/test_parsing.py

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt):
246246
"13/2019",
247247
"202001", # YYYYMM isn't ISO8601
248248
"2020/01", # YYYY/MM isn't ISO8601 either
249+
"87156549591102612381000001219H5",
249250
],
250251
)
251252
def test_guess_datetime_format_invalid_inputs(invalid_dt):

0 commit comments

Comments
 (0)