Skip to content
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

DEPR/CLN: Remove how keyword from df.rolling() etc. #18668

Merged
merged 1 commit into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 0 additions & 6 deletions doc/source/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ accept the following arguments:
result is NA)
- ``center``: boolean, whether to set the labels at the center (default is False)

.. warning::

The ``freq`` and ``how`` arguments were in the API prior to 0.18.0 changes. These are deprecated in the new API. You can simply resample the input prior to creating a window function.

For example, instead of ``s.rolling(window=5,freq='D').max()`` to get the max value on a rolling 5 Day window, one could use ``s.resample('D').max().rolling(window=5).max()``, which first resamples the data to daily data, then provides a rolling 5 day window.

We can then call methods on these ``rolling`` objects. These return like-indexed objects:

.. ipython:: python
Expand Down
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ Removal of prior version deprecations/changes
- The ``SparseList`` class has been removed (:issue:`14007`)
- The ``pandas.io.wb`` and ``pandas.io.data`` stub modules have been removed (:issue:`13735`)
- ``Categorical.from_array`` has been removed (:issue:`13854`)
- The ``freq`` parameter has been removed from the ``rolling``/``expanding``/``ewm`` methods of DataFrame
and Series (deprecated since v0.18). Instead, resample before calling the methods. (:issue:18601)
- The ``freq`` and ``how`` parameters have been removed from the ``rolling``/``expanding``/``ewm`` methods of DataFrame
and Series (deprecated since v0.18). Instead, resample before calling the methods. (:issue:18601 & :issue:18668)
Copy link
Member

Choose a reason for hiding this comment

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

@topper-123 small note for future reference: the issue references need some backticks (see eg line above)

- ``DatetimeIndex.to_datetime``, ``Timestamp.to_datetime``, ``PeriodIndex.to_datetime``, and ``Index.to_datetime`` have been removed (:issue:`8254`, :issue:`14096`, :issue:`14113`)

.. _whatsnew_0220.performance:
Expand Down
82 changes: 23 additions & 59 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ def validate(self):
raise ValueError("closed must be 'right', 'left', 'both' or "
"'neither'")

def _convert_freq(self, how=None):
def _convert_freq(self):
""" resample according to the how, return a new object """

obj = self._selected_obj
index = None
return obj, index

def _create_blocks(self, how):
def _create_blocks(self):
""" split data into blocks & return conformed data """

obj, index = self._convert_freq(how)
obj, index = self._convert_freq()
if index is not None:
index = self._on

Expand Down Expand Up @@ -196,7 +196,7 @@ def _get_index(self, index=None):
return index, index.asi8
return index, index

def _prep_values(self, values=None, kill_inf=True, how=None):
def _prep_values(self, values=None, kill_inf=True):

if values is None:
values = getattr(self._selected_obj, 'values', self._selected_obj)
Expand Down Expand Up @@ -320,22 +320,10 @@ def aggregate(self, arg, *args, **kwargs):
agg = aggregate

_shared_docs['sum'] = dedent("""
%(name)s sum

Parameters
----------
how : string, default None
.. deprecated:: 0.18.0
Method for down- or re-sampling""")
%(name)s sum""")

_shared_docs['mean'] = dedent("""
%(name)s mean

Parameters
----------
how : string, default None
.. deprecated:: 0.18.0
Method for down- or re-sampling""")
%(name)s mean""")


class Window(_Window):
Expand Down Expand Up @@ -549,17 +537,14 @@ def _pop_args(win_type, arg_names, kwargs):
# GH #15662. `False` makes symmetric window, rather than periodic.
return sig.get_window(win_type, window, False).astype(float)

def _apply_window(self, mean=True, how=None, **kwargs):
def _apply_window(self, mean=True, **kwargs):
"""
Applies a moving window of type ``window_type`` on the data.

Parameters
----------
mean : boolean, default True
If True computes weighted mean, else weighted sum
how : string, default to None
.. deprecated:: 0.18.0
how to resample

Returns
-------
Expand All @@ -569,7 +554,7 @@ def _apply_window(self, mean=True, how=None, **kwargs):
window = self._prep_window(**kwargs)
center = self.center

blocks, obj, index = self._create_blocks(how=how)
blocks, obj, index = self._create_blocks()
results = []
for b in blocks:
try:
Expand Down Expand Up @@ -686,7 +671,7 @@ def __init__(self, obj, *args, **kwargs):
cov = GroupByMixin._dispatch('cov', other=None, pairwise=None)

def _apply(self, func, name, window=None, center=None,
check_minp=None, how=None, **kwargs):
check_minp=None, **kwargs):
"""
dispatch to apply; we are stripping all of the _apply kwargs and
performing the original function call on the grouped object
Expand All @@ -710,7 +695,7 @@ def _constructor(self):
return Rolling

