From 5eaa8d0579a83a1ef3a7635cd1bd409bfd2e5408 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 21 Oct 2018 18:16:44 +0100 Subject: [PATCH 01/21] Adding depracated warning for units M and Y --- pandas/core/tools/timedeltas.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index e3428146b91d8..8abbc249c81cf 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -3,11 +3,12 @@ """ import numpy as np +import warnings from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit from pandas.core.dtypes.common import is_list_like -from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries +from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass import pandas as pd from pandas.core.arrays.timedeltas import sequence_to_td64ns From 0a836d23db81ab0ce474e741755e4a763652f180 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 12 Nov 2018 21:04:04 +0000 Subject: [PATCH 02/21] Added M and Y units deprecated test and whats new note --- doc/source/whatsnew/v0.24.0.rst | 3 +++ pandas/core/tools/timedeltas.py | 4 ++++ pandas/tests/scalar/timedelta/test_timedelta.py | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 16319a3b83ca4..22cd01b30c9b8 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1269,6 +1269,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`) @@ -1280,6 +1281,8 @@ Deprecations - Passing an integer to :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtypes is deprecated, will raise ``TypeError`` in a future version. Use ``obj.fillna(pd.Timedelta(...))`` instead (:issue:`24694`) - ``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`). +- Deprecated the `M` and `Y` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex`(:issue:`16344`) + .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index 8abbc249c81cf..ebed11ab082e4 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -91,6 +91,10 @@ 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.", + FutureWarning, stacklevel=2) + if arg is None: return arg elif isinstance(arg, ABCSeries): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 9b5fdfb06a9fa..e76b90d916446 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -371,6 +371,14 @@ def test_unit_parser(self, units, np_unit, wrapper): result = Timedelta('2{}'.format(unit)) assert result == expected + @pytest.mark.parametrize('units', ['Y', 'y', 'M']) + def test_unit_M_Y_deprecated(self, units): + for unit in units: + with tm.assert_produces_warning(FutureWarning): + to_timedelta(10, unit) + TimedeltaIndex([1, 1, 1], unit) + Timedelta(10, unit) + def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') assert Timedelta(10) == np.timedelta64(10, 'ns') From e890fa69bce7ac435f1ded92486c11adeb9da462 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Fri, 23 Nov 2018 00:35:52 +0000 Subject: [PATCH 03/21] Small fixes --- pandas/core/tools/timedeltas.py | 5 +++-- pandas/tests/scalar/timedelta/test_timedelta.py | 9 ++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index ebed11ab082e4..d2b934602a5dd 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -2,8 +2,9 @@ timedelta support tools """ -import numpy as np import warnings +import numpy as np + from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit @@ -91,7 +92,7 @@ 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']: + if unit in {'Y', 'y', 'M'}: warnings.warn("M and Y units are deprecated.", FutureWarning, stacklevel=2) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index e76b90d916446..5dedd05ce6bdb 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -373,11 +373,10 @@ def test_unit_parser(self, units, np_unit, wrapper): @pytest.mark.parametrize('units', ['Y', 'y', 'M']) def test_unit_M_Y_deprecated(self, units): - for unit in units: - with tm.assert_produces_warning(FutureWarning): - to_timedelta(10, unit) - TimedeltaIndex([1, 1, 1], unit) - Timedelta(10, unit) + with tm.assert_produces_warning(FutureWarning): + to_timedelta(10, units) + TimedeltaIndex([1, 1, 1], units) + Timedelta(10, units) def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') From bfe74e572ff246a52cd8bed7ea53cf191b6ed981 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 16 Dec 2018 16:18:33 +0000 Subject: [PATCH 04/21] Updated test --- pandas/tests/scalar/timedelta/test_timedelta.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 5dedd05ce6bdb..f345970eca9b7 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -368,15 +368,19 @@ def test_unit_parser(self, units, np_unit, wrapper): result = to_timedelta('2{}'.format(unit)) assert result == expected - result = Timedelta('2{}'.format(unit)) + result = Timedelta('2{}'.format(units)) assert result == expected @pytest.mark.parametrize('units', ['Y', 'y', 'M']) - def test_unit_M_Y_deprecated(self, units): - with tm.assert_produces_warning(FutureWarning): + def test_unit_m_y_deprecated(self, units): + with pytest.warns(FutureWarning) as record1: to_timedelta(10, units) - TimedeltaIndex([1, 1, 1], units) + assert len(record1) == 1 + assert str(record1[0].message) == "M and Y units are deprecated." + with pytest.warns(FutureWarning) as record2: Timedelta(10, units) + assert len(record2) == 1 + assert str(record2[0].message) == "M and Y units are deprecated." def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') From e5e1198f97c16da0ed09cfc8a6a657583bd46214 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 13 Jan 2019 00:16:08 +0000 Subject: [PATCH 05/21] Modifications --- pandas/_libs/tslibs/timedeltas.pyx | 4 ++++ pandas/core/indexes/timedeltas.py | 4 ++++ pandas/core/tools/timedeltas.py | 5 ++--- pandas/tests/scalar/timedelta/test_timedelta.py | 16 +++++++--------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 0a19d8749fc7c..04195e111a981 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1185,6 +1185,10 @@ class Timedelta(_Timedelta): "Value must be Timedelta, string, integer, " "float, timedelta or convertible") + if unit in {'Y', 'y', 'M'}: + raise FutureWarning("M and Y units are deprecated and " + "will be removed in a future version.") + if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index cbe5ae198838f..5fba75b470b7a 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -207,6 +207,10 @@ 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'}: + raise FutureWarning("M and Y units are deprecated and " + "will be removed in a future version.") + if isinstance(data, TimedeltaArray): if copy: data = data.copy() diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index d2b934602a5dd..88a458882e029 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -2,7 +2,6 @@ timedelta support tools """ -import warnings import numpy as np @@ -93,8 +92,8 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): "'raise', or 'coerce'}") if unit in {'Y', 'y', 'M'}: - warnings.warn("M and Y units are deprecated.", - FutureWarning, stacklevel=2) + raise FutureWarning("M and Y units are deprecated and " + "will be removed in a future version.") if arg is None: return arg diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index f345970eca9b7..a4dc32eeb2637 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -368,19 +368,17 @@ def test_unit_parser(self, units, np_unit, wrapper): result = to_timedelta('2{}'.format(unit)) assert result == expected - result = Timedelta('2{}'.format(units)) + result = Timedelta('2{}'.format(unit)) assert result == expected @pytest.mark.parametrize('units', ['Y', 'y', 'M']) def test_unit_m_y_deprecated(self, units): - with pytest.warns(FutureWarning) as record1: - to_timedelta(10, units) - assert len(record1) == 1 - assert str(record1[0].message) == "M and Y units are deprecated." - with pytest.warns(FutureWarning) as record2: - Timedelta(10, units) - assert len(record2) == 1 - assert str(record2[0].message) == "M and Y units are deprecated." + for unit in units: + with pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + to_timedelta(10, unit) + Timedelta(10, unit) + TimedeltaIndex(10, unit) def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') From d0dd551bc7fe35dcf2804720bcfa0e202a5b32a6 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 13 Jan 2019 03:29:11 +0000 Subject: [PATCH 06/21] Fixing errors in Travis tests --- pandas/_libs/tslibs/timedeltas.pyx | 5 +++-- pandas/core/indexes/timedeltas.py | 5 +++-- pandas/core/tools/timedeltas.py | 6 ++++-- pandas/tests/scalar/timedelta/test_timedelta.py | 17 +++++++++++------ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 04195e111a981..80417337036ee 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1186,8 +1186,9 @@ class Timedelta(_Timedelta): "float, timedelta or convertible") if unit in {'Y', 'y', 'M'}: - raise FutureWarning("M and Y units are deprecated and " - "will be removed in a future version.") + warnings.warn("M and Y units are deprecated and " + "will be removed in a future version.", + FutureWarning, stacklevel=2) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 5fba75b470b7a..830925535dab1 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -208,8 +208,9 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, .format(cls=cls.__name__, data=repr(data))) if unit in {'Y', 'y', 'M'}: - raise FutureWarning("M and Y units are deprecated and " - "will be removed in a future version.") + 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: diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index 88a458882e029..ccacefc2fc55c 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -2,6 +2,7 @@ timedelta support tools """ +import warnings import numpy as np @@ -92,8 +93,9 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): "'raise', or 'coerce'}") if unit in {'Y', 'y', 'M'}: - raise FutureWarning("M and Y units are deprecated and " - "will be removed in a future version.") + 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 diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index a4dc32eeb2637..c9057db75b570 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -373,12 +373,17 @@ def test_unit_parser(self, units, np_unit, wrapper): @pytest.mark.parametrize('units', ['Y', 'y', 'M']) def test_unit_m_y_deprecated(self, units): - for unit in units: - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): - to_timedelta(10, unit) - Timedelta(10, unit) - TimedeltaIndex(10, unit) + for unit in units: + with pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + to_timedelta(10, unit) + with pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + Timedelta(10, unit) + with pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + TimedeltaIndex(10, unit) + def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') From af5d761078a4fba0f9d7a4491df9844685193523 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 15 Jan 2019 14:56:54 +0000 Subject: [PATCH 07/21] Modified whats new doc and seperated tests in different files --- doc/source/whatsnew/v0.24.0.rst | 2 +- .../tests/indexes/timedeltas/test_timedelta.py | 6 ++++++ pandas/tests/indexes/timedeltas/test_tools.py | 7 +++++++ .../tests/scalar/timedelta/test_timedelta.py | 18 +++++------------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 22cd01b30c9b8..10456f8f5a9b4 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1281,7 +1281,7 @@ Deprecations - Passing an integer to :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtypes is deprecated, will raise ``TypeError`` in a future version. Use ``obj.fillna(pd.Timedelta(...))`` instead (:issue:`24694`) - ``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`). -- Deprecated the `M` and `Y` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex`(:issue:`16344`) +- Deprecated the `M (months)` and `Y (year)` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex`(:issue:`16344`) .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 547366ec79094..42b7139f40fe9 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -325,6 +325,12 @@ 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 pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + TimedeltaIndex([1, 3, 7], unit) + class TestTimeSeries(object): diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index d211219159233..b6d755dbb27aa 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -173,3 +173,10 @@ def test_to_timedelta_on_missing_values(self): actual = pd.to_timedelta(pd.NaT) assert actual.value == timedelta_NaT.astype('int64') + + @pytest.mark.parametrize('arg', [1, [1, 2]]) + @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) + def test_to_timedelta_unit_m_y_deprecated(self, arg, unit): + with pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + to_timedelta(arg, unit) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index c9057db75b570..f7b158f1d968d 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -371,19 +371,11 @@ def test_unit_parser(self, units, np_unit, wrapper): result = Timedelta('2{}'.format(unit)) assert result == expected - @pytest.mark.parametrize('units', ['Y', 'y', 'M']) - def test_unit_m_y_deprecated(self, units): - for unit in units: - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): - to_timedelta(10, unit) - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): - Timedelta(10, unit) - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): - TimedeltaIndex(10, unit) - + @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) + def test_unit_m_y_deprecated(self, unit): + with pytest.raises(FutureWarning, + match=r'.* units are deprecated .*'): + Timedelta(10, unit) def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') From 938c6fed6f555e90493a6937a85c842f02e90944 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 15 Jan 2019 16:34:59 +0000 Subject: [PATCH 08/21] Amend warnings --- pandas/tests/indexes/timedeltas/test_timedelta.py | 8 ++++++-- pandas/tests/indexes/timedeltas/test_tools.py | 8 ++++++-- pandas/tests/scalar/timedelta/test_timedelta.py | 7 +++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 42b7139f40fe9..bd131962b7adf 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -13,6 +13,9 @@ from ..datetimelike import DatetimeLike +import re + + randn = np.random.randn @@ -327,9 +330,10 @@ def test_freq_conversion(self): @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) def test_unit_m_y_deprecated(self, unit): - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): + 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): diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index b6d755dbb27aa..3518286cf8160 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -10,6 +10,8 @@ import pandas.util.testing as tm from pandas.util.testing import assert_series_equal +import re + class TestTimedeltas(object): @@ -177,6 +179,8 @@ def test_to_timedelta_on_missing_values(self): @pytest.mark.parametrize('arg', [1, [1, 2]]) @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) def test_to_timedelta_unit_m_y_deprecated(self, arg, unit): - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): + with tm.assert_produces_warning(FutureWarning) as w: to_timedelta(arg, unit) + msg = r'.* units are deprecated .*' + assert re.match(msg, str(w[0].message)) + diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index f7b158f1d968d..6f20e3dd038e6 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -12,6 +12,8 @@ Series, Timedelta, TimedeltaIndex, timedelta_range, to_timedelta) import pandas.util.testing as tm +import re + class TestTimedeltaArithmetic(object): @@ -373,9 +375,10 @@ def test_unit_parser(self, units, np_unit, wrapper): @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) def test_unit_m_y_deprecated(self, unit): - with pytest.raises(FutureWarning, - match=r'.* units are deprecated .*'): + with tm.assert_produces_warning(FutureWarning) as w: Timedelta(10, unit) + msg = r'.* units are deprecated .*' + assert re.match(msg, str(w[0].message)) def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, 'ns') From 16f8b9c226bc41a9a35581cd54ec0c5de7168e06 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 15 Jan 2019 23:46:44 +0000 Subject: [PATCH 09/21] remove stacklevel --- pandas/_libs/tslibs/timedeltas.pyx | 4 ++-- pandas/core/indexes/timedeltas.py | 2 +- pandas/core/tools/timedeltas.py | 2 +- pandas/tests/indexes/timedeltas/test_tools.py | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 80417337036ee..4b34fcac01947 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1188,7 +1188,7 @@ class Timedelta(_Timedelta): 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) + FutureWarning) if is_timedelta64_object(value): value = value.view('i8') @@ -1523,4 +1523,4 @@ cdef _broadcast_floordiv_td64(int64_t value, object other, # resolution in ns Timedelta.min = Timedelta(np.iinfo(np.int64).min + 1) -Timedelta.max = Timedelta(np.iinfo(np.int64).max) +Timedelta.max = Timedelta(np.iinfo(np.int64).max) \ No newline at end of file diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 830925535dab1..08a6552fc4c6c 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -210,7 +210,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, 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) + FutureWarning) if isinstance(data, TimedeltaArray): if copy: diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index ccacefc2fc55c..c17e2abc2de24 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -95,7 +95,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): 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) + FutureWarning) if arg is None: return arg diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index 3518286cf8160..86e281caa282a 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -183,4 +183,3 @@ def test_to_timedelta_unit_m_y_deprecated(self, arg, unit): to_timedelta(arg, unit) msg = r'.* units are deprecated .*' assert re.match(msg, str(w[0].message)) - From 5a643204621ff26141d864010b4d59b5229a9c19 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Wed, 16 Jan 2019 22:24:37 +0000 Subject: [PATCH 10/21] Added stack level to warning --- doc/source/whatsnew/v0.24.0.rst | 2 +- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/core/indexes/timedeltas.py | 2 +- pandas/core/tools/timedeltas.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 10456f8f5a9b4..9976631fffb3e 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1281,7 +1281,7 @@ Deprecations - Passing an integer to :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtypes is deprecated, will raise ``TypeError`` in a future version. Use ``obj.fillna(pd.Timedelta(...))`` instead (:issue:`24694`) - ``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`). -- Deprecated the `M (months)` and `Y (year)` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex`(:issue:`16344`) +- Deprecated the `M (months)` and `Y (year)` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex` (:issue:`16344`) .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 4b34fcac01947..8d49dc6844576 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1188,7 +1188,7 @@ class Timedelta(_Timedelta): if unit in {'Y', 'y', 'M'}: warnings.warn("M and Y units are deprecated and " "will be removed in a future version.", - FutureWarning) + FutureWarning, stacklevel=3) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 08a6552fc4c6c..bc4803660621c 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -210,7 +210,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, if unit in {'Y', 'y', 'M'}: warnings.warn("M and Y units are deprecated and " "will be removed in a future version.", - FutureWarning) + FutureWarning, stacklevel=3) if isinstance(data, TimedeltaArray): if copy: diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index c17e2abc2de24..6bb14b48d8671 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -95,7 +95,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): if unit in {'Y', 'y', 'M'}: warnings.warn("M and Y units are deprecated and " "will be removed in a future version.", - FutureWarning) + FutureWarning, stacklevel=3) if arg is None: return arg From 3b674765cd2716272e00267b4316dce268e39ca6 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 29 Jan 2019 00:08:31 +0000 Subject: [PATCH 11/21] added stack_level=False to make tests pass locally --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/core/indexes/timedeltas.py | 4 ++-- pandas/core/tools/timedeltas.py | 2 +- pandas/tests/scalar/timedelta/test_timedelta.py | 8 ++++++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 8d49dc6844576..130e877117b69 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1188,7 +1188,7 @@ class Timedelta(_Timedelta): 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) + FutureWarning, stacklevel=2) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index bc4803660621c..7da2387f1d713 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -197,7 +197,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, warnings.warn("Creating a TimedeltaIndex by passing range " "endpoints is deprecated. Use " "`pandas.timedelta_range` instead.", - FutureWarning, stacklevel=2) + FutureWarning, stacklevel=3) result = TimedeltaArray._generate_range(start, end, periods, freq, closed=closed) return cls._simple_new(result._data, freq=freq, name=name) @@ -210,7 +210,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, 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) + FutureWarning, stacklevel=2) if isinstance(data, TimedeltaArray): if copy: diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index 6bb14b48d8671..ccacefc2fc55c 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -95,7 +95,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): 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) + FutureWarning, stacklevel=2) if arg is None: return arg diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 6f20e3dd038e6..8173693f7306a 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -375,10 +375,14 @@ def test_unit_parser(self, units, np_unit, wrapper): @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) def test_unit_m_y_deprecated(self, unit): - with tm.assert_produces_warning(FutureWarning) as w: + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False) as w1: Timedelta(10, unit) msg = r'.* units are deprecated .*' - assert re.match(msg, str(w[0].message)) + 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') From ab524605c6179c777df7a4b13be02d0a8d02deef Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 29 Jan 2019 01:18:48 +0000 Subject: [PATCH 12/21] Add check_stacklevel=False to test_range_kwargs_deprecated to remove travis error Modifications --- pandas/tests/indexes/timedeltas/test_construction.py | 2 +- pandas/tests/indexes/timedeltas/test_tools.py | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/pandas/tests/indexes/timedeltas/test_construction.py b/pandas/tests/indexes/timedeltas/test_construction.py index 3938d6acad2f0..b96e4ab4179a7 100644 --- a/pandas/tests/indexes/timedeltas/test_construction.py +++ b/pandas/tests/indexes/timedeltas/test_construction.py @@ -18,7 +18,7 @@ def test_verify_integrity_deprecated(self): def test_range_kwargs_deprecated(self): # GH#23919 - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): TimedeltaIndex(start='1 Day', end='3 Days', freq='D') def test_int64_nocopy(self): diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index 86e281caa282a..5898a8aaef1be 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -175,11 +175,3 @@ def test_to_timedelta_on_missing_values(self): actual = pd.to_timedelta(pd.NaT) assert actual.value == timedelta_NaT.astype('int64') - - @pytest.mark.parametrize('arg', [1, [1, 2]]) - @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) - def test_to_timedelta_unit_m_y_deprecated(self, arg, unit): - with tm.assert_produces_warning(FutureWarning) as w: - to_timedelta(arg, unit) - msg = r'.* units are deprecated .*' - assert re.match(msg, str(w[0].message)) From b1599a5b1fceb1cb9b761eb53539444f1beafa79 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sat, 2 Feb 2019 14:48:16 +0000 Subject: [PATCH 13/21] Added Requested changes --- doc/source/whatsnew/v0.24.0.rst | 1 - doc/source/whatsnew/v0.25.0.rst | 5 +---- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/tests/indexes/timedeltas/test_timedelta.py | 3 ++- pandas/tests/indexes/timedeltas/test_tools.py | 2 -- pandas/tests/scalar/timedelta/test_timedelta.py | 6 ++++-- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index e6ba896a16fbc..54a52a72a62c6 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1327,7 +1327,6 @@ Deprecations - Passing an integer to :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtypes is deprecated, will raise ``TypeError`` in a future version. Use ``obj.fillna(pd.Timedelta(...))`` instead (:issue:`24694`) - ``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`). -- Deprecated the `M (months)` and `Y (year)` `units` parameter of :func: `pandas.to_timedelta`, :func: `pandas.Timedelta` and :func: `pandas.TimedeltaIndex` (:issue:`16344`) .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 09626be713c4f..dcec664ebe4a1 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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: diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 130e877117b69..80417337036ee 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1523,4 +1523,4 @@ cdef _broadcast_floordiv_td64(int64_t value, object other, # resolution in ns Timedelta.min = Timedelta(np.iinfo(np.int64).min + 1) -Timedelta.max = Timedelta(np.iinfo(np.int64).max) \ No newline at end of file +Timedelta.max = Timedelta(np.iinfo(np.int64).max) diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 2a944dc3e80bf..4fcac8079642b 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -330,7 +330,8 @@ def test_freq_conversion(self): @pytest.mark.parametrize('unit', ['Y', 'y', 'M']) def test_unit_m_y_deprecated(self, unit): - with tm.assert_produces_warning(FutureWarning) as w: + 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)) diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index 5898a8aaef1be..d211219159233 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -10,8 +10,6 @@ import pandas.util.testing as tm from pandas.util.testing import assert_series_equal -import re - class TestTimedeltas(object): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index f9c1b1af21a34..9f28ba3f37a6f 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -380,11 +380,13 @@ def test_unit_parser(self, units, np_unit, wrapper): @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: + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False) 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: + 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)) From a21a6a21175338306019985210455737403ca623 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sat, 2 Feb 2019 16:36:45 +0000 Subject: [PATCH 14/21] Fix linting and add pytest filter warnings decorator --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/core/tools/timedeltas.py | 4 ++-- pandas/tests/indexes/timedeltas/test_timedelta.py | 4 +--- pandas/tests/scalar/timedelta/test_timedelta.py | 4 ++-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 80417337036ee..c74dbed1c5c6f 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1188,7 +1188,7 @@ class Timedelta(_Timedelta): 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) + FutureWarning, stacklevel=3) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index 6fb237fdfab3c..30cb15f311b9f 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -3,13 +3,13 @@ """ import warnings -import numpy as np +import numpy as np from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit from pandas.core.dtypes.common import is_list_like -from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass +from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries import pandas as pd from pandas.core.arrays.timedeltas import sequence_to_td64ns diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 4fcac8079642b..989363046dfd8 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -1,4 +1,5 @@ from datetime import timedelta +import re import numpy as np import pytest @@ -13,9 +14,6 @@ from ..datetimelike import DatetimeLike -import re - - randn = np.random.randn diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 9f28ba3f37a6f..a077de42ac830 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -1,5 +1,6 @@ """ test the scalar Timedelta """ from datetime import timedelta +import re import numpy as np import pytest @@ -12,8 +13,6 @@ Series, Timedelta, TimedeltaIndex, timedelta_range, to_timedelta) import pandas.util.testing as tm -import re - class TestTimedeltaArithmetic(object): @@ -319,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'), From 261672e6a66997051757e245cddf0134c2ee62da Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 3 Feb 2019 00:14:09 +0000 Subject: [PATCH 15/21] add decorator to skip pytest for python 2.7 build --- pandas/tests/scalar/timedelta/test_timedelta.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index a077de42ac830..cbb21eef0af92 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -1,6 +1,7 @@ """ test the scalar Timedelta """ from datetime import timedelta import re +import sys import numpy as np import pytest @@ -378,6 +379,8 @@ 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, From 4010bdb3856a3f554fbf4308af3993639b93129c Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 3 Feb 2019 01:16:14 +0000 Subject: [PATCH 16/21] Reverting changes in unrelated files - see @jorisvandenbossche comment" --- pandas/core/indexes/timedeltas.py | 2 +- pandas/tests/indexes/timedeltas/test_construction.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 7da2387f1d713..830925535dab1 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -197,7 +197,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, warnings.warn("Creating a TimedeltaIndex by passing range " "endpoints is deprecated. Use " "`pandas.timedelta_range` instead.", - FutureWarning, stacklevel=3) + FutureWarning, stacklevel=2) result = TimedeltaArray._generate_range(start, end, periods, freq, closed=closed) return cls._simple_new(result._data, freq=freq, name=name) diff --git a/pandas/tests/indexes/timedeltas/test_construction.py b/pandas/tests/indexes/timedeltas/test_construction.py index b96e4ab4179a7..3938d6acad2f0 100644 --- a/pandas/tests/indexes/timedeltas/test_construction.py +++ b/pandas/tests/indexes/timedeltas/test_construction.py @@ -18,7 +18,7 @@ def test_verify_integrity_deprecated(self): def test_range_kwargs_deprecated(self): # GH#23919 - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with tm.assert_produces_warning(FutureWarning): TimedeltaIndex(start='1 Day', end='3 Days', freq='D') def test_int64_nocopy(self): From 3f87a0b617b25650ffed083c30fd1ff203ef9a67 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 3 Feb 2019 17:55:57 +0000 Subject: [PATCH 17/21] Minor changes --- doc/source/whatsnew/v0.24.0.rst | 1 - pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/tests/indexes/timedeltas/test_timedelta.py | 3 +-- pandas/tests/scalar/timedelta/test_timedelta.py | 7 +++---- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 54a52a72a62c6..a38aba5ab6b9b 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1315,7 +1315,6 @@ 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`) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index c74dbed1c5c6f..80417337036ee 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1188,7 +1188,7 @@ class Timedelta(_Timedelta): 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) + FutureWarning, stacklevel=2) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 989363046dfd8..3cbd9942f9d84 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -328,8 +328,7 @@ def test_freq_conversion(self): @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: + 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)) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index cbb21eef0af92..26ed089dff675 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -379,12 +379,11 @@ 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.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, - check_stacklevel=False) as w1: + with tm.assert_produces_warning(FutureWarning) as w1: Timedelta(10, unit) msg = r'.* units are deprecated .*' assert re.match(msg, str(w1[0].message)) From f44bae0e6f6ee417b34babae6d847b0f39a25bc5 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 3 Feb 2019 19:50:24 +0000 Subject: [PATCH 18/21] Reverting back to checkstacklevel=False for Timdelta as Travis was failing --- pandas/tests/scalar/timedelta/test_timedelta.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 26ed089dff675..c322a97497b8d 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -383,7 +383,8 @@ def test_unit_parser(self, units, np_unit, wrapper): 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: + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False) as w1: Timedelta(10, unit) msg = r'.* units are deprecated .*' assert re.match(msg, str(w1[0].message)) From b745b8caf76dbb95a21c4aaeb1d4b66edca2ab9f Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 4 Feb 2019 00:23:37 +0000 Subject: [PATCH 19/21] set stacklevel=1 for Timedelta and remove checkstacklevel=False --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/tests/scalar/timedelta/test_timedelta.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 80417337036ee..a6c18a9422fab 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1188,7 +1188,7 @@ class Timedelta(_Timedelta): 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) + FutureWarning, stacklevel=1) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index c322a97497b8d..26ed089dff675 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -383,8 +383,7 @@ def test_unit_parser(self, units, np_unit, wrapper): 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, - check_stacklevel=False) as w1: + with tm.assert_produces_warning(FutureWarning) as w1: Timedelta(10, unit) msg = r'.* units are deprecated .*' assert re.match(msg, str(w1[0].message)) From ca0ea094363f33646f4cb9d947180bfe6ef614b8 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Fri, 8 Feb 2019 21:02:34 +0000 Subject: [PATCH 20/21] added requested changes and removed unused sys import - linter --- pandas/_libs/tslibs/timedeltas.pyx | 10 +++++----- pandas/tests/scalar/timedelta/test_timedelta.py | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index a6c18a9422fab..f08a57375a301 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -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): @@ -1185,11 +1190,6 @@ 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=1) - if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 26ed089dff675..7d5b479810205 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -1,7 +1,6 @@ """ test the scalar Timedelta """ from datetime import timedelta import re -import sys import numpy as np import pytest @@ -379,8 +378,7 @@ def test_unit_parser(self, units, np_unit, wrapper): result = Timedelta('2{}'.format(unit)) assert result == expected - @pytest.mark.skipif(sys.version_info < (3, 5), - reason="requires python3.5 or higher") + @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: @@ -392,6 +390,11 @@ def test_unit_m_y_deprecated(self, unit): 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') From 76497d35814c5931c92bb4d57369e68aa0bd7471 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Fri, 8 Feb 2019 22:56:05 +0000 Subject: [PATCH 21/21] reverting file - removed added line --- doc/source/whatsnew/v0.24.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index a38aba5ab6b9b..a49ea2cf493a6 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1327,7 +1327,6 @@ 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