-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DEPR: Deprecate use of strings denoting units with 'M', 'Y' or 'y' in pd.to_timedelta (36666) #36838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DEPR: Deprecate use of strings denoting units with 'M', 'Y' or 'y' in pd.to_timedelta (36666) #36838
Changes from 9 commits
de4fb4d
61ec5b6
abb613e
4ccf43d
271f2fc
7b11a92
b39eadf
f56796a
ad38ad3
bc03065
d491cce
76e6602
cdc5821
5c471b3
02d642b
89af5bb
a5ef0c3
0bc1a89
3682687
2abd0ed
6f4e7ca
8701f26
783e039
0cc6dff
6a02d05
71f801e
e183f9b
2d8c9fe
1d2871b
e01f75d
5277943
3b83aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
""" | ||
timedelta support tools | ||
""" | ||
import re | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
|
@@ -25,10 +27,13 @@ def to_timedelta(arg, unit=None, errors="raise"): | |
Parameters | ||
---------- | ||
arg : str, timedelta, list-like or Series | ||
The data to be converted to timedelta. The character M by itself, | ||
e.g. '1M', is treated as minute, not month. The characters Y and y | ||
are treated as the mean length of the Gregorian calendar year - | ||
365.2425 days or 365 days 5 hours 49 minutes 12 seconds. | ||
The data to be converted to timedelta. | ||
|
||
.. deprecated:: 1.2 | ||
Strings denoting units with 'M', 'Y', 'm' or 'y' do not represent | ||
unambiguous timedelta values durations and will removed in a future | ||
version | ||
|
||
unit : str, optional | ||
Denotes the unit of the arg for numeric `arg`. Defaults to ``"ns"``. | ||
|
||
|
@@ -118,8 +123,18 @@ def to_timedelta(arg, unit=None, errors="raise"): | |
"arg must be a string, timedelta, list, tuple, 1-d array, or Series" | ||
) | ||
|
||
if isinstance(arg, str) and unit is not None: | ||
raise ValueError("unit must not be specified if the input is/contains a str") | ||
if isinstance(arg, str): | ||
if unit is not None: | ||
raise ValueError( | ||
"unit must not be specified if the input is/contains a str" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm we already have this check on L103, so something must be going wrong here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you do: |
||
elif re.search(r"^\d+\s?[M|Y|m|y]$", arg): | ||
warnings.warn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wouldn't do this here at all |
||
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous " | ||
"timedelta values durations and will removed in a future version", | ||
FutureWarning, | ||
stacklevel=2, | ||
) | ||
|
||
# ...so it must be a scalar value. Return scalar. | ||
return _coerce_scalar_to_timedelta_type(arg, unit=unit, errors=errors) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change this to parameterize this (e.g. elminate the for loop and parameterize on units) then add a parameter whether should test for FutureWarning or not (could be bool is ok). then in the body you can easily check this for both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have approx 50 values that have no warning and just one ("m") that has a warning. IMO a parameter for this would be overkill. But if we want this then I can change it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the test. IMO |
||
with tm.assert_produces_warning(warning): | ||
result = to_timedelta(f"2{unit}") | ||
assert result == expected | ||
result = Timedelta(f"2{unit}") | ||
assert result == expected | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,27 @@ def test_to_timedelta_invalid(self): | |
invalid_data, to_timedelta(invalid_data, errors="ignore") | ||
) | ||
|
||
@pytest.mark.parametrize( | ||
"val, warning", | ||
[ | ||
("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, warning): | ||
# GH36666 Deprecate use of strings denoting units with 'M', 'Y', 'm' or 'y' | ||
# in pd.to_timedelta | ||
with tm.assert_produces_warning(warning): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you but the github issue number here as a comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. CI fails due to other tests on master though. Will merge master later once that is solved. |
||
to_timedelta(val) | ||
|
||
def test_to_timedelta_via_apply(self): | ||
# GH 5458 | ||
expected = Series([np.timedelta64(1, "s")]) | ||
|
Uh oh!
There was an error while loading. Please reload this page.