Skip to content

DEPR: Add Deprecated warning for timedelta with passed units M and Y #23264

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

Merged
merged 26 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5eaa8d0
Adding depracated warning for units M and Y
ryankarlos Oct 21, 2018
0a836d2
Added M and Y units deprecated test and whats new note
ryankarlos Nov 12, 2018
e890fa6
Small fixes
ryankarlos Nov 23, 2018
bfe74e5
Updated test
ryankarlos Dec 16, 2018
e5e1198
Modifications
ryankarlos Jan 13, 2019
d0dd551
Fixing errors in Travis tests
ryankarlos Jan 13, 2019
af5d761
Modified whats new doc and seperated tests in different files
ryankarlos Jan 15, 2019
938c6fe
Amend warnings
ryankarlos Jan 15, 2019
16f8b9c
remove stacklevel
ryankarlos Jan 15, 2019
5a64320
Added stack level to warning
ryankarlos Jan 16, 2019
3b67476
added stack_level=False to make tests pass locally
ryankarlos Jan 29, 2019
ab52460
Add check_stacklevel=False to test_range_kwargs_deprecated to remove …
ryankarlos Jan 29, 2019
9cb51c5
Merge remote-tracking branch 'upstream/master' into timedelta_unit_de…
ryankarlos Jan 29, 2019
c6f82c7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
ryankarlos Feb 2, 2019
b1599a5
Added Requested changes
ryankarlos Feb 2, 2019
a21a6a2
Fix linting and add pytest filter warnings decorator
ryankarlos Feb 2, 2019
261672e
add decorator to skip pytest for python 2.7 build
ryankarlos Feb 3, 2019
4010bdb
Reverting changes in unrelated files - see @jorisvandenbossche comment"
ryankarlos Feb 3, 2019
3f87a0b
Minor changes
ryankarlos Feb 3, 2019
b7aedc1
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
ryankarlos Feb 3, 2019
f44bae0
Reverting back to checkstacklevel=False for Timdelta as Travis was fa…
ryankarlos Feb 3, 2019
5d2dceb
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
ryankarlos Feb 3, 2019
b745b8c
set stacklevel=1 for Timedelta and remove checkstacklevel=False
ryankarlos Feb 4, 2019
d014f56
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
ryankarlos Feb 8, 2019
ca0ea09
added requested changes and removed unused sys import - linter
ryankarlos Feb 8, 2019
76497d3
reverting file - removed added line
ryankarlos Feb 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ Other API Changes
Deprecations
~~~~~~~~~~~~

-
-
-

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

.. _whatsnew_0250.prior_deprecations:

Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,11 @@ class Timedelta(_Timedelta):
"[weeks, days, hours, minutes, seconds, "
"milliseconds, microseconds, nanoseconds]")

if unit in {'Y', 'y', 'M'}:
warnings.warn("M and Y units are deprecated and "
"will be removed in a future version.",
FutureWarning, stacklevel=1)

if isinstance(value, Timedelta):
value = value.value
elif is_string_object(value):
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None,
'collection of some kind, {data} was passed'
.format(cls=cls.__name__, data=repr(data)))

if unit in {'Y', 'y', 'M'}:
warnings.warn("M and Y units are deprecated and "
"will be removed in a future version.",
FutureWarning, stacklevel=2)

if isinstance(data, TimedeltaArray):
if copy:
data = data.copy()
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
timedelta support tools
"""

import warnings

import numpy as np

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

if unit in {'Y', 'y', 'M'}:
warnings.warn("M and Y units are deprecated and "
"will be removed in a future version.",
FutureWarning, stacklevel=2)

if arg is None:
return arg
elif isinstance(arg, ABCSeries):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import timedelta
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -325,6 +326,13 @@ def test_freq_conversion(self):
result = td.astype('timedelta64[s]')
assert_index_equal(result, expected)

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


class TestTimeSeries(object):

Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" test the scalar Timedelta """
from datetime import timedelta
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -317,6 +318,7 @@ def test_nat_converters(self):
assert result.dtype.kind == 'm'
assert result.astype('int64') == iNaT

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

@pytest.mark.skipif(compat.PY2, reason="requires python3.5 or higher")
@pytest.mark.parametrize('unit', ['Y', 'y', 'M'])
def test_unit_m_y_deprecated(self, unit):
with tm.assert_produces_warning(FutureWarning) as w1:
Timedelta(10, unit)
msg = r'.* units are deprecated .*'
assert re.match(msg, str(w1[0].message))
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False) as w2:
to_timedelta(10, unit)
msg = r'.* units are deprecated .*'
assert re.match(msg, str(w2[0].message))
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False) as w3:
to_timedelta([1, 2], unit)
msg = r'.* units are deprecated .*'
assert re.match(msg, str(w3[0].message))

def test_numeric_conversions(self):
assert Timedelta(0) == np.timedelta64(0, 'ns')
assert Timedelta(10) == np.timedelta64(10, 'ns')
Expand Down