Skip to content
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

BUG: GroupbyRolling with an empty frame #36208

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Fixed regressions
- Fix regression in pickle roundtrip of the ``closed`` attribute of :class:`IntervalIndex` (:issue:`35658`)
- Fixed regression in :meth:`DataFrameGroupBy.agg` where a ``ValueError: buffer source array is read-only`` would be raised when the underlying array is read-only (:issue:`36014`)
- Fixed regression in :meth:`Series.groupby.rolling` number of levels of :class:`MultiIndex` in input was compressed to one (:issue:`36018`)
-
- Fixed regression in :class:`DataFrameGroupBy` on an empty :class:`DataFrame` (:issue:`36197`)

.. ---------------------------------------------------------------------------

Expand Down
10 changes: 6 additions & 4 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2240,10 +2240,12 @@ def _create_blocks(self, obj: FrameOrSeriesUnion):
"""
# Ensure the object we're rolling over is monotonically sorted relative
# to the groups
groupby_order = np.concatenate(
list(self._groupby.grouper.indices.values())
).astype(np.int64)
obj = obj.take(groupby_order)
# GH 36197
if not obj.empty:
groupby_order = np.concatenate(
list(self._groupby.grouper.indices.values())
).astype(np.int64)
obj = obj.take(groupby_order)
return super()._create_blocks(obj)

def _get_cython_func_type(self, func: str) -> Callable:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/window/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,15 @@ def test_groupby_rolling_index_changed(self, func):
name="a",
)
tm.assert_series_equal(result, expected)

def test_groupby_rolling_empty_frame(self):
# GH 36197
expected = pd.DataFrame({"s1": []})
result = expected.groupby("s1").rolling(window=1).sum()
expected.index = pd.MultiIndex.from_tuples([], names=["s1", None])
tm.assert_frame_equal(result, expected)

expected = pd.DataFrame({"s1": [], "s2": []})
result = expected.groupby(["s1", "s2"]).rolling(window=1).sum()
expected.index = pd.MultiIndex.from_tuples([], names=["s1", "s2", None])
tm.assert_frame_equal(result, expected)