def _apply(self, func, name=None, window=None, center=None,
check_minp=None, how=None, **kwargs):
check_minp=None, **kwargs):
"""
Rolling statistical measure using supplied function. Designed to be
used with passed-in Cython array-based functions.
Expand All @@ -723,9 +708,6 @@ def _apply(self, func, name=None, window=None, center=None,
window : int/array, default to _get_window()
center : boolean, default to self.center
check_minp : function, default to _use_window
how : string, default to None
.. deprecated:: 0.18.0
how to resample

Returns
-------
Expand All @@ -739,7 +721,7 @@ def _apply(self, func, name=None, window=None, center=None,
if check_minp is None:
check_minp = _use_window

blocks, obj, index = self._create_blocks(how=how)
blocks, obj, index = self._create_blocks()
index, indexi = self._get_index(index=index)
results = []
for b in blocks:
Expand Down Expand Up @@ -803,7 +785,7 @@ class _Rolling_and_Expanding(_Rolling):

def count(self):

blocks, obj, index = self._create_blocks(how=None)
blocks, obj, index = self._create_blocks()
index, indexi = self._get_index(index=index)

window = self._get_window()
Expand Down Expand Up @@ -849,45 +831,30 @@ def sum(self, *args, **kwargs):

_shared_docs['max'] = dedent("""
%(name)s maximum
""")

Parameters
----------
how : string, default 'max'
.. deprecated:: 0.18.0
Method for down- or re-sampling""")

def max(self, how=None, *args, **kwargs):
def max(self, *args, **kwargs):
nv.validate_window_func('max', args, kwargs)
return self._apply('roll_max', 'max', how=how, **kwargs)
return self._apply('roll_max', 'max', **kwargs)

_shared_docs['min'] = dedent("""
%(name)s minimum
""")

Parameters
----------
how : string, default 'min'
.. deprecated:: 0.18.0
Method for down- or re-sampling""")

def min(self, how=None, *args, **kwargs):
def min(self, *args, **kwargs):
nv.validate_window_func('min', args, kwargs)
return self._apply('roll_min', 'min', how=how, **kwargs)
return self._apply('roll_min', 'min', **kwargs)

def mean(self, *args, **kwargs):
nv.validate_window_func('mean', args, kwargs)
return self._apply('roll_mean', 'mean', **kwargs)

_shared_docs['median'] = dedent("""
%(name)s median
""")

Parameters
----------
how : string, default 'median'
.. deprecated:: 0.18.0
Method for down- or re-sampling""")

def median(self, how=None, **kwargs):
return self._apply('roll_median_c', 'median', how=how, **kwargs)
def median(self, **kwargs):
return self._apply('roll_median_c', 'median', **kwargs)

_shared_docs['std'] = dedent("""
%(name)s standard deviation
Expand Down Expand Up @@ -1709,23 +1676,20 @@ def aggregate(self, arg, *args, **kwargs):

agg = aggregate

def _apply(self, func, how=None, **kwargs):
def _apply(self, func, **kwargs):
"""Rolling statistical measure using supplied function. Designed to be
used with passed-in Cython array-based functions.

Parameters
----------
func : string/callable to apply
how : string, default to None
.. deprecated:: 0.18.0
how to resample

Returns
-------
y : type of input argument

"""
blocks, obj, index = self._create_blocks(how=how)
blocks, obj, index = self._create_blocks()
results = []
for b in blocks:
try:
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ def test_rolling_max_gh6297(self):
x = series.resample('D').max().rolling(window=1).max()
tm.assert_series_equal(expected, x)

def test_rolling_max_how_resample(self):
def test_rolling_max_resample(self):

indices = [datetime(1975, 1, i) for i in range(1, 6)]
# So that we can have 3 datapoints on last day (4, 10, and 20)
Expand All @@ -3040,17 +3040,17 @@ def test_rolling_max_how_resample(self):
# Now specify median (10.0)
expected = Series([0.0, 1.0, 2.0, 3.0, 10.0],
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
x = series.resample('D').median().rolling(window=1).max(how='median')
x = series.resample('D').median().rolling(window=1).max()
tm.assert_series_equal(expected, x)

# Now specify mean (4+10+20)/3
v = (4.0 + 10.0 + 20.0) / 3.0
expected = Series([0.0, 1.0, 2.0, 3.0, v],
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
x = series.resample('D').mean().rolling(window=1).max(how='mean')
x = series.resample('D').mean().rolling(window=1).max()
tm.assert_series_equal(expected, x)

def test_rolling_min_how_resample(self):
def test_rolling_min_resample(self):

indices = [datetime(1975, 1, i) for i in range(1, 6)]
# So that we can have 3 datapoints on last day (4, 10, and 20)
Expand All @@ -3068,7 +3068,7 @@ def test_rolling_min_how_resample(self):
r = series.resample('D').min().rolling(window=1)
tm.assert_series_equal(expected, r.min())

def test_rolling_median_how_resample(self):
def test_rolling_median_resample(self):

indices = [datetime(1975, 1, i) for i in range(1, 6)]
# So that we can have 3 datapoints on last day (4, 10, and 20)
Expand Down