From 96f0b9409045aef8c06ee040c2ec6bacab07eb70 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Fri, 1 Mar 2024 22:47:03 +0100 Subject: [PATCH 1/5] correct def parse_timedelta_unit, add tests, fix test --- pandas/_libs/tslibs/timedeltas.pyx | 8 ++++++++ .../timedeltas/test_timedelta_range.py | 20 +++++++++++++++++++ .../scalar/timedelta/test_constructors.py | 18 +++++++++++++++++ .../tests/scalar/timedelta/test_timedelta.py | 1 - 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 18ead7d5381df..a853c72b111ea 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 in {"MIN", "MS", "US", "NS"}: + warnings.warn( + f"\'{unit}\' is deprecated and will be removed in a future version. " + f"Please use \'{unit.lower()}\' instead of \'{unit}\'.", + FutureWarning, + stacklevel=find_stack_level(), + ) + unit = unit.lower() elif unit in c_DEPR_ABBREVS: warnings.warn( f"\'{unit}\' is deprecated and will be removed in a " 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") From 34e216953c66d4340344da81ed278e8f049a684f Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sat, 2 Mar 2024 22:42:31 +0100 Subject: [PATCH 2/5] add deprecated units to c_DEPR_ABBREVS --- pandas/_libs/tslibs/dtypes.pyx | 4 ++++ pandas/_libs/tslibs/timedeltas.pyx | 8 -------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index c0d1b2e79f587..baf83cb901f42 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -401,6 +401,10 @@ cdef dict c_DEPR_ABBREVS = { "u": "us", "N": "ns", "n": "ns", + "MIN":"min", + "MS": "ms", + "US": "us", + "NS": "ns", } diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index a853c72b111ea..18ead7d5381df 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -719,14 +719,6 @@ cpdef inline str parse_timedelta_unit(str unit): return "ns" elif unit == "M": return unit - elif unit in {"MIN", "MS", "US", "NS"}: - warnings.warn( - f"\'{unit}\' is deprecated and will be removed in a future version. " - f"Please use \'{unit.lower()}\' instead of \'{unit}\'.", - FutureWarning, - stacklevel=find_stack_level(), - ) - unit = unit.lower() elif unit in c_DEPR_ABBREVS: warnings.warn( f"\'{unit}\' is deprecated and will be removed in a " From c42376b1782fe7581e1fd13506d1fd900a366c63 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sat, 2 Mar 2024 22:58:33 +0100 Subject: [PATCH 3/5] fix cython-lint error --- pandas/_libs/tslibs/dtypes.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index baf83cb901f42..11353baf37fd8 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -401,7 +401,7 @@ cdef dict c_DEPR_ABBREVS = { "u": "us", "N": "ns", "n": "ns", - "MIN":"min", + "MIN": "min", "MS": "ms", "US": "us", "NS": "ns", From b16c72fa0107b0fedc27a9ded631669f87fdcf31 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sat, 2 Mar 2024 23:25:53 +0100 Subject: [PATCH 4/5] remove 'MS' from c_DEPR_ABBREVS --- pandas/_libs/tslibs/dtypes.pyx | 1 - pandas/_libs/tslibs/timedeltas.pyx | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index 11353baf37fd8..8f1a845a268ed 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -402,7 +402,6 @@ cdef dict c_DEPR_ABBREVS = { "N": "ns", "n": "ns", "MIN": "min", - "MS": "ms", "US": "us", "NS": "ns", } diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 18ead7d5381df..ea68b0b56dec7 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 " From a53a00dea3b23bbfa2b7282f01093b237585d953 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 7 Mar 2024 15:07:30 +0100 Subject: [PATCH 5/5] add a note to v3.0.0, update Timedelta docstring --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/_libs/tslibs/timedeltas.pyx | 5 +++++ pandas/core/groupby/generic.py | 2 -- 3 files changed, 6 insertions(+), 2 deletions(-) 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/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index ea68b0b56dec7..befab1dfaddf0 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1809,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,