From e798b10cb3592d42d4849cfa45276eac72b9ad58 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Mon, 4 Jan 2021 21:15:48 -0800 Subject: [PATCH 1/8] DEPR: Rolling.win_type returning freq --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/core/window/rolling.py | 15 ++++++++++++++- pandas/tests/window/test_win_type.py | 8 +++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index ac3b5dcaf53ae..af68850cdcec9 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -161,7 +161,7 @@ Deprecations - Deprecated :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth` as a public methods, users should use :meth:`MultiIndex.is_monotonic_increasing` instead (:issue:`32259`) - Deprecated keyword ``try_cast`` in :meth:`Series.where`, :meth:`Series.mask`, :meth:`DataFrame.where`, :meth:`DataFrame.mask`; cast results manually if desired (:issue:`38836`) - Deprecated comparison of :class:`Timestamp` object with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`) -- +- Deprecated :attr:`Rolling.win_type` returning ``"freq"`` (:issue:``) .. --------------------------------------------------------------------------- diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 7ae1e61d426b9..abeb63be47c82 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -110,7 +110,8 @@ def __init__( self.window = window self.min_periods = min_periods self.center = center - self.win_type = win_type + # TODO: Change this back to self.win_type once deprecation is enforced + self._win_type = win_type self.axis = obj._get_axis_number(axis) if axis is not None else None self.method = method self._win_freq_i8 = None @@ -131,6 +132,18 @@ def __init__( ) self.validate() + @property + def win_type(self): + if self._win_freq_i8 is not None: + warnings.warn( + "win_type will no longer return 'freq' in a future version. " + "Check the type of self.window instead.", + FutureWarning, + stacklevel=2, + ) + return "freq" + return self._win_type + def validate(self) -> None: if self.center is not None and not is_bool(self.center): raise ValueError("center must be a boolean") diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index 4b1028e165c80..784dc8d23264d 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -4,7 +4,7 @@ from pandas.errors import UnsupportedFunctionCall import pandas.util._test_decorators as td -from pandas import DataFrame, Series, Timedelta, concat +from pandas import DataFrame, Series, Timedelta, concat, date_range import pandas._testing as tm from pandas.api.indexers import BaseIndexer @@ -137,6 +137,12 @@ def test_consistent_win_type_freq(arg): s.rolling(arg, win_type="freq") +def test_win_type_freq_return_deprecation(): + freq_roll = Series(range(2), index=date_range("2020", periods=2)).rolling("2s") + with tm.assert_produces_warning(FutureWarning): + freq_roll.win_type + + @td.skip_if_no_scipy def test_win_type_not_implemented(): class CustomIndexer(BaseIndexer): From dcdb38018268ab358ecbb1df36f32a71f86b5d66 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Mon, 4 Jan 2021 21:17:30 -0800 Subject: [PATCH 2/8] Add PR number --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index af68850cdcec9..cbb5121782ad9 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -161,7 +161,7 @@ Deprecations - Deprecated :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth` as a public methods, users should use :meth:`MultiIndex.is_monotonic_increasing` instead (:issue:`32259`) - Deprecated keyword ``try_cast`` in :meth:`Series.where`, :meth:`Series.mask`, :meth:`DataFrame.where`, :meth:`DataFrame.mask`; cast results manually if desired (:issue:`38836`) - Deprecated comparison of :class:`Timestamp` object with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`) -- Deprecated :attr:`Rolling.win_type` returning ``"freq"`` (:issue:``) +- Deprecated :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`) .. --------------------------------------------------------------------------- From e9cffde1c243037bf796e98ff934672e8dc25bec Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Mon, 4 Jan 2021 21:17:48 -0800 Subject: [PATCH 3/8] Add extra space below --- doc/source/whatsnew/v1.3.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index cbb5121782ad9..598cb68e0198c 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -162,6 +162,7 @@ Deprecations - Deprecated keyword ``try_cast`` in :meth:`Series.where`, :meth:`Series.mask`, :meth:`DataFrame.where`, :meth:`DataFrame.mask`; cast results manually if desired (:issue:`38836`) - Deprecated comparison of :class:`Timestamp` object with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`) - Deprecated :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`) +- .. --------------------------------------------------------------------------- From 6cd2e557978d40a0917ae4de925a23d4c9e4eab5 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 5 Jan 2021 11:12:22 -0800 Subject: [PATCH 4/8] Deprecate is_datetimelike as well --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/core/window/rolling.py | 9 +++++++++ pandas/tests/window/test_api.py | 6 ++++++ pandas/tests/window/test_win_type.py | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 598cb68e0198c..fd3b3b87e1ee5 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -162,6 +162,7 @@ Deprecations - Deprecated keyword ``try_cast`` in :meth:`Series.where`, :meth:`Series.mask`, :meth:`DataFrame.where`, :meth:`DataFrame.mask`; cast results manually if desired (:issue:`38836`) - Deprecated comparison of :class:`Timestamp` object with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`) - Deprecated :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`) +- Deprecated :attr:`Rolling.is_datetimelike` (:issue:`38963`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index abeb63be47c82..a4612a4c8ed5d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -144,6 +144,15 @@ def win_type(self): return "freq" return self._win_type + @property + def is_datetimelike(self): + warnings.warn( + "is_datetimelike is deprecated and will be removed in a future version.", + FutureWarning, + stacklevel=2, + ) + return self._win_freq_i8 is not None + def validate(self) -> None: if self.center is not None and not is_bool(self.center): raise ValueError("center must be a boolean") diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index 52c629f96b713..7d3c29dc60be0 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -319,3 +319,9 @@ def test_multiple_agg_funcs(func, window_size, expected_vals): result = window.agg({"low": ["mean", "max"], "high": ["mean", "min"]}) tm.assert_frame_equal(result, expected) + + +def test_is_datetimelike_deprecated(): + s = Series(range(1)).rolling(1) + with tm.assert_produces_warning(FutureWarning): + assert not s.is_datetimelike diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index 784dc8d23264d..1cfba6f020018 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -140,7 +140,7 @@ def test_consistent_win_type_freq(arg): def test_win_type_freq_return_deprecation(): freq_roll = Series(range(2), index=date_range("2020", periods=2)).rolling("2s") with tm.assert_produces_warning(FutureWarning): - freq_roll.win_type + assert freq_roll.win_type == "freq" @td.skip_if_no_scipy From bf43e5717a16d75f2c7e0be8810a1cd82cc4342d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 5 Jan 2021 13:19:43 -0800 Subject: [PATCH 5/8] Filter warning when copying --- pandas/core/window/rolling.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index a4612a4c8ed5d..6e190cb67fc36 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -132,6 +132,11 @@ def __init__( ) self.validate() + # TODO: Remove once win_type deprecation is enforced + def _shallow_copy(self, obj, **kwargs): + warnings.simplefilter("ignore", FutureWarning) + super()._shallow_copy(obj, **kwargs) + @property def win_type(self): if self._win_freq_i8 is not None: From 1def4d9719a0a86bb8eb92f82db7b4423083cf76 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 5 Jan 2021 19:56:40 -0800 Subject: [PATCH 6/8] Add better warning handling --- pandas/core/window/rolling.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 6e190cb67fc36..57bca190bba63 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -134,8 +134,9 @@ def __init__( # TODO: Remove once win_type deprecation is enforced def _shallow_copy(self, obj, **kwargs): - warnings.simplefilter("ignore", FutureWarning) - super()._shallow_copy(obj, **kwargs) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "win_type", FutureWarning) + super()._shallow_copy(obj, **kwargs) @property def win_type(self): From 36e1ae7b0baea77da8928fe86e69a2a3b9512c36 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 5 Jan 2021 22:07:13 -0800 Subject: [PATCH 7/8] Move warning to lower level --- pandas/core/groupby/base.py | 6 +++++- pandas/core/window/rolling.py | 6 ------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 99426c55da29b..7534e7bfba5a1 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -5,6 +5,7 @@ """ import collections from typing import List +import warnings from pandas._typing import final @@ -27,7 +28,10 @@ def _shallow_copy(self, obj, **kwargs): obj = obj.obj for attr in self._attributes: if attr not in kwargs: - kwargs[attr] = getattr(self, attr) + # TODO: Remove once win_type deprecation is enforced + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "win_type", FutureWarning) + kwargs[attr] = getattr(self, attr) return self._constructor(obj, **kwargs) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 57bca190bba63..a4612a4c8ed5d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -132,12 +132,6 @@ def __init__( ) self.validate() - # TODO: Remove once win_type deprecation is enforced - def _shallow_copy(self, obj, **kwargs): - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", "win_type", FutureWarning) - super()._shallow_copy(obj, **kwargs) - @property def win_type(self): if self._win_freq_i8 is not None: From 10b8d6cdc669d229827933a4da8635109f286b5e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 5 Jan 2021 23:11:57 -0800 Subject: [PATCH 8/8] supress warning in groupby --- pandas/core/groupby/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 7534e7bfba5a1..594c5899209df 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -63,7 +63,10 @@ def _gotitem(self, key, ndim, subset=None): # we need to make a shallow copy of ourselves # with the same groupby - kwargs = {attr: getattr(self, attr) for attr in self._attributes} + # TODO: Remove once win_type deprecation is enforced + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "win_type", FutureWarning) + kwargs = {attr: getattr(self, attr) for attr in self._attributes} # Try to select from a DataFrame, falling back to a Series try: