From 42ff7ec49bc004a2986cd1fbfb60b04076430f29 Mon Sep 17 00:00:00 2001 From: Conrad Date: Wed, 12 Jul 2023 17:07:31 -0400 Subject: [PATCH 1/4] fix message --- pandas/_libs/tslibs/timedeltas.pyx | 5 ++++- pandas/tests/tslibs/test_timedeltas.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 28aeb854638b6..167224823b819 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -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( + "Units 'M', 'Y', and 'y' are no longer supported, as they do not " + "represent unambiguous timedelta values durations." + "Allowed unites are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'") new_reso = get_supported_reso(reso) if reso != NPY_DATETIMEUNIT.NPY_FR_GENERIC: diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index 2308aa27b60ab..e3e47f65a5135 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -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="Units 'M', 'Y', and 'y' are no longer supported, as they do not " + "represent unambiguous timedelta values durations." + "Allowed unites are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'", ): Timedelta(np.timedelta64(1, unit)) From e986a767c11f266ae1b5bffb5ef8d34d15e667ee Mon Sep 17 00:00:00 2001 From: Con Date: Thu, 13 Jul 2023 09:23:19 -0400 Subject: [PATCH 2/4] lang --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/tests/tslibs/test_timedeltas.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 167224823b819..000cc1aebddad 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1846,7 +1846,7 @@ class Timedelta(_Timedelta): raise ValueError( "Units 'M', 'Y', and 'y' are no longer supported, as they do not " "represent unambiguous timedelta values durations." - "Allowed unites are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'") + "Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'") new_reso = get_supported_reso(reso) if reso != NPY_DATETIMEUNIT.NPY_FR_GENERIC: diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index e3e47f65a5135..c91bf9b95c4b4 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -79,7 +79,7 @@ def test_unsupported_td64_unit_raises(unit): ValueError, match="Units 'M', 'Y', and 'y' are no longer supported, as they do not " "represent unambiguous timedelta values durations." - "Allowed unites are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'", + "Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'", ): Timedelta(np.timedelta64(1, unit)) From 3bf2e46efb52437f0134523e3a2ab7b9bc53f4c0 Mon Sep 17 00:00:00 2001 From: Con Date: Thu, 13 Jul 2023 14:24:47 -0400 Subject: [PATCH 3/4] v2 lang and docs --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/_libs/tslibs/timedeltas.pyx | 4 ++-- pandas/tests/tslibs/test_timedeltas.py | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7450fc6fdc1da..06effbdce4fc1 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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`) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 000cc1aebddad..ffa9a67542e21 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1844,8 +1844,8 @@ class Timedelta(_Timedelta): NPY_DATETIMEUNIT.NPY_FR_GENERIC]): err = npy_unit_to_abbrev(reso) raise ValueError( - "Units 'M', 'Y', and 'y' are no longer supported, as they do not " - "represent unambiguous timedelta values durations." + 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) diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index c91bf9b95c4b4..4784a6d0d600d 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -77,8 +77,8 @@ def test_unsupported_td64_unit_raises(unit): # GH 52806 with pytest.raises( ValueError, - match="Units 'M', 'Y', and 'y' are no longer supported, as they do not " - "represent unambiguous timedelta values durations." + 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)) From 3644a8b1d3a8aea077b110366c140b0d43cc9b35 Mon Sep 17 00:00:00 2001 From: Con Date: Thu, 13 Jul 2023 14:35:52 -0400 Subject: [PATCH 4/4] drop doc update --- doc/source/whatsnew/v2.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 06effbdce4fc1..7450fc6fdc1da 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -111,7 +111,6 @@ 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`)