diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index cd6977f43d322..133e7149d4546 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -182,6 +182,7 @@ Other Deprecations - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) +- Deprecated strings ``MIN``, ``MS``, ``US``, and ``NS`` denoting units in :class:`Timedelta` and :func:`to_timedelta` (:issue:`57700`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index c0d1b2e79f587..8f1a845a268ed 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -401,6 +401,9 @@ cdef dict c_DEPR_ABBREVS = { "u": "us", "N": "ns", "n": "ns", + "MIN": "min", + "US": "us", + "NS": "ns", } diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 18ead7d5381df..befab1dfaddf0 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -719,6 +719,14 @@ cpdef inline str parse_timedelta_unit(str unit): return "ns" elif unit == "M": return unit + elif unit == "MS": + warnings.warn( + f"\'{unit}\' is deprecated and will be removed in a future version. " + f"Please use \'ms\' instead of \'{unit}\'.", + FutureWarning, + stacklevel=find_stack_level(), + ) + unit = "ms" elif unit in c_DEPR_ABBREVS: warnings.warn( f"\'{unit}\' is deprecated and will be removed in a " @@ -1801,6 +1809,11 @@ class Timedelta(_Timedelta): Values `H`, `T`, `S`, `L`, `U`, and `N` are deprecated in favour of the values `h`, `min`, `s`, `ms`, `us`, and `ns`. + .. deprecated:: 3.0.0 + + Units `MIN`, `MS`, `US`, and `NS` are deprecated in favour + of the units `min`, `ms`, `us`, and `ns`. + **kwargs Available kwargs: {days, seconds, microseconds, milliseconds, minutes, hours, weeks}. diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index a88bd4c42edec..d48592d1a61cb 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -20,7 +20,6 @@ Union, cast, ) -import warnings import numpy as np @@ -32,7 +31,6 @@ Substitution, doc, ) -from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( ensure_int64, diff --git a/pandas/tests/indexes/timedeltas/test_timedelta_range.py b/pandas/tests/indexes/timedeltas/test_timedelta_range.py index f22bdb7a90516..6638c047154c8 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta_range.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta_range.py @@ -171,3 +171,23 @@ def test_timedelta_range_deprecated_freq( expected_values, dtype="timedelta64[ns]", freq=expected_freq ) tm.assert_index_equal(result, expected) + + @pytest.mark.parametrize( + "depr_unit, unit", + [ + ("MIN", "min"), + ("MS", "ms"), + ("US", "us"), + ("NS", "ns"), + ], + ) + def test_timedelta_units_MIN_MS_US_NS_deprecated(self, depr_unit, unit): + # GH#52536 + depr_msg = ( + f"'{depr_unit}' is deprecated and will be removed in a future version." + ) + + expected = to_timedelta(np.arange(5), unit=unit) + with tm.assert_produces_warning(FutureWarning, match=depr_msg): + result = to_timedelta(np.arange(5), unit=depr_unit) + tm.assert_index_equal(result, expected) diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index e680ca737b546..b383630aeb512 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -170,6 +170,24 @@ def test_unit_parser(self, unit, np_unit, wrapper): result = Timedelta(f"2{unit}") assert result == expected + @pytest.mark.parametrize( + "unit,unit_depr", + [ + ("min", "MIN"), + ("ms", "MS"), + ("us", "US"), + ("ns", "NS"), + ], + ) + def test_units_MIN_MS_US_NS_deprecated(self, unit, unit_depr): + # GH#52536 + msg = f"'{unit_depr}' is deprecated and will be removed in a future version." + + expected = Timedelta(1, unit=unit) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = Timedelta(1, unit=unit_depr) + tm.assert_equal(result, expected) + def test_construct_from_kwargs_overflow(): # GH#55503 diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index d4398f66e6f89..72747879de4a3 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -461,7 +461,6 @@ def conv(v): assert Timedelta("1000") == np.timedelta64(1000, "ns") assert Timedelta("1000ns") == np.timedelta64(1000, "ns") - assert Timedelta("1000NS") == np.timedelta64(1000, "ns") assert Timedelta("10us") == np.timedelta64(10000, "ns") assert Timedelta("100us") == np.timedelta64(100000, "ns")