Skip to content

Commit 76e6602

Browse files
Move warnings code to timedeltas.pyx and update tests
1 parent d491cce commit 76e6602

File tree

4 files changed

+20
-44
lines changed

4 files changed

+20
-44
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import re
3+
import warnings
34

45
import cython
56

@@ -298,6 +299,14 @@ cdef inline int64_t parse_timedelta_string(str ts) except? -1:
298299
if len(ts) == 0 or ts in nat_strings:
299300
return NPY_NAT
300301

302+
if re.search(r"^\d+\s?[M|Y|m|y]$", ts):
303+
warnings.warn(
304+
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous "
305+
"timedelta values durations and will be removed in a future version",
306+
FutureWarning,
307+
stacklevel=2,
308+
)
309+
301310
for c in ts:
302311

303312
# skip whitespace / commas
@@ -1468,16 +1477,6 @@ cdef _broadcast_floordiv_td64(
14681477
return res
14691478

14701479

1471-
def check_unambiguous_timedelta_values(object[:] values):
1472-
cdef:
1473-
Py_ssize_t i, n = len(values)
1474-
1475-
for i in range(n):
1476-
if re.search(r"^\d+\s?[M|Y|m|y]$", str(values[i])):
1477-
return True
1478-
return False
1479-
1480-
14811480
# resolution in ns
14821481
Timedelta.min = Timedelta(np.iinfo(np.int64).min + 1)
14831482
Timedelta.max = Timedelta(np.iinfo(np.int64).max)

pandas/core/tools/timedeltas.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
"""
22
timedelta support tools
33
"""
4-
import re
5-
import warnings
64

75
import numpy as np
86

97
from pandas._libs.tslibs import NaT
10-
from pandas._libs.tslibs.timedeltas import (
11-
Timedelta,
12-
check_unambiguous_timedelta_values,
13-
parse_timedelta_unit,
14-
)
8+
from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit
159

1610
from pandas.core.dtypes.common import is_list_like
1711
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
@@ -110,16 +104,6 @@ def to_timedelta(arg, unit=None, errors="raise"):
110104
"represent unambiguous timedelta values durations."
111105
)
112106

113-
if isinstance(arg, (list, tuple, ABCSeries, ABCIndexClass, np.ndarray)):
114-
arr = np.array(arg, dtype="object", ndmin=1)
115-
if arr.ndim == 1 and check_unambiguous_timedelta_values(arr):
116-
warnings.warn(
117-
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous "
118-
"timedelta values durations and will removed in a future version",
119-
FutureWarning,
120-
stacklevel=2,
121-
)
122-
123107
if arg is None:
124108
return arg
125109
elif isinstance(arg, ABCSeries):
@@ -137,18 +121,8 @@ def to_timedelta(arg, unit=None, errors="raise"):
137121
"arg must be a string, timedelta, list, tuple, 1-d array, or Series"
138122
)
139123

140-
if isinstance(arg, str):
141-
if unit is not None:
142-
raise ValueError(
143-
"unit must not be specified if the input is/contains a str"
144-
)
145-
elif re.search(r"^\d+\s?[M|Y|m|y]$", arg):
146-
warnings.warn(
147-
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous "
148-
"timedelta values durations and will removed in a future version",
149-
FutureWarning,
150-
stacklevel=2,
151-
)
124+
if isinstance(arg, str) and unit is not None:
125+
raise ValueError("unit must not be specified if the input is/contains a str")
152126

153127
# ...so it must be a scalar value. Return scalar.
154128
return _coerce_scalar_to_timedelta_type(arg, unit=unit, errors=errors)

pandas/tests/scalar/timedelta/test_timedelta.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,12 @@ def test_unit_parser(self, unit, np_unit, wrapper, warning):
255255
tm.assert_index_equal(result, expected)
256256

257257
str_repr = [f"{x}{unit}" for x in np.arange(5)]
258-
with tm.assert_produces_warning(warning):
258+
with tm.assert_produces_warning(warning, check_stacklevel=False):
259259
result = to_timedelta(wrapper(str_repr))
260260
tm.assert_index_equal(result, expected)
261-
result = TimedeltaIndex(wrapper(str_repr))
261+
with tm.assert_produces_warning(warning, check_stacklevel=False):
262+
result = to_timedelta(wrapper(str_repr))
263+
262264
tm.assert_index_equal(result, expected)
263265

264266
# scalar
@@ -268,10 +270,11 @@ def test_unit_parser(self, unit, np_unit, wrapper, warning):
268270
result = Timedelta(2, unit=unit)
269271
assert result == expected
270272

271-
with tm.assert_produces_warning(warning):
273+
with tm.assert_produces_warning(warning, check_stacklevel=False):
272274
result = to_timedelta(f"2{unit}")
273275
assert result == expected
274-
result = Timedelta(f"2{unit}")
276+
with tm.assert_produces_warning(warning, check_stacklevel=False):
277+
result = Timedelta(f"2{unit}")
275278
assert result == expected
276279

277280
@pytest.mark.parametrize("unit", ["Y", "y", "M"])

pandas/tests/tools/test_to_timedelta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_to_timedelta_invalid(self):
139139
def test_unambiguous_timedelta_values(self, val, warning):
140140
# GH36666 Deprecate use of strings denoting units with 'M', 'Y', 'm' or 'y'
141141
# in pd.to_timedelta
142-
with tm.assert_produces_warning(warning):
142+
with tm.assert_produces_warning(warning, check_stacklevel=False):
143143
to_timedelta(val)
144144

145145
def test_to_timedelta_via_apply(self):

0 commit comments

Comments
 (0)