Skip to content

Commit 0e52a68

Browse files
phoflsimonjayhawkins
authored andcommitted
Backport PR pandas-dev#36753 on branch 1.1.x: BUG: Segfault with string Index when using Rolling after Groupby
1 parent cf4974d commit 0e52a68

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pandas/core/window/rolling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ def _wrap_results(self, results, blocks, obj, exclude=None) -> FrameOrSeries:
409409
if self.on is not None and not self._on.equals(obj.index):
410410

411411
name = self._on.name
412-
final.append(Series(self._on, index=obj.index, name=name))
412+
final.append(Series(self._on, index=self.obj.index, name=name))
413413

414414
if self._selection is not None:
415415

@@ -2259,7 +2259,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer:
22592259
"""
22602260
rolling_indexer: Type[BaseIndexer]
22612261
indexer_kwargs: Optional[Dict] = None
2262-
index_array = self.obj.index.asi8
2262+
index_array = self._on.asi8
22632263
if isinstance(self.window, BaseIndexer):
22642264
rolling_indexer = type(self.window)
22652265
indexer_kwargs = self.window.__dict__

pandas/tests/window/test_grouper.py

+29
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,32 @@ def test_groupby_rolling_empty_frame(self):
417417
result = expected.groupby(["s1", "s2"]).rolling(window=1).sum()
418418
expected.index = pd.MultiIndex.from_tuples([], names=["s1", "s2", None])
419419
tm.assert_frame_equal(result, expected)
420+
421+
def test_groupby_rolling_string_index(self):
422+
# GH: 36727
423+
df = pd.DataFrame(
424+
[
425+
["A", "group_1", pd.Timestamp(2019, 1, 1, 9)],
426+
["B", "group_1", pd.Timestamp(2019, 1, 2, 9)],
427+
["Z", "group_2", pd.Timestamp(2019, 1, 3, 9)],
428+
["H", "group_1", pd.Timestamp(2019, 1, 6, 9)],
429+
["E", "group_2", pd.Timestamp(2019, 1, 20, 9)],
430+
],
431+
columns=["index", "group", "eventTime"],
432+
).set_index("index")
433+
434+
groups = df.groupby("group")
435+
df["count_to_date"] = groups.cumcount()
436+
rolling_groups = groups.rolling("10d", on="eventTime")
437+
result = rolling_groups.apply(lambda df: df.shape[0])
438+
expected = pd.DataFrame(
439+
[
440+
["A", "group_1", pd.Timestamp(2019, 1, 1, 9), 1.0],
441+
["B", "group_1", pd.Timestamp(2019, 1, 2, 9), 2.0],
442+
["H", "group_1", pd.Timestamp(2019, 1, 6, 9), 3.0],
443+
["Z", "group_2", pd.Timestamp(2019, 1, 3, 9), 1.0],
444+
["E", "group_2", pd.Timestamp(2019, 1, 20, 9), 1.0],
445+
],
446+
columns=["index", "group", "eventTime", "count_to_date"],
447+
).set_index(["group", "index"])
448+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)