Skip to content

Commit

Permalink
Backport PR #36208: BUG: GroupbyRolling with an empty frame (#36213)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Roeschke <emailformattr@gmail.com>
  • Loading branch information
meeseeksmachine and mroeschke committed Sep 8, 2020
1 parent c899247 commit 9bb2034
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
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 @@ -2226,10 +2226,12 @@ def _create_blocks(self, obj: FrameOrSeries):
"""
# 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 @@ -404,3 +404,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)

0 comments on commit 9bb2034

Please sign in to comment.