Skip to content

Commit

Permalink
BUG: GroupbyRolling with an empty frame (pandas-dev#36208)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Roeschke <[email protected]>
  • Loading branch information
2 people authored and Kevin D Smith committed Nov 2, 2020
1 parent 224853a commit 05a5a9d
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 @@ -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)

0 comments on commit 05a5a9d

Please sign in to comment.