Skip to content

ERR: Better error message #32808

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 1 commit 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
10 changes: 8 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,14 +1852,20 @@ 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]:
raise ValueError(
f"Unexpected value {err.args[1]}.\n"
"You can coerce to NaT by passing `errors='coerce'`"
) from err

raise err

if tz_parsed is not None:
# We can take a shortcut since the datetime64 numpy array
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"):
"""Convert string 'r' to a timedelta object."""
try:
result = Timedelta(r, unit)
except ValueError:
except ValueError as err:
if errors == "raise":
raise
raise ValueError(
f"Unexpected value {err.args[0]}.\n"
"You can coerce to NaT by passing `errors='coerce'`"
) from err
elif errors == "ignore":
return r

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,10 @@ def test_string_na_nat_conversion(self, cache):
malformed = np.array(["1/100/2000", np.nan], dtype=object)

# GH 10636, default is now 'raise'
msg = r"Unknown string format:|day is out of range for month"
msg = (
"Unexpected value 1/100/2000.\n"
"You can coerce to NaT by passing `errors='coerce'`"
)
with pytest.raises(ValueError, match=msg):
to_datetime(malformed, errors="raise", cache=cache)

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/tools/test_to_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def test_to_timedelta_invalid(self):
invalid_data, to_timedelta(invalid_data, errors="ignore")
)

msg = (
"Unexpected value unit abbreviation w/o a number.\n"
"You can coerce to NaT by passing `errors='coerce'`"
)
with pytest.raises(ValueError, match=msg):
pd.to_timedelta("foo", errors="raise")

def test_to_timedelta_via_apply(self):
# GH 5458
expected = Series([np.timedelta64(1, "s")])
Expand Down