Skip to content

Commit 6875a05

Browse files
authored
BUG: RollingGroupby with closed and column selection no longer raises ValueError (#35639)
1 parent aefae55 commit 6875a05

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
@@ -2212,7 +2212,7 @@ def _apply(
22122212
# Cannot use _wrap_outputs because we calculate the result all at once
22132213
# Compose MultiIndex result from grouping levels then rolling level
22142214
# Aggregate the MultiIndex data as tuples then the level names
2215-
grouped_object_index = self._groupby._selected_obj.index
2215+
grouped_object_index = self.obj.index
22162216
grouped_index_name = [grouped_object_index.name]
22172217
groupby_keys = [grouping.name for grouping in self._groupby.grouper._groupings]
22182218
result_index_names = groupby_keys + grouped_index_name
@@ -2236,10 +2236,6 @@ def _apply(
22362236
def _constructor(self):
22372237
return Rolling
22382238

2239-
@cache_readonly
2240-
def _selected_obj(self):
2241-
return self._groupby._selected_obj
2242-
22432239
def _create_blocks(self, obj: FrameOrSeries):
22442240
"""
22452241
Split data into blocks & return conformed data.
@@ -2278,7 +2274,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer:
22782274
rolling_indexer: Union[Type[FixedWindowIndexer], Type[VariableWindowIndexer]]
22792275
if self.is_freq_type:
22802276
rolling_indexer = VariableWindowIndexer
2281-
index_array = self._groupby._selected_obj.index.asi8
2277+
index_array = self.obj.index.asi8
22822278
else:
22832279
rolling_indexer = FixedWindowIndexer
22842280
index_array = None
@@ -2295,7 +2291,7 @@ def _gotitem(self, key, ndim, subset=None):
22952291
# here so our index is carried thru to the selected obj
22962292
# when we do the splitting for the groupby
22972293
if self.on is not None:
2298-
self._groupby.obj = self._groupby.obj.set_index(self._on)
2294+
self.obj = self.obj.set_index(self._on)
22992295
self.on = None
23002296
return super()._gotitem(key, ndim, subset=subset)
23012297

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)