Skip to content

ERR: better error message on unparseable datetimes #31118

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,14 +1848,18 @@ def objects_to_datetime64ns(
yearfirst=yearfirst,
require_iso8601=require_iso8601,
)
except ValueError as e:
except ValueError as err:
try:
values, tz_parsed = conversion.datetime_to_datetime64(data)
# If tzaware, these values represent unix timestamps, so we
# return them as i8 to distinguish from wall times
return values.view("i8"), tz_parsed
except (ValueError, TypeError):
raise e
if "Unknown string format" in err.args[0]:
msg = " ".join(err.args)
msg = f"{msg}. You can coerce to NaT by passing errors='coerce'"
raise ValueError(msg) from err
raise err

if tz_parsed is not None:
# We can take a shortcut since the datetime64 numpy array
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"):

try:
result = Timedelta(r, unit)
except ValueError:
except ValueError as err:
if errors == "raise":
raise
# GH#10720
msg = f"{err.args[0]}. You can coerce to NaT by passing errors='coerce'"
raise ValueError(msg) from err
elif errors == "ignore":
return r

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ def test_to_datetime_unparseable_ignore(self):
s = "Month 1, 1999"
assert pd.to_datetime(s, errors="ignore") == s

def test_to_datetime_unparseable_raise(self):
# GH#10720
msg = "Month 1, 1999"
expected_args = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this msg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

f"Unknown string format: %s {msg}. "
"You can coerce to NaT by passing errors='coerce'"
)

with pytest.raises(ValueError, match=expected_args):
pd.to_datetime(msg, errors="raise")

@td.skip_if_windows # `tm.set_timezone` does not work in windows
def test_to_datetime_now(self):
# See GH#18666
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/timedeltas/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ def test_to_timedelta_invalid(self):
to_timedelta(["1 day", "bar", "1 min"], errors="coerce"),
)

# GH#10720
invalid_data = "some_nonesense"
expected_msg = (
"unit abbreviation w/o a number. "
"You can coerce to NaT by passing errors='coerce'"
)

with pytest.raises(ValueError, match=expected_msg):
pd.to_timedelta(invalid_data, errors="raise")

# gh-13613: these should not error because errors='ignore'
invalid_data = "apple"
assert invalid_data == to_timedelta(invalid_data, errors="ignore")
Expand Down