From a5871a2daf1f3f678675ee73e19d4b64df3d0a9d Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 1 Aug 2020 19:22:39 -0700 Subject: [PATCH 1/4] BUG: RollingGroupby respects __getitem__ --- doc/source/whatsnew/v1.1.1.rst | 4 ++++ pandas/core/window/rolling.py | 4 ++++ pandas/tests/window/test_grouper.py | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/doc/source/whatsnew/v1.1.1.rst b/doc/source/whatsnew/v1.1.1.rst index 443589308ad4c..4e3c6710684ac 100644 --- a/doc/source/whatsnew/v1.1.1.rst +++ b/doc/source/whatsnew/v1.1.1.rst @@ -44,6 +44,10 @@ Bug fixes - +**Groupby/resample/rolling** + +- Bug in :class:`pandas.core.groupby.RollingGroupby` where ``__getitem__`` would not select the specified column (:issue:`35486`) + .. --------------------------------------------------------------------------- .. _whatsnew_111.contributors: diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 445f179248226..87bcaa7d9512f 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -2220,6 +2220,10 @@ def _apply( def _constructor(self): return Rolling + @cache_readonly + def _selected_obj(self): + return self._groupby._selected_obj + def _create_blocks(self, obj: FrameOrSeries): """ Split data into blocks & return conformed data. diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index 744ca264e91d9..3799bbd2bb96b 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -214,3 +214,18 @@ def foo(x): name="value", ) tm.assert_series_equal(result, expected) + + def test_groupby_subselect_rolling(self): + # GH 35486 + df = DataFrame( + {"a": [1, 2, 3, 2], "b": [4.0, 2.0, 3.0, 1.0], "c": [10, 20, 30, 20]} + ) + result = df.groupby("a")[["b"]].rolling(2).max() + expected = DataFrame( + [np.nan, np.nan, 2.0, np.nan], + columns=["b"], + index=pd.MultiIndex.from_tuples( + ((1, 0), (2, 1), (2, 3), (3, 2)), names=["a", None] + ), + ) + tm.assert_frame_equal(result, expected) From 665667389085359942d2f9d68b3c1ee9e1412e6d Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 2 Aug 2020 10:27:56 -0700 Subject: [PATCH 2/4] Move to regression section --- doc/source/whatsnew/v1.1.1.rst | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.1.1.rst b/doc/source/whatsnew/v1.1.1.rst index 4e3c6710684ac..b0ba83804c7e6 100644 --- a/doc/source/whatsnew/v1.1.1.rst +++ b/doc/source/whatsnew/v1.1.1.rst @@ -15,7 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- Bug in :class:`pandas.core.groupby.RollingGroupby` where ``__getitem__`` would not select the specified column (:issue:`35486`) - - @@ -44,10 +44,6 @@ Bug fixes - -**Groupby/resample/rolling** - -- Bug in :class:`pandas.core.groupby.RollingGroupby` where ``__getitem__`` would not select the specified column (:issue:`35486`) - .. --------------------------------------------------------------------------- .. _whatsnew_111.contributors: From 9331b1e34b0121d20c42fd2b809ba34752e6a6c4 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Mon, 3 Aug 2020 11:31:52 -0700 Subject: [PATCH 3/4] Clarify whatsnew --- doc/source/whatsnew/v1.1.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.1.rst b/doc/source/whatsnew/v1.1.1.rst index b0ba83804c7e6..3cd00e10e031e 100644 --- a/doc/source/whatsnew/v1.1.1.rst +++ b/doc/source/whatsnew/v1.1.1.rst @@ -15,7 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- Bug in :class:`pandas.core.groupby.RollingGroupby` where ``__getitem__`` would not select the specified column (:issue:`35486`) +- Bug in :class:`pandas.core.groupby.RollingGroupby` where column selection was ignored (:issue:`35486`) - - From d53bba7304b0dea610a84239c5b59eeb8d0ef406 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Mon, 3 Aug 2020 22:13:42 -0700 Subject: [PATCH 4/4] Add test for selecting series --- pandas/tests/window/test_grouper.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index 3799bbd2bb96b..ca5a9eccea4f5 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -229,3 +229,13 @@ def test_groupby_subselect_rolling(self): ), ) tm.assert_frame_equal(result, expected) + + result = df.groupby("a")["b"].rolling(2).max() + expected = Series( + [np.nan, np.nan, 2.0, np.nan], + index=pd.MultiIndex.from_tuples( + ((1, 0), (2, 1), (2, 3), (3, 2)), names=["a", None] + ), + name="b", + ) + tm.assert_series_equal(result, expected)