Skip to content

Commit 5e38842

Browse files
Backport PR pandas-dev#31842: BUG: Fix raw parameter not being respected in groupby.rolling.apply (pandas-dev#32093)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent e3044d5 commit 5e38842

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717

1818
- Fixed regression in :meth:`DataFrame.to_excel` when ``columns`` kwarg is passed (:issue:`31677`)
1919
- Fixed regression in :meth:`Series.align` when ``other`` is a DataFrame and ``method`` is not None (:issue:`31785`)
20+
- Fixed regression in :meth:`pandas.core.groupby.RollingGroupby.apply` where the ``raw`` parameter was ignored (:issue:`31754`)
2021
-
2122

2223
.. ---------------------------------------------------------------------------

pandas/core/window/rolling.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,14 @@ def apply(
12961296
raise ValueError("engine must be either 'numba' or 'cython'")
12971297

12981298
# TODO: Why do we always pass center=False?
1299-
# name=func for WindowGroupByMixin._apply
1299+
# name=func & raw=raw for WindowGroupByMixin._apply
13001300
return self._apply(
13011301
apply_func,
13021302
center=False,
13031303
floor=0,
13041304
name=func,
13051305
use_numba_cache=engine == "numba",
1306+
raw=raw,
13061307
)
13071308

13081309
def _generate_cython_apply_func(self, args, kwargs, raw, offset, func):

pandas/tests/window/test_grouper.py

+18
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,21 @@ def test_expanding_apply(self, raw):
190190
result = r.apply(lambda x: x.sum(), raw=raw)
191191
expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw))
192192
tm.assert_frame_equal(result, expected)
193+
194+
@pytest.mark.parametrize("expected_value,raw_value", [[1.0, True], [0.0, False]])
195+
def test_groupby_rolling(self, expected_value, raw_value):
196+
# GH 31754
197+
198+
def foo(x):
199+
return int(isinstance(x, np.ndarray))
200+
201+
df = pd.DataFrame({"id": [1, 1, 1], "value": [1, 2, 3]})
202+
result = df.groupby("id").value.rolling(1).apply(foo, raw=raw_value)
203+
expected = Series(
204+
[expected_value] * 3,
205+
index=pd.MultiIndex.from_tuples(
206+
((1, 0), (1, 1), (1, 2)), names=["id", None]
207+
),
208+
name="value",
209+
)
210+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)