Skip to content

Commit c12eeaa

Browse files
Catch deprecated patterns with a regex to prevent false positives and update tests
1 parent c7baff9 commit c12eeaa

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

pandas/core/tools/timedeltas.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
timedelta support tools
33
"""
4+
import re
45
import warnings
56

67
import numpy as np
@@ -127,7 +128,7 @@ def to_timedelta(arg, unit=None, errors="raise"):
127128
raise ValueError(
128129
"unit must not be specified if the input is/contains a str"
129130
)
130-
elif arg.upper().endswith("M") or arg.upper().endswith("Y"):
131+
elif re.search(r"^\d+\s?[M|Y|m|y]$", arg):
131132
warnings.warn(
132133
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous "
133134
"timedelta values durations and will removed in a future version",

pandas/tests/scalar/timedelta/test_timedelta.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ def test_unit_parser(self, units, np_unit, wrapper):
258258
if unit == "M":
259259
expected = Timedelta(np.timedelta64(2, "m").astype("timedelta64[ns]"))
260260

261-
result = to_timedelta(f"2{unit}")
261+
warning = None if unit != "m" else FutureWarning
262+
with tm.assert_produces_warning(warning):
263+
result = to_timedelta(f"2{unit}")
262264
assert result == expected
263265
result = Timedelta(f"2{unit}")
264266
assert result == expected

pandas/tests/tools/test_to_timedelta.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,22 @@ def test_to_timedelta_invalid(self):
122122
)
123123

124124
@pytest.mark.parametrize(
125-
"val",
125+
"val, warning",
126126
[
127-
"1M",
128-
"1 M",
129-
"1Y",
130-
"1 Y",
131-
"1m",
132-
"1 m",
133-
"1y",
134-
"1 y",
127+
("1M", FutureWarning),
128+
("1 M", FutureWarning),
129+
("1Y", FutureWarning),
130+
("1 Y", FutureWarning),
131+
("1m", FutureWarning),
132+
("1 m", FutureWarning),
133+
("1y", FutureWarning),
134+
("1 y", FutureWarning),
135+
("1 day", None),
136+
("2day", None),
135137
],
136138
)
137-
def test_unambiguous_timedelta_values(self, val):
138-
with tm.assert_produces_warning(FutureWarning):
139+
def test_unambiguous_timedelta_values(self, val, warning):
140+
with tm.assert_produces_warning(warning):
139141
to_timedelta(val)
140142

141143
def test_to_timedelta_via_apply(self):

0 commit comments

Comments
 (0)