diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 869faef8da33c..48808a7ef7a46 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -450,6 +450,8 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - In :func:`concat` the default value for ``sort`` has been changed from ``None`` to ``False`` (:issue:`20613`) - Removed previously deprecated "raise_conflict" argument from :meth:`DataFrame.update`, use "errors" instead (:issue:`23585`) - Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`) +- Changed the default value for the `raw` argument in :func:`Series.rolling().apply() `, :func:`DataFrame.rolling().apply() `, +- :func:`Series.expanding().apply() `, and :func:`DataFrame.expanding().apply() ` to ``False`` (:issue:`20584`) - .. _whatsnew_1000.performance: diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index f7673f5685ba0..2e527b90249c9 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -148,7 +148,7 @@ def count(self, **kwargs): @Substitution(name="expanding") @Appender(_shared_docs["apply"]) - def apply(self, func, raw=None, args=(), kwargs={}): + def apply(self, func, raw=False, args=(), kwargs={}): return super().apply(func, raw=raw, args=args, kwargs=kwargs) @Substitution(name="expanding") diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 2f37ba9b8f725..7f3404100f71c 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -6,7 +6,6 @@ from functools import partial from textwrap import dedent from typing import Callable, Dict, List, Optional, Set, Tuple, Union -import warnings import numpy as np @@ -1190,15 +1189,11 @@ def count(self): raw : bool, default None * ``False`` : passes each row or column as a Series to the function. - * ``True`` or ``None`` : the passed function will receive ndarray + * ``True`` : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance. - The `raw` parameter is required and will show a FutureWarning if - not passed. In the future `raw` will default to False. - - .. versionadded:: 0.23.0 *args, **kwargs Arguments and keyword arguments to be passed into func. @@ -1214,27 +1209,15 @@ def count(self): """ ) - def apply(self, func, raw=None, args=(), kwargs={}): + def apply(self, func, raw=False, args=(), kwargs={}): from pandas import Series kwargs.pop("_level", None) kwargs.pop("floor", None) window = self._get_window() offset = _offset(window, self.center) - - # TODO: default is for backward compat - # change to False in the future - if raw is None: - warnings.warn( - "Currently, 'apply' passes the values as ndarrays to the " - "applied function. In the future, this will change to passing " - "it as Series objects. You need to specify 'raw=True' to keep " - "the current behaviour, and you can pass 'raw=False' to " - "silence this warning", - FutureWarning, - stacklevel=3, - ) - raw = True + if not is_bool(raw): + raise ValueError("raw parameter must be `True` or `False`") window_func = partial( self._get_cython_func_type("roll_generic"), @@ -1898,7 +1881,7 @@ def count(self): @Substitution(name="rolling") @Appender(_shared_docs["apply"]) - def apply(self, func, raw=None, args=(), kwargs={}): + def apply(self, func, raw=False, args=(), kwargs={}): return super().apply(func, raw=raw, args=args, kwargs=kwargs) @Substitution(name="rolling") diff --git a/pandas/tests/window/test_moments.py b/pandas/tests/window/test_moments.py index 6e4bc621d7f49..f1c89d3c6c1b4 100644 --- a/pandas/tests/window/test_moments.py +++ b/pandas/tests/window/test_moments.py @@ -687,17 +687,10 @@ def f(x): result = s.rolling(2, min_periods=0).apply(len, raw=raw) tm.assert_series_equal(result, expected) - @pytest.mark.parametrize("klass", [Series, DataFrame]) - @pytest.mark.parametrize( - "method", [lambda x: x.rolling(window=2), lambda x: x.expanding()] - ) - def test_apply_future_warning(self, klass, method): - - # gh-5071 - s = klass(np.arange(3)) - - with tm.assert_produces_warning(FutureWarning): - method(s).apply(lambda x: len(x)) + @pytest.mark.parametrize("bad_raw", [None, 1, 0]) + def test_rolling_apply_invalid_raw(self, bad_raw): + with pytest.raises(ValueError, match="raw parameter must be `True` or `False`"): + Series(range(3)).rolling(1).apply(len, raw=bad_raw) def test_rolling_apply_out_of_bounds(self, raw): # gh-1850