Skip to content

Commit 9042122

Browse files
committed
correct the to_timedelta definition and add a test for the warning
1 parent eb127a4 commit 9042122

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

pandas/_libs/tslibs/timedeltas.pyx

-6
Original file line numberDiff line numberDiff line change
@@ -1745,12 +1745,6 @@ class Timedelta(_Timedelta):
17451745
"Units 'M', 'Y', and 'y' are no longer supported, as they do not "
17461746
"represent unambiguous timedelta values durations."
17471747
)
1748-
if unit in {"T", "t", "L", "l"}:
1749-
warnings.warn(
1750-
"Units 'T' and'L' are deprecated and will be removed in a future version.",
1751-
FutureWarning,
1752-
stacklevel=2,
1753-
)
17541748

17551749
# GH 30543 if pd.Timedelta already passed, return it
17561750
# check that only value is passed

pandas/core/tools/timedeltas.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ def to_timedelta(
174174
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
175175
dtype='timedelta64[ns]', freq=None)
176176
"""
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+
177184
if unit is not None:
178185
unit = parse_timedelta_unit(unit)
179186

@@ -186,13 +193,6 @@ def to_timedelta(
186193
"represent unambiguous timedelta values durations."
187194
)
188195

189-
if unit in {"T", "t", "L", "l"}:
190-
warnings.warn(
191-
"Units 'T' and'L' are deprecated and will be removed in a future version.",
192-
FutureWarning,
193-
stacklevel=find_stack_level(),
194-
)
195-
196196
if arg is None:
197197
return arg
198198
elif isinstance(arg, ABCSeries):

pandas/tests/indexes/timedeltas/test_timedelta_range.py

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ def test_timedelta_range(self):
4242
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 = (
56+
f"Unit '{depr_unit}' is deprecated."
57+
)
58+
59+
expected = to_timedelta(np.arange(5), unit=unit)
60+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
61+
result = to_timedelta(np.arange(5), unit=depr_unit)
62+
tm.assert_index_equal(result, expected)
63+
4564
@pytest.mark.parametrize(
4665
"periods, freq", [(3, "2D"), (5, "D"), (6, "19H12T"), (7, "16H"), (9, "12H")]
4766
)

0 commit comments

Comments
 (0)