Skip to content

Commit 8e36374

Browse files
jbrockmendelproost
authored andcommitted
DEPR: YyM units for Timedelta and friends (pandas-dev#30118)
1 parent 2032c49 commit 8e36374

File tree

6 files changed

+21
-35
lines changed

6 files changed

+21
-35
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
539539
- Changed the the default value of `inplace` in :meth:`DataFrame.set_index` and :meth:`Series.set_axis`. It now defaults to ``False`` (:issue:`27600`)
540540
- Removed the previously deprecated :attr:`Series.cat.categorical`, :attr:`Series.cat.index`, :attr:`Series.cat.name` (:issue:`24751`)
541541
- :func:`to_datetime` no longer accepts "box" argument, always returns :class:`DatetimeIndex` or :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`24486`)
542+
- :func:`to_timedelta`, :class:`Timedelta`, and :class:`TimedeltaIndex` no longer allow "M", "y", or "Y" for the "unit" argument (:issue:`23264`)
542543
- Removed the previously deprecated ``time_rule`` keyword from (non-public) :func:`offsets.generate_range`, which has been moved to :func:`core.arrays._ranges.generate_range` (:issue:`24157`)
543544
- :meth:`DataFrame.loc` or :meth:`Series.loc` with listlike indexers and missing labels will no longer reindex (:issue:`17295`)
544545
- :meth:`DataFrame.to_excel` and :meth:`Series.to_excel` with non-existent columns will no longer reindex (:issue:`17295`)

pandas/_libs/tslibs/timedeltas.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import collections
22
import textwrap
3-
import warnings
43

54
import cython
65

@@ -1204,9 +1203,10 @@ class Timedelta(_Timedelta):
12041203
"milliseconds, microseconds, nanoseconds]")
12051204

12061205
if unit in {'Y', 'y', 'M'}:
1207-
warnings.warn("M and Y units are deprecated and "
1208-
"will be removed in a future version.",
1209-
FutureWarning, stacklevel=1)
1206+
raise ValueError(
1207+
"Units 'M' and 'Y' are no longer supported, as they do not "
1208+
"represent unambiguous timedelta values durations."
1209+
)
12101210

12111211
if isinstance(value, Timedelta):
12121212
value = value.value

pandas/core/indexes/timedeltas.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
""" implement the TimedeltaIndex """
22
from datetime import datetime
3-
import warnings
43

54
import numpy as np
65

@@ -201,11 +200,9 @@ def __new__(
201200
)
202201

203202
if unit in {"Y", "y", "M"}:
204-
warnings.warn(
205-
"M and Y units are deprecated and "
206-
"will be removed in a future version.",
207-
FutureWarning,
208-
stacklevel=2,
203+
raise ValueError(
204+
"Units 'M' and 'Y' are no longer supported, as they do not "
205+
"represent unambiguous timedelta values durations."
209206
)
210207

211208
if isinstance(data, TimedeltaArray):

pandas/core/tools/timedeltas.py

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

5-
import warnings
6-
75
import numpy as np
86

97
from pandas._libs.tslibs import NaT
@@ -100,10 +98,9 @@ def to_timedelta(arg, unit="ns", box=True, errors="raise"):
10098
raise ValueError("errors must be one of 'ignore', 'raise', or 'coerce'}")
10199

102100
if unit in {"Y", "y", "M"}:
103-
warnings.warn(
104-
"M and Y units are deprecated and will be removed in a future version.",
105-
FutureWarning,
106-
stacklevel=2,
101+
raise ValueError(
102+
"Units 'M' and 'Y' are no longer supported, as they do not "
103+
"represent unambiguous timedelta values durations."
107104
)
108105

109106
if arg is None:

pandas/tests/indexes/timedeltas/test_timedelta.py

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

43
import numpy as np
54
import pytest
@@ -326,11 +325,10 @@ def test_freq_conversion(self):
326325
tm.assert_index_equal(result, expected)
327326

328327
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
329-
def test_unit_m_y_deprecated(self, unit):
330-
with tm.assert_produces_warning(FutureWarning) as w:
328+
def test_unit_m_y_raises(self, unit):
329+
msg = "Units 'M' and 'Y' are no longer supported"
330+
with pytest.raises(ValueError, match=msg):
331331
TimedeltaIndex([1, 3, 7], unit)
332-
msg = r".* units are deprecated .*"
333-
assert re.match(msg, str(w[0].message))
334332

335333

336334
class TestTimeSeries:

pandas/tests/scalar/timedelta/test_timedelta.py

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

54
import numpy as np
65
import pytest
@@ -318,12 +317,9 @@ def test_nat_converters(self):
318317
assert result.dtype.kind == "M"
319318
assert result.astype("int64") == iNaT
320319

321-
@pytest.mark.filterwarnings("ignore:M and Y units are deprecated")
322320
@pytest.mark.parametrize(
323321
"units, np_unit",
324322
[
325-
(["Y", "y"], "Y"),
326-
(["M"], "M"),
327323
(["W", "w"], "W"),
328324
(["D", "d", "days", "day", "Days", "Day"], "D"),
329325
(
@@ -426,19 +422,16 @@ def test_unit_parser(self, units, np_unit, wrapper):
426422
assert result == expected
427423

428424
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
429-
def test_unit_m_y_deprecated(self, unit):
430-
with tm.assert_produces_warning(FutureWarning) as w1:
425+
def test_unit_m_y_raises(self, unit):
426+
msg = "Units 'M' and 'Y' are no longer supported"
427+
with pytest.raises(ValueError, match=msg):
431428
Timedelta(10, unit)
432-
msg = r".* units are deprecated .*"
433-
assert re.match(msg, str(w1[0].message))
434-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False) as w2:
429+
430+
with pytest.raises(ValueError, match=msg):
435431
to_timedelta(10, unit)
436-
msg = r".* units are deprecated .*"
437-
assert re.match(msg, str(w2[0].message))
438-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False) as w3:
432+
433+
with pytest.raises(ValueError, match=msg):
439434
to_timedelta([1, 2], unit)
440-
msg = r".* units are deprecated .*"
441-
assert re.match(msg, str(w3[0].message))
442435

443436
def test_numeric_conversions(self):
444437
assert Timedelta(0) == np.timedelta64(0, "ns")

0 commit comments

Comments
 (0)