Skip to content

Commit ea7230e

Browse files
ryankarlosPingviinituutti
authored andcommitted
DEPR: Add Deprecated warning for timedelta with passed units M and Y (pandas-dev#23264)
1 parent 89c5623 commit ea7230e

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ Other API Changes
4242
Deprecations
4343
~~~~~~~~~~~~
4444

45-
-
46-
-
47-
-
48-
45+
- Deprecated the `M (months)` and `Y (year)` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex` (:issue:`16344`)
4946

5047
.. _whatsnew_0250.prior_deprecations:
5148

pandas/_libs/tslibs/timedeltas.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,11 @@ class Timedelta(_Timedelta):
11581158
"[weeks, days, hours, minutes, seconds, "
11591159
"milliseconds, microseconds, nanoseconds]")
11601160

1161+
if unit in {'Y', 'y', 'M'}:
1162+
warnings.warn("M and Y units are deprecated and "
1163+
"will be removed in a future version.",
1164+
FutureWarning, stacklevel=1)
1165+
11611166
if isinstance(value, Timedelta):
11621167
value = value.value
11631168
elif is_string_object(value):

pandas/core/indexes/timedeltas.py

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None,
207207
'collection of some kind, {data} was passed'
208208
.format(cls=cls.__name__, data=repr(data)))
209209

210+
if unit in {'Y', 'y', 'M'}:
211+
warnings.warn("M and Y units are deprecated and "
212+
"will be removed in a future version.",
213+
FutureWarning, stacklevel=2)
214+
210215
if isinstance(data, TimedeltaArray):
211216
if copy:
212217
data = data.copy()

pandas/core/tools/timedeltas.py

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
timedelta support tools
33
"""
44

5+
import warnings
6+
57
import numpy as np
68

79
from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit
@@ -90,6 +92,11 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
9092
raise ValueError("errors must be one of 'ignore', "
9193
"'raise', or 'coerce'}")
9294

95+
if unit in {'Y', 'y', 'M'}:
96+
warnings.warn("M and Y units are deprecated and "
97+
"will be removed in a future version.",
98+
FutureWarning, stacklevel=2)
99+
93100
if arg is None:
94101
return arg
95102
elif isinstance(arg, ABCSeries):

pandas/tests/indexes/timedeltas/test_timedelta.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import timedelta
2+
import re
23

34
import numpy as np
45
import pytest
@@ -325,6 +326,13 @@ def test_freq_conversion(self):
325326
result = td.astype('timedelta64[s]')
326327
assert_index_equal(result, expected)
327328

329+
@pytest.mark.parametrize('unit', ['Y', 'y', 'M'])
330+
def test_unit_m_y_deprecated(self, unit):
331+
with tm.assert_produces_warning(FutureWarning) as w:
332+
TimedeltaIndex([1, 3, 7], unit)
333+
msg = r'.* units are deprecated .*'
334+
assert re.match(msg, str(w[0].message))
335+
328336

329337
class TestTimeSeries(object):
330338

pandas/tests/scalar/timedelta/test_timedelta.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" test the scalar Timedelta """
22
from datetime import timedelta
3+
import re
34

45
import numpy as np
56
import pytest
@@ -317,6 +318,7 @@ def test_nat_converters(self):
317318
assert result.dtype.kind == 'm'
318319
assert result.astype('int64') == iNaT
319320

321+
@pytest.mark.filterwarnings("ignore:M and Y units are deprecated")
320322
@pytest.mark.parametrize('units, np_unit',
321323
[(['Y', 'y'], 'Y'),
322324
(['M'], 'M'),
@@ -376,6 +378,24 @@ def test_unit_parser(self, units, np_unit, wrapper):
376378
result = Timedelta('2{}'.format(unit))
377379
assert result == expected
378380

381+
@pytest.mark.skipif(compat.PY2, reason="requires python3.5 or higher")
382+
@pytest.mark.parametrize('unit', ['Y', 'y', 'M'])
383+
def test_unit_m_y_deprecated(self, unit):
384+
with tm.assert_produces_warning(FutureWarning) as w1:
385+
Timedelta(10, unit)
386+
msg = r'.* units are deprecated .*'
387+
assert re.match(msg, str(w1[0].message))
388+
with tm.assert_produces_warning(FutureWarning,
389+
check_stacklevel=False) as w2:
390+
to_timedelta(10, unit)
391+
msg = r'.* units are deprecated .*'
392+
assert re.match(msg, str(w2[0].message))
393+
with tm.assert_produces_warning(FutureWarning,
394+
check_stacklevel=False) as w3:
395+
to_timedelta([1, 2], unit)
396+
msg = r'.* units are deprecated .*'
397+
assert re.match(msg, str(w3[0].message))
398+
379399
def test_numeric_conversions(self):
380400
assert Timedelta(0) == np.timedelta64(0, 'ns')
381401
assert Timedelta(10) == np.timedelta64(10, 'ns')

0 commit comments

Comments
 (0)