Skip to content

DEPR: Rolling.win_type returning freq & is_datetimelike #38963

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 9 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ 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:`38963`)
Copy link
Member

Choose a reason for hiding this comment

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

small future note: since pd.Rolling does not exist, such a :attr:`Rolling.win_type` does not work

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah okay thanks for the note.

- Deprecated :attr:`Rolling.is_datetimelike` (:issue:`38963`)
-

.. ---------------------------------------------------------------------------
Expand Down
29 changes: 28 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -131,6 +132,32 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
warnings.simplefilter("ignore", FutureWarning)
super()._shallow_copy(obj, **kwargs)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "win_type", FutureWarning)
super()._shallow_copy(obj, **kwargs)

this should ensure the warning filter is only changed inside that context
this should ensure


@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

@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")
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/window/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion pandas/tests/window/test_win_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
assert freq_roll.win_type == "freq"


@td.skip_if_no_scipy
def test_win_type_not_implemented():
class CustomIndexer(BaseIndexer):
Expand Down