Skip to content

BUG: catch decimal.InvalidOperation exception from dateutil #51157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ Datetimelike
- Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with datetime or timedelta dtypes incorrectly raising ``ValueError`` (:issue:`11312`)
- Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`)
- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`51162`)
- Bug in :func:`to_datetime` was raising ``decimal.InvalidOperation`` when parsing date strings with ``errors='coerce'`` (:issue:`51084`)
-

Timedelta
Expand Down
9 changes: 7 additions & 2 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ cnp.import_array()

# dateutil compat

from decimal import InvalidOperation

from dateutil.parser import (
DEFAULTPARSER,
parse as du_parse,
Expand Down Expand Up @@ -646,7 +648,10 @@ cdef datetime dateutil_parse(
str reso = None
dict repl = {}

res, _ = DEFAULTPARSER._parse(timestr, dayfirst=dayfirst, yearfirst=yearfirst)
try:
res, _ = DEFAULTPARSER._parse(timestr, dayfirst=dayfirst, yearfirst=yearfirst)
except InvalidOperation:
res = None

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

try:
parsed_datetime = du_parse(dt_str, dayfirst=dayfirst)
except (ValueError, OverflowError):
except (ValueError, OverflowError, InvalidOperation):
# In case the datetime can't be parsed, its format cannot be guessed
return None

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt):
"13/2019",
"202001", # YYYYMM isn't ISO8601
"2020/01", # YYYY/MM isn't ISO8601 either
"87156549591102612381000001219H5",
],
)
def test_guess_datetime_format_invalid_inputs(invalid_dt):
Expand Down