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 3 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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Other enhancements
- Improve error message when setting :class:`DataFrame` with wrong number of columns through :meth:`DataFrame.isetitem` (:issue:`51701`)
- Improved error handling when using :meth:`DataFrame.to_json` with incompatible ``index`` and ``orient`` arguments (:issue:`52143`)
- Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue:`52084`)
- Improved error message when creating a Timedelta from a ambiguous time period (:issue:`54059`)
- Let :meth:`DataFrame.to_feather` accept a non-default :class:`Index` and non-string column names (:issue:`51787`)
- Performance improvement in :func:`read_csv` (:issue:`52632`) with ``engine="c"``
- :meth:`Categorical.from_codes` has gotten a ``validate`` parameter (:issue:`50975`)
Expand Down
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