Skip to content

Commit ce0476f

Browse files
authored
Fix compressed multiindex for output of groupby.rolling (#36152)
1 parent b37c9f8 commit ce0476f

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
@@ -2211,17 +2211,17 @@ def _apply(
22112211
# Compose MultiIndex result from grouping levels then rolling level
22122212
# Aggregate the MultiIndex data as tuples then the level names
22132213
grouped_object_index = self.obj.index
2214-
grouped_index_name = [grouped_object_index.name]
2214+
grouped_index_name = [*grouped_object_index.names]
22152215
groupby_keys = [grouping.name for grouping in self._groupby.grouper._groupings]
22162216
result_index_names = groupby_keys + grouped_index_name
22172217

22182218
result_index_data = []
22192219
for key, values in self._groupby.grouper.indices.items():
22202220
for value in values:
2221-
if not is_list_like(key):
2222-
data = [key, grouped_object_index[value]]
2223-
else:
2224-
data = [*key, grouped_object_index[value]]
2221+
data = [
2222+
*com.maybe_make_list(key),
2223+
*com.maybe_make_list(grouped_object_index[value]),
2224+
]
22252225
result_index_data.append(tuple(data))
22262226

22272227
result_index = MultiIndex.from_tuples(

pandas/tests/window/test_grouper.py

+21
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,24 @@ def test_groupby_subset_rolling_subset_with_closed(self):
372372
name="column1",
373373
)
374374
tm.assert_series_equal(result, expected)
375+
376+
@pytest.mark.parametrize("func", ["max", "min"])
377+
def test_groupby_rolling_index_changed(self, func):
378+
# GH: #36018 nlevels of MultiIndex changed
379+
ds = Series(
380+
[1, 2, 2],
381+
index=pd.MultiIndex.from_tuples(
382+
[("a", "x"), ("a", "y"), ("c", "z")], names=["1", "2"]
383+
),
384+
name="a",
385+
)
386+
387+
result = getattr(ds.groupby(ds).rolling(2), func)()
388+
expected = Series(
389+
[np.nan, np.nan, 2.0],
390+
index=pd.MultiIndex.from_tuples(
391+
[(1, "a", "x"), (2, "a", "y"), (2, "c", "z")], names=["a", "1", "2"]
392+
),
393+
name="a",
394+
)
395+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)