Skip to content

Commit

Permalink
Catch deprecated patterns with a regex to prevent false positives and…
Browse files Browse the repository at this point in the history
… update tests
  • Loading branch information
avinashpancham committed Oct 4, 2020
1 parent c7baff9 commit c12eeaa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
timedelta support tools
"""
import re
import warnings

import numpy as np
Expand Down Expand Up @@ -127,7 +128,7 @@ def to_timedelta(arg, unit=None, errors="raise"):
raise ValueError(
"unit must not be specified if the input is/contains a str"
)
elif arg.upper().endswith("M") or arg.upper().endswith("Y"):
elif re.search(r"^\d+\s?[M|Y|m|y]$", arg):
warnings.warn(
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous "
"timedelta values durations and will removed in a future version",
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ def test_unit_parser(self, units, np_unit, wrapper):
if unit == "M":
expected = Timedelta(np.timedelta64(2, "m").astype("timedelta64[ns]"))

result = to_timedelta(f"2{unit}")
warning = None if unit != "m" else FutureWarning
with tm.assert_produces_warning(warning):
result = to_timedelta(f"2{unit}")
assert result == expected
result = Timedelta(f"2{unit}")
assert result == expected
Expand Down
24 changes: 13 additions & 11 deletions pandas/tests/tools/test_to_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,22 @@ def test_to_timedelta_invalid(self):
)

@pytest.mark.parametrize(
"val",
"val, warning",
[
"1M",
"1 M",
"1Y",
"1 Y",
"1m",
"1 m",
"1y",
"1 y",
("1M", FutureWarning),
("1 M", FutureWarning),
("1Y", FutureWarning),
("1 Y", FutureWarning),
("1m", FutureWarning),
("1 m", FutureWarning),
("1y", FutureWarning),
("1 y", FutureWarning),
("1 day", None),
("2day", None),
],
)
def test_unambiguous_timedelta_values(self, val):
with tm.assert_produces_warning(FutureWarning):
def test_unambiguous_timedelta_values(self, val, warning):
with tm.assert_produces_warning(warning):
to_timedelta(val)

def test_to_timedelta_via_apply(self):
Expand Down

0 comments on commit c12eeaa

Please sign in to comment.