Skip to content

DEV: Improves Error msg when constructing a Timedelta #54098

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 5 commits into from
Jul 17, 2023
Merged
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
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,10 @@ class Timedelta(_Timedelta):
NPY_DATETIMEUNIT.NPY_FR_W,
NPY_DATETIMEUNIT.NPY_FR_GENERIC]):
err = npy_unit_to_abbrev(reso)
raise ValueError(f" cannot construct a Timedelta from a unit {err}")
raise ValueError(
f"Unit {err} is not supported. "
"Only unambiguous timedelta values durations are supported. "
"Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'")

new_reso = get_supported_reso(reso)
if reso != NPY_DATETIMEUNIT.NPY_FR_GENERIC:
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/tslibs/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def test_delta_to_nanoseconds_td64_MY_raises():
def test_unsupported_td64_unit_raises(unit):
# GH 52806
with pytest.raises(
ValueError, match=f"cannot construct a Timedelta from a unit {unit}"
ValueError,
match=f"Unit {unit} is not supported. "
"Only unambiguous timedelta values durations are supported. "
"Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'",
):
Timedelta(np.timedelta64(1, unit))

Expand Down