Skip to content

Backport PR #36753 on branch 1.1.x: BUG: Segfault with string Index when using Rolling after Groupby #37003

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
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def _wrap_results(self, results, blocks, obj, exclude=None) -> FrameOrSeries:
if self.on is not None and not self._on.equals(obj.index):

name = self._on.name
final.append(Series(self._on, index=obj.index, name=name))
final.append(Series(self._on, index=self.obj.index, name=name))

if self._selection is not None:

Expand Down Expand Up @@ -2259,7 +2259,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer:
"""
rolling_indexer: Type[BaseIndexer]
indexer_kwargs: Optional[Dict] = None
index_array = self.obj.index.asi8
index_array = self._on.asi8
if isinstance(self.window, BaseIndexer):
rolling_indexer = type(self.window)
indexer_kwargs = self.window.__dict__
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/window/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,35 @@ def test_groupby_rolling_empty_frame(self):
expected.index = pd.MultiIndex.from_tuples([], names=["s1", "s2", None])
tm.assert_frame_equal(result, expected)

def test_groupby_rolling_string_index(self):
# GH: 36727
df = pd.DataFrame(
[
["A", "group_1", pd.Timestamp(2019, 1, 1, 9)],
["B", "group_1", pd.Timestamp(2019, 1, 2, 9)],
["Z", "group_2", pd.Timestamp(2019, 1, 3, 9)],
["H", "group_1", pd.Timestamp(2019, 1, 6, 9)],
["E", "group_2", pd.Timestamp(2019, 1, 20, 9)],
],
columns=["index", "group", "eventTime"],
).set_index("index")

groups = df.groupby("group")
df["count_to_date"] = groups.cumcount()
rolling_groups = groups.rolling("10d", on="eventTime")
result = rolling_groups.apply(lambda df: df.shape[0])
expected = pd.DataFrame(
[
["A", "group_1", pd.Timestamp(2019, 1, 1, 9), 1.0],
["B", "group_1", pd.Timestamp(2019, 1, 2, 9), 2.0],
["H", "group_1", pd.Timestamp(2019, 1, 6, 9), 3.0],
["Z", "group_2", pd.Timestamp(2019, 1, 3, 9), 1.0],
["E", "group_2", pd.Timestamp(2019, 1, 20, 9), 1.0],
],
columns=["index", "group", "eventTime", "count_to_date"],
).set_index(["group", "index"])
tm.assert_frame_equal(result, expected)

def test_groupby_rolling_no_sort(self):
# GH 36889
result = (
Expand Down