Skip to content

Commit fc75d9c

Browse files
mroeschkeMatt Roeschke
and
Matt Roeschke
authored
BUG: GroupbyRolling with an empty frame (#36208)
Co-authored-by: Matt Roeschke <[email protected]>
1 parent 19f0a9f commit fc75d9c

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
@@ -2240,10 +2240,12 @@ def _create_blocks(self, obj: FrameOrSeriesUnion):
22402240
"""
22412241
# Ensure the object we're rolling over is monotonically sorted relative
22422242
# to the groups
2243-
groupby_order = np.concatenate(
2244-
list(self._groupby.grouper.indices.values())
2245-
).astype(np.int64)
2246-
obj = obj.take(groupby_order)
2243+
# GH 36197
2244+
if not obj.empty:
2245+
groupby_order = np.concatenate(
2246+
list(self._groupby.grouper.indices.values())
2247+
).astype(np.int64)
2248+
obj = obj.take(groupby_order)
22472249
return super()._create_blocks(obj)
22482250

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

pandas/tests/window/test_grouper.py

+12
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,15 @@ def test_groupby_rolling_index_changed(self, func):
393393
name="a",
394394
)
395395
tm.assert_series_equal(result, expected)
396+
397+
def test_groupby_rolling_empty_frame(self):
398+
# GH 36197
399+
expected = pd.DataFrame({"s1": []})
400+
result = expected.groupby("s1").rolling(window=1).sum()
401+
expected.index = pd.MultiIndex.from_tuples([], names=["s1", None])
402+
tm.assert_frame_equal(result, expected)
403+
404+
expected = pd.DataFrame({"s1": [], "s2": []})
405+
result = expected.groupby(["s1", "s2"]).rolling(window=1).sum()
406+
expected.index = pd.MultiIndex.from_tuples([], names=["s1", "s2", None])
407+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)