Skip to content

Commit c899247

Browse files
Backport PR #36152: Fix compressed multiindex for output of groupby.rolling (#36203)
Co-authored-by: patrick <[email protected]>
1 parent bb5cf8e commit c899247

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Regression in :meth:`DataFrame.replace` where a ``TypeError`` would be raised when attempting to replace elements of type :class:`Interval` (:issue:`35931`)
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`)
26+
- Fixed regression in :meth:`Series.groupby.rolling` number of levels of :class:`MultiIndex` in input was compressed to one (:issue:`36018`)
2627
-
2728

2829
.. ---------------------------------------------------------------------------

pandas/core/window/rolling.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2197,17 +2197,17 @@ def _apply(
21972197
# Compose MultiIndex result from grouping levels then rolling level
21982198
# Aggregate the MultiIndex data as tuples then the level names
21992199
grouped_object_index = self.obj.index
2200-
grouped_index_name = [grouped_object_index.name]
2200+
grouped_index_name = [*grouped_object_index.names]
22012201
groupby_keys = [grouping.name for grouping in self._groupby.grouper._groupings]
22022202
result_index_names = groupby_keys + grouped_index_name
22032203

22042204
result_index_data = []
22052205
for key, values in self._groupby.grouper.indices.items():
22062206
for value in values:
2207-
if not is_list_like(key):
2208-
data = [key, grouped_object_index[value]]
2209-
else:
2210-
data = [*key, grouped_object_index[value]]
2207+
data = [
2208+
*com.maybe_make_list(key),
2209+
*com.maybe_make_list(grouped_object_index[value]),
2210+
]
22112211
result_index_data.append(tuple(data))
22122212

22132213
result_index = MultiIndex.from_tuples(

pandas/tests/window/test_grouper.py

+21
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,24 @@ def test_groupby_subset_rolling_subset_with_closed(self):
383383
name="column1",
384384
)
385385
tm.assert_series_equal(result, expected)
386+
387+
@pytest.mark.parametrize("func", ["max", "min"])
388+
def test_groupby_rolling_index_changed(self, func):
389+
# GH: #36018 nlevels of MultiIndex changed
390+
ds = Series(
391+
[1, 2, 2],
392+
index=pd.MultiIndex.from_tuples(
393+
[("a", "x"), ("a", "y"), ("c", "z")], names=["1", "2"]
394+
),
395+
name="a",
396+
)
397+
398+
result = getattr(ds.groupby(ds).rolling(2), func)()
399+
expected = Series(
400+
[np.nan, np.nan, 2.0],
401+
index=pd.MultiIndex.from_tuples(
402+
[(1, "a", "x"), (2, "a", "y"), (2, "c", "z")], names=["a", "1", "2"]
403+
),
404+
name="a",
405+
)
406+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)