Skip to content

Commit 9f240a2

Browse files
authored
DEV: Improves Error msg when constructing a Timedelta (#54098)
* fix message * lang * v2 lang and docs * drop doc update
1 parent b3e5945 commit 9f240a2

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,10 @@ class Timedelta(_Timedelta):
18431843
NPY_DATETIMEUNIT.NPY_FR_W,
18441844
NPY_DATETIMEUNIT.NPY_FR_GENERIC]):
18451845
err = npy_unit_to_abbrev(reso)
1846-
raise ValueError(f" cannot construct a Timedelta from a unit {err}")
1846+
raise ValueError(
1847+
f"Unit {err} is not supported. "
1848+
"Only unambiguous timedelta values durations are supported. "
1849+
"Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'")
18471850

18481851
new_reso = get_supported_reso(reso)
18491852
if reso != NPY_DATETIMEUNIT.NPY_FR_GENERIC:

pandas/tests/tslibs/test_timedeltas.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ def test_delta_to_nanoseconds_td64_MY_raises():
7676
def test_unsupported_td64_unit_raises(unit):
7777
# GH 52806
7878
with pytest.raises(
79-
ValueError, match=f"cannot construct a Timedelta from a unit {unit}"
79+
ValueError,
80+
match=f"Unit {unit} is not supported. "
81+
"Only unambiguous timedelta values durations are supported. "
82+
"Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'",
8083
):
8184
Timedelta(np.timedelta64(1, unit))
8285

0 commit comments

Comments
 (0)