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 18 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,7 @@ Deprecations
- The ``keep_tz=False`` option (the default) of the ``keep_tz`` keyword of
:meth:`DatetimeIndex.to_series` is deprecated (:issue:`17832`).
- Timezone converting a tz-aware ``datetime.datetime`` or :class:`Timestamp` with :class:`Timestamp` and the ``tz`` argument is now deprecated. Instead, use :meth:`Timestamp.tz_convert` (:issue:`23579`)
<<<<<<< HEAD
- :func:`pandas.api.types.is_period` is deprecated in favor of ``pandas.api.types.is_period_dtype`` (:issue:`23917`)
- :func:`pandas.api.types.is_datetimetz` is deprecated in favor of ``pandas.api.types.is_datetime64tz`` (:issue:`23917`)
- Creating a :class:`TimedeltaIndex`, :class:`DatetimeIndex`, or :class:`PeriodIndex` by passing range arguments `start`, `end`, and `periods` is deprecated in favor of :func:`timedelta_range`, :func:`date_range`, or :func:`period_range` (:issue:`23919`)
Expand All @@ -1327,6 +1328,7 @@ Deprecations
- ``Series.cat.categorical``, ``Series.cat.name`` and ``Sersies.cat.index`` have been deprecated. Use the attributes on ``Series.cat`` or ``Series`` directly. (:issue:`24751`).
- Passing a dtype without a precision like ``np.dtype('datetime64')`` or ``timedelta64`` to :class:`Index`, :class:`DatetimeIndex` and :class:`TimedeltaIndex` is now deprecated. Use the nanosecond-precision dtype instead (:issue:`24753`).


.. _whatsnew_0240.deprecations.datetimelike_int_ops:

Integer Addition/Subtraction with Datetimes and Timedeltas is Deprecated
Expand Down
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 @@ -1185,6 +1185,11 @@ class Timedelta(_Timedelta):
"Value must be Timedelta, string, integer, "
"float, timedelta or convertible")

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

if is_timedelta64_object(value):
value = value.view('i8')

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
9 changes: 9 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,14 @@ 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,
check_stacklevel=False) 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
18 changes: 18 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" test the scalar Timedelta """
from datetime import timedelta
import re
import sys

import numpy as np
import pytest
Expand Down Expand Up @@ -317,6 +319,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 +379,21 @@ def test_unit_parser(self, units, np_unit, wrapper):
result = Timedelta('2{}'.format(unit))
assert result == expected

@pytest.mark.skipif(sys.version_info < (3, 6),
reason="requires python3.6 or higher")
@pytest.mark.parametrize('unit', ['Y', 'y', 'M'])
def test_unit_m_y_deprecated(self, unit):
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False) as w1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the check_stacklevel=False here?
Here it should be work, in the one below for to_timedelta, it is fine to keep.

Copy link
Contributor Author

@ryankarlos ryankarlos Feb 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche I just tried running this locally and i get an assertion error if I remove check_stacklevel for Timdelta (setting stacklevel = 2 or stacklevel=3 seems to make no difference). Not sure if it will get through travis if i don't set to check_stacklevel=False- i remember experimenting with this in the previous builds which failed.:

../../../anaconda/envs/pandas-dev/lib/python3.7/contextlib.py:119: AssertionError
________________________________ TestTimedeltas.test_unit_m_y_deprecated[M] _________________________________

self = <pandas.tests.scalar.timedelta.test_timedelta.TestTimedeltas object at 0x1c169e02b0>, unit = 'M'

@pytest.mark.skipif(sys.version_info < (3, 5),
                    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)

pandas/tests/scalar/timedelta/test_timedelta.py:387:


self = <contextlib._GeneratorContextManager object at 0x1c169e0438>, type = None, value = None
traceback = None

def __exit__(self, type, value, traceback):
    if type is None:
        try:
          next(self.gen)

E AssertionError: Warning not set with correct stacklevel. File where warning is raised: /Users/ryannazareth/anaconda/envs/pandas-dev/lib/python3.7/site-packages/_pytest/python.py != /Users/ryannazareth/Documents/Python_sprints/pandas-ryankarlos/pandas/tests/scalar/timedelta/test_timedelta.py. Warning message: M and Y units are deprecated and will be removed in a future version.

../../../anaconda/envs/pandas-dev/lib/python3.7/contextlib.py:119: AssertionError
================================== 3 failed, 75 deselected in 0.68 seconds ==================================

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))

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