Skip to content

Commit cbfe215

Browse files
DEPR: deprecate unit parameters ’T’, 't', 'L', and 'l' (#53557)
* BUG: add ’T’ to UnitChoices to fix incorrect unit validation in to_timedelta() * deprecate units T, t, L, l and fix tests * raise FutureWarning instead of ValueError * correct the to_timedelta definition and add a test for the warning --------- Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent 047da25 commit cbfe215

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ Deprecations
295295
- Deprecated option "mode.use_inf_as_na", convert inf entries to ``NaN`` before instead (:issue:`51684`)
296296
- Deprecated parameter ``obj`` in :meth:`GroupBy.get_group` (:issue:`53545`)
297297
- Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`)
298+
- Deprecated strings ``T``, ``t``, ``L`` and ``l`` denoting units in :func:`to_timedelta` (:issue:`52536`)
298299
- Deprecated the "method" and "limit" keywords on :meth:`Series.fillna`, :meth:`DataFrame.fillna`, :meth:`SeriesGroupBy.fillna`, :meth:`DataFrameGroupBy.fillna`, and :meth:`Resampler.fillna`, use ``obj.bfill()`` or ``obj.ffill()`` instead (:issue:`53394`)
299300
- Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`)
300301
- Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
301-
-
302302

303303
.. ---------------------------------------------------------------------------
304304
.. _whatsnew_210.performance:

pandas/_libs/tslibs/timedeltas.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ UnitChoices = Literal[
3737
"minute",
3838
"min",
3939
"minutes",
40+
"T",
4041
"t",
4142
"s",
4243
"seconds",
@@ -47,6 +48,7 @@ UnitChoices = Literal[
4748
"millisecond",
4849
"milli",
4950
"millis",
51+
"L",
5052
"l",
5153
"us",
5254
"microseconds",

pandas/core/tools/timedeltas.py

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
TYPE_CHECKING,
88
overload,
99
)
10+
import warnings
1011

1112
import numpy as np
1213

@@ -19,6 +20,7 @@
1920
Timedelta,
2021
parse_timedelta_unit,
2122
)
23+
from pandas.util._exceptions import find_stack_level
2224

2325
from pandas.core.dtypes.common import is_list_like
2426
from pandas.core.dtypes.generic import (
@@ -118,6 +120,9 @@ def to_timedelta(
118120
119121
Must not be specified when `arg` context strings and ``errors="raise"``.
120122
123+
.. deprecated:: 2.1.0
124+
Units 'T' and 'L' are deprecated and will be removed in a future version.
125+
121126
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
122127
- If 'raise', then invalid parsing will raise an exception.
123128
- If 'coerce', then invalid parsing will be set as NaT.
@@ -169,6 +174,13 @@ def to_timedelta(
169174
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
170175
dtype='timedelta64[ns]', freq=None)
171176
"""
177+
if unit in {"T", "t", "L", "l"}:
178+
warnings.warn(
179+
f"Unit '{unit}' is deprecated and will be removed in a future version.",
180+
FutureWarning,
181+
stacklevel=find_stack_level(),
182+
)
183+
172184
if unit is not None:
173185
unit = parse_timedelta_unit(unit)
174186

pandas/tests/indexes/timedeltas/test_timedelta_range.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,27 @@ def test_timedelta_range(self):
3838
result = timedelta_range("1 days, 00:00:02", periods=5, freq="2D")
3939
tm.assert_index_equal(result, expected)
4040

41-
expected = to_timedelta(np.arange(50), unit="T") * 30
42-
result = timedelta_range("0 days", freq="30T", periods=50)
41+
expected = to_timedelta(np.arange(50), unit="min") * 30
42+
result = timedelta_range("0 days", freq="30min", periods=50)
4343
tm.assert_index_equal(result, expected)
4444

45+
@pytest.mark.parametrize(
46+
"depr_unit, unit",
47+
[
48+
("T", "minute"),
49+
("t", "minute"),
50+
("L", "millisecond"),
51+
("l", "millisecond"),
52+
],
53+
)
54+
def test_timedelta_units_T_L_deprecated(self, depr_unit, unit):
55+
depr_msg = f"Unit '{depr_unit}' is deprecated."
56+
57+
expected = to_timedelta(np.arange(5), unit=unit)
58+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
59+
result = to_timedelta(np.arange(5), unit=depr_unit)
60+
tm.assert_index_equal(result, expected)
61+
4562
@pytest.mark.parametrize(
4663
"periods, freq", [(3, "2D"), (5, "D"), (6, "19H12T"), (7, "16H"), (9, "12H")]
4764
)

pandas/tests/resample/test_timedelta.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ def test_resample_as_freq_with_subperiod():
4848
def test_resample_with_timedeltas():
4949
expected = DataFrame({"A": np.arange(1480)})
5050
expected = expected.groupby(expected.index // 30).sum()
51-
expected.index = timedelta_range("0 days", freq="30T", periods=50)
51+
expected.index = timedelta_range("0 days", freq="30min", periods=50)
5252

5353
df = DataFrame(
54-
{"A": np.arange(1480)}, index=pd.to_timedelta(np.arange(1480), unit="T")
54+
{"A": np.arange(1480)}, index=pd.to_timedelta(np.arange(1480), unit="min")
5555
)
56-
result = df.resample("30T").sum()
56+
result = df.resample("30min").sum()
5757

5858
tm.assert_frame_equal(result, expected)
5959

6060
s = df["A"]
61-
result = s.resample("30T").sum()
61+
result = s.resample("30min").sum()
6262
tm.assert_series_equal(result, expected["A"])
6363

6464

pandas/tests/scalar/timedelta/test_timedelta.py

-4
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,9 @@ def test_nat_converters(self):
492492
"minute",
493493
"min",
494494
"minutes",
495-
"t",
496495
"Minute",
497496
"Min",
498497
"Minutes",
499-
"T",
500498
]
501499
]
502500
+ [
@@ -520,13 +518,11 @@ def test_nat_converters(self):
520518
"millisecond",
521519
"milli",
522520
"millis",
523-
"l",
524521
"MS",
525522
"Milliseconds",
526523
"Millisecond",
527524
"Milli",
528525
"Millis",
529-
"L",
530526
]
531527
]
532528
+ [

0 commit comments

Comments
 (0)