Skip to content

Commit a48cc4d

Browse files
Backport PR #35513: BUG: RollingGroupby respects __getitem__ (#35591)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent bea9af8 commit a48cc4d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

doc/source/whatsnew/v1.1.1.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717

1818
- Fixed regression where :func:`read_csv` would raise a ``ValueError`` when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`35493`).
19-
-
20-
-
19+
- Fixed regression in :class:`pandas.core.groupby.RollingGroupby` where column selection was ignored (:issue:`35486`)
2120

2221
.. ---------------------------------------------------------------------------
2322

pandas/core/window/rolling.py

+4
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,10 @@ def _apply(
22202220
def _constructor(self):
22212221
return Rolling
22222222

2223+
@cache_readonly
2224+
def _selected_obj(self):
2225+
return self._groupby._selected_obj
2226+
22232227
def _create_blocks(self, obj: FrameOrSeries):
22242228
"""
22252229
Split data into blocks & return conformed data.

pandas/tests/window/test_grouper.py

+25
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,28 @@ def foo(x):
214214
name="value",
215215
)
216216
tm.assert_series_equal(result, expected)
217+
218+
def test_groupby_subselect_rolling(self):
219+
# GH 35486
220+
df = DataFrame(
221+
{"a": [1, 2, 3, 2], "b": [4.0, 2.0, 3.0, 1.0], "c": [10, 20, 30, 20]}
222+
)
223+
result = df.groupby("a")[["b"]].rolling(2).max()
224+
expected = DataFrame(
225+
[np.nan, np.nan, 2.0, np.nan],
226+
columns=["b"],
227+
index=pd.MultiIndex.from_tuples(
228+
((1, 0), (2, 1), (2, 3), (3, 2)), names=["a", None]
229+
),
230+
)
231+
tm.assert_frame_equal(result, expected)
232+
233+
result = df.groupby("a")["b"].rolling(2).max()
234+
expected = Series(
235+
[np.nan, np.nan, 2.0, np.nan],
236+
index=pd.MultiIndex.from_tuples(
237+
((1, 0), (2, 1), (2, 3), (3, 2)), names=["a", None]
238+
),
239+
name="b",
240+
)
241+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)