Skip to content

Commit a057e74

Browse files
Backport PR #35639: BUG: RollingGroupby with closed and column selection no longer raises ValueError (#35651)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 396ba93 commit a057e74

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

doc/source/whatsnew/v1.1.1.rst

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Categorical
5151
-
5252
-
5353

54+
**Groupby/resample/rolling**
55+
56+
- Bug in :class:`pandas.core.groupby.RollingGroupby` where passing ``closed`` with column selection would raise a ``ValueError`` (:issue:`35549`)
57+
5458
**Plotting**
5559

5660
-

pandas/core/window/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, obj, *args, **kwargs):
5252
kwargs.pop("parent", None)
5353
groupby = kwargs.pop("groupby", None)
5454
if groupby is None:
55-
groupby, obj = obj, obj.obj
55+
groupby, obj = obj, obj._selected_obj
5656
self._groupby = groupby
5757
self._groupby.mutated = True
5858
self._groupby.grouper.mutated = True

pandas/core/window/rolling.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ def _apply(
21962196
# Cannot use _wrap_outputs because we calculate the result all at once
21972197
# Compose MultiIndex result from grouping levels then rolling level
21982198
# Aggregate the MultiIndex data as tuples then the level names
2199-
grouped_object_index = self._groupby._selected_obj.index
2199+
grouped_object_index = self.obj.index
22002200
grouped_index_name = [grouped_object_index.name]
22012201
groupby_keys = [grouping.name for grouping in self._groupby.grouper._groupings]
22022202
result_index_names = groupby_keys + grouped_index_name
@@ -2220,10 +2220,6 @@ 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-
22272223
def _create_blocks(self, obj: FrameOrSeries):
22282224
"""
22292225
Split data into blocks & return conformed data.
@@ -2262,7 +2258,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer:
22622258
rolling_indexer: Union[Type[FixedWindowIndexer], Type[VariableWindowIndexer]]
22632259
if self.is_freq_type:
22642260
rolling_indexer = VariableWindowIndexer
2265-
index_array = self._groupby._selected_obj.index.asi8
2261+
index_array = self.obj.index.asi8
22662262
else:
22672263
rolling_indexer = FixedWindowIndexer
22682264
index_array = None
@@ -2279,7 +2275,7 @@ def _gotitem(self, key, ndim, subset=None):
22792275
# here so our index is carried thru to the selected obj
22802276
# when we do the splitting for the groupby
22812277
if self.on is not None:
2282-
self._groupby.obj = self._groupby.obj.set_index(self._on)
2278+
self.obj = self.obj.set_index(self._on)
22832279
self.on = None
22842280
return super()._gotitem(key, ndim, subset=subset)
22852281

pandas/tests/window/test_grouper.py

+51
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,54 @@ def test_groupby_subselect_rolling(self):
304304
name="b",
305305
)
306306
tm.assert_series_equal(result, expected)
307+
308+
def test_groupby_rolling_subset_with_closed(self):
309+
# GH 35549
310+
df = pd.DataFrame(
311+
{
312+
"column1": range(6),
313+
"column2": range(6),
314+
"group": 3 * ["A", "B"],
315+
"date": [pd.Timestamp("2019-01-01")] * 6,
316+
}
317+
)
318+
result = (
319+
df.groupby("group").rolling("1D", on="date", closed="left")["column1"].sum()
320+
)
321+
expected = Series(
322+
[np.nan, 0.0, 2.0, np.nan, 1.0, 4.0],
323+
index=pd.MultiIndex.from_tuples(
324+
[("A", pd.Timestamp("2019-01-01"))] * 3
325+
+ [("B", pd.Timestamp("2019-01-01"))] * 3,
326+
names=["group", "date"],
327+
),
328+
name="column1",
329+
)
330+
tm.assert_series_equal(result, expected)
331+
332+
def test_groupby_subset_rolling_subset_with_closed(self):
333+
# GH 35549
334+
df = pd.DataFrame(
335+
{
336+
"column1": range(6),
337+
"column2": range(6),
338+
"group": 3 * ["A", "B"],
339+
"date": [pd.Timestamp("2019-01-01")] * 6,
340+
}
341+
)
342+
343+
result = (
344+
df.groupby("group")[["column1", "date"]]
345+
.rolling("1D", on="date", closed="left")["column1"]
346+
.sum()
347+
)
348+
expected = Series(
349+
[np.nan, 0.0, 2.0, np.nan, 1.0, 4.0],
350+
index=pd.MultiIndex.from_tuples(
351+
[("A", pd.Timestamp("2019-01-01"))] * 3
352+
+ [("B", pd.Timestamp("2019-01-01"))] * 3,
353+
names=["group", "date"],
354+
),
355+
name="column1",
356+
)
357+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)