Skip to content

Commit de28255

Browse files
mroeschkejreback
authored andcommitted
DEPR: Change raw kwarg in rolling/expanding.apply to False (#29829)
1 parent 00b1d34 commit de28255

File tree

4 files changed

+12
-34
lines changed

4 files changed

+12
-34
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
450450
- In :func:`concat` the default value for ``sort`` has been changed from ``None`` to ``False`` (:issue:`20613`)
451451
- Removed previously deprecated "raise_conflict" argument from :meth:`DataFrame.update`, use "errors" instead (:issue:`23585`)
452452
- Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`)
453+
- Changed the default value for the `raw` argument in :func:`Series.rolling().apply() <pandas.core.window.Rolling.apply>`, :func:`DataFrame.rolling().apply() <pandas.core.window.Rolling.apply>`,
454+
- :func:`Series.expanding().apply() <pandas.core.window.Expanding.apply>`, and :func:`DataFrame.expanding().apply() <pandas.core.window.Expanding.apply>` to ``False`` (:issue:`20584`)
453455
-
454456

455457
.. _whatsnew_1000.performance:

pandas/core/window/expanding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def count(self, **kwargs):
148148

149149
@Substitution(name="expanding")
150150
@Appender(_shared_docs["apply"])
151-
def apply(self, func, raw=None, args=(), kwargs={}):
151+
def apply(self, func, raw=False, args=(), kwargs={}):
152152
return super().apply(func, raw=raw, args=args, kwargs=kwargs)
153153

154154
@Substitution(name="expanding")

pandas/core/window/rolling.py

+5-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from functools import partial
77
from textwrap import dedent
88
from typing import Callable, Dict, List, Optional, Set, Tuple, Union
9-
import warnings
109

1110
import numpy as np
1211

@@ -1190,15 +1189,11 @@ def count(self):
11901189
raw : bool, default None
11911190
* ``False`` : passes each row or column as a Series to the
11921191
function.
1193-
* ``True`` or ``None`` : the passed function will receive ndarray
1192+
* ``True`` : the passed function will receive ndarray
11941193
objects instead.
11951194
If you are just applying a NumPy reduction function this will
11961195
achieve much better performance.
11971196
1198-
The `raw` parameter is required and will show a FutureWarning if
1199-
not passed. In the future `raw` will default to False.
1200-
1201-
.. versionadded:: 0.23.0
12021197
*args, **kwargs
12031198
Arguments and keyword arguments to be passed into func.
12041199
@@ -1214,27 +1209,15 @@ def count(self):
12141209
"""
12151210
)
12161211

1217-
def apply(self, func, raw=None, args=(), kwargs={}):
1212+
def apply(self, func, raw=False, args=(), kwargs={}):
12181213
from pandas import Series
12191214

12201215
kwargs.pop("_level", None)
12211216
kwargs.pop("floor", None)
12221217
window = self._get_window()
12231218
offset = _offset(window, self.center)
1224-
1225-
# TODO: default is for backward compat
1226-
# change to False in the future
1227-
if raw is None:
1228-
warnings.warn(
1229-
"Currently, 'apply' passes the values as ndarrays to the "
1230-
"applied function. In the future, this will change to passing "
1231-
"it as Series objects. You need to specify 'raw=True' to keep "
1232-
"the current behaviour, and you can pass 'raw=False' to "
1233-
"silence this warning",
1234-
FutureWarning,
1235-
stacklevel=3,
1236-
)
1237-
raw = True
1219+
if not is_bool(raw):
1220+
raise ValueError("raw parameter must be `True` or `False`")
12381221

12391222
window_func = partial(
12401223
self._get_cython_func_type("roll_generic"),
@@ -1898,7 +1881,7 @@ def count(self):
18981881

18991882
@Substitution(name="rolling")
19001883
@Appender(_shared_docs["apply"])
1901-
def apply(self, func, raw=None, args=(), kwargs={}):
1884+
def apply(self, func, raw=False, args=(), kwargs={}):
19021885
return super().apply(func, raw=raw, args=args, kwargs=kwargs)
19031886

19041887
@Substitution(name="rolling")

pandas/tests/window/test_moments.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -687,17 +687,10 @@ def f(x):
687687
result = s.rolling(2, min_periods=0).apply(len, raw=raw)
688688
tm.assert_series_equal(result, expected)
689689

690-
@pytest.mark.parametrize("klass", [Series, DataFrame])
691-
@pytest.mark.parametrize(
692-
"method", [lambda x: x.rolling(window=2), lambda x: x.expanding()]
693-
)
694-
def test_apply_future_warning(self, klass, method):
695-
696-
# gh-5071
697-
s = klass(np.arange(3))
698-
699-
with tm.assert_produces_warning(FutureWarning):
700-
method(s).apply(lambda x: len(x))
690+
@pytest.mark.parametrize("bad_raw", [None, 1, 0])
691+
def test_rolling_apply_invalid_raw(self, bad_raw):
692+
with pytest.raises(ValueError, match="raw parameter must be `True` or `False`"):
693+
Series(range(3)).rolling(1).apply(len, raw=bad_raw)
701694

702695
def test_rolling_apply_out_of_bounds(self, raw):
703696
# gh-1850

0 commit comments

Comments
 (0)