From 057726f1ed7f60c59db7ea6f4971e6c65f0fc90c Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 9 Feb 2020 21:30:38 -0800 Subject: [PATCH 1/3] BUG: Fix raw parameter not being respected in groupby.rolling.raw --- doc/source/whatsnew/v1.0.2.rst | 3 ++- pandas/core/window/rolling.py | 3 ++- pandas/tests/window/test_grouper.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index b055b44274bd8..d3b92672de696 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -18,7 +18,8 @@ Fixed regressions - Fixed regression in :meth:`DataFrame.to_excel` when ``columns`` kwarg is passed (:issue:`31677`) - Fixed regression in :meth:`Series.align` when ``other`` is a DataFrame and ``method`` is not None (:issue:`31785`) - - +- Fixed regression in :meth:`pandas.core.groupby.RollingGroupby.apply` where the ``raw`` parameter was ignored (:issue:`31754`) +- .. --------------------------------------------------------------------------- .. _whatsnew_102.bug_fixes: diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 580c7cc0554d0..8506b2ff6ee9e 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1296,13 +1296,14 @@ def apply( raise ValueError("engine must be either 'numba' or 'cython'") # TODO: Why do we always pass center=False? - # name=func for WindowGroupByMixin._apply + # name=func & raw=raw for WindowGroupByMixin._apply return self._apply( apply_func, center=False, floor=0, name=func, use_numba_cache=engine == "numba", + raw=raw, ) def _generate_cython_apply_func(self, args, kwargs, raw, offset, func): diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index 355ef3a90d424..5b2687271f9d6 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -190,3 +190,21 @@ def test_expanding_apply(self, raw): result = r.apply(lambda x: x.sum(), raw=raw) expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw)) tm.assert_frame_equal(result, expected) + + @pytest.mark.parametrize("expected_value,raw_value", [[1.0, True], [0.0, False]]) + def test_groupby_rolling(self, expected_value, raw_value): + # GH 31754 + + def foo(x): + return int(isinstance(x, np.ndarray)) + + df = pd.DataFrame({"id": [1, 1, 1], "value": [1, 2, 3]}) + result = df.groupby("id").value.rolling(1).apply(foo, raw=raw_value) + expected = Series( + [expected_value] * 3, + index=pd.MultiIndex.from_tuples( + ((1, 0), (1, 1), (1, 2)), names=["id", None] + ), + name="value", + ) + tm.assert_series_equal(result, expected) From 14edbecccd4f17be6bf206403fea0c2136f0fade Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 9 Feb 2020 22:10:18 -0800 Subject: [PATCH 2/3] Fix whatsnew note --- doc/source/whatsnew/v1.0.2.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index d3b92672de696..ac0509778b6b6 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -17,7 +17,6 @@ Fixed regressions - Fixed regression in :meth:`DataFrame.to_excel` when ``columns`` kwarg is passed (:issue:`31677`) - Fixed regression in :meth:`Series.align` when ``other`` is a DataFrame and ``method`` is not None (:issue:`31785`) -- - Fixed regression in :meth:`pandas.core.groupby.RollingGroupby.apply` where the ``raw`` parameter was ignored (:issue:`31754`) - .. --------------------------------------------------------------------------- From 3d744fa633eadc4ffb487fb880466fdd5c90a5ac Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 9 Feb 2020 22:58:32 -0800 Subject: [PATCH 3/3] Add blank line --- doc/source/whatsnew/v1.0.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index ac0509778b6b6..cf33b2f8de3f3 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -19,6 +19,7 @@ Fixed regressions - Fixed regression in :meth:`Series.align` when ``other`` is a DataFrame and ``method`` is not None (:issue:`31785`) - Fixed regression in :meth:`pandas.core.groupby.RollingGroupby.apply` where the ``raw`` parameter was ignored (:issue:`31754`) - + .. --------------------------------------------------------------------------- .. _whatsnew_102.bug_fixes: