-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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 11 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,11 +1,17 @@ | ||
""" | ||
timedelta support tools | ||
""" | ||
import re | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
from pandas._libs.tslibs import NaT | ||
from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit | ||
from pandas._libs.tslibs.timedeltas import ( | ||
Timedelta, | ||
check_unambiguous_timedelta_values, | ||
parse_timedelta_unit, | ||
) | ||
|
||
from pandas.core.dtypes.common import is_list_like | ||
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries | ||
|
@@ -25,10 +31,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"``. | ||
avinashpancham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
@@ -101,6 +110,16 @@ def to_timedelta(arg, unit=None, errors="raise"): | |
"represent unambiguous timedelta values durations." | ||
) | ||
|
||
if isinstance(arg, (list, tuple, ABCSeries, ABCIndexClass, np.ndarray)): | ||
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. the warning shouldn't be here |
||
arr = np.array(arg, dtype="object", ndmin=1) | ||
if arr.ndim == 1 and check_unambiguous_timedelta_values(arr): | ||
warnings.warn( | ||
"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, | ||
) | ||
|
||
if arg is None: | ||
return arg | ||
elif isinstance(arg, ABCSeries): | ||
|
@@ -118,8 +137,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 |
---|---|---|
|
@@ -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")]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than this you can do it in the Timedelta string converter