Skip to content

BUG: RollingGroupby respects __getitem__ #35513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~

-
- Bug in :class:`pandas.core.groupby.RollingGroupby` where column selection was ignored (:issue:`35486`)
-
-

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/window/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also test a df.groupby('a')['b'] as well

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)