Skip to content

Commit 9bb2034

Browse files
Backport PR #36208: BUG: GroupbyRolling with an empty frame (#36213)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent c899247 commit 9bb2034

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

doc/source/whatsnew/v1.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Fixed regressions
2424
- Fix regression in pickle roundtrip of the ``closed`` attribute of :class:`IntervalIndex` (:issue:`35658`)
2525
- 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`)
2626
- Fixed regression in :meth:`Series.groupby.rolling` number of levels of :class:`MultiIndex` in input was compressed to one (:issue:`36018`)
27-
-
27+
- Fixed regression in :class:`DataFrameGroupBy` on an empty :class:`DataFrame` (:issue:`36197`)
2828

2929
.. ---------------------------------------------------------------------------
3030

pandas/core/window/rolling.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2226,10 +2226,12 @@ def _create_blocks(self, obj: FrameOrSeries):
22262226
"""
22272227
# Ensure the object we're rolling over is monotonically sorted relative
22282228
# to the groups
2229-
groupby_order = np.concatenate(
2230-
list(self._groupby.grouper.indices.values())
2231-
).astype(np.int64)
2232-
obj = obj.take(groupby_order)
2229+
# GH 36197
2230+
if not obj.empty:
2231+
groupby_order = np.concatenate(
2232+
list(self._groupby.grouper.indices.values())
2233+
).astype(np.int64)
2234+
obj = obj.take(groupby_order)
22332235
return super()._create_blocks(obj)
22342236

22352237
def _get_cython_func_type(self, func: str) -> Callable:

pandas/tests/window/test_grouper.py

+12
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,15 @@ def test_groupby_rolling_index_changed(self, func):
404404
name="a",
405405
)
406406
tm.assert_series_equal(result, expected)
407+
408+
def test_groupby_rolling_empty_frame(self):
409+
# GH 36197
410+
expected = pd.DataFrame({"s1": []})
411+
result = expected.groupby("s1").rolling(window=1).sum()
412+
expected.index = pd.MultiIndex.from_tuples([], names=["s1", None])
413+
tm.assert_frame_equal(result, expected)
414+
415+
expected = pd.DataFrame({"s1": [], "s2": []})
416+
result = expected.groupby(["s1", "s2"]).rolling(window=1).sum()
417+
expected.index = pd.MultiIndex.from_tuples([], names=["s1", "s2", None])
418+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)