Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ Groupby/resample/rolling
- Bug when subsetting columns on a :class:`~pandas.core.groupby.DataFrameGroupBy` (e.g. ``df.groupby('a')[['b']])``) would reset the attributes ``axis``, ``dropna``, ``group_keys``, ``level``, ``mutated``, ``sort``, and ``squeeze`` to their default values. (:issue:`9959`)
- Bug in :meth:`DataFrameGroupby.tshift` failing to raise ``ValueError`` when a frequency cannot be inferred for the index of a group (:issue:`35937`)
- Bug in :meth:`DataFrame.groupby` does not always maintain column index name for ``any``, ``all``, ``bfill``, ``ffill``, ``shift`` (:issue:`29764`)
- Bug in :meth:`Series.groupby.rolling` compressed number of levels of :class:`MultiIndex` in input to one (:issue:`36018`)
-

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

result_index_data = []
for key, values in self._groupby.grouper.indices.items():
for value in values:
if not is_list_like(key):
data = [key, grouped_object_index[value]]
data = [key] if not is_list_like(key) else [*key]
if is_list_like(grouped_object_index[value]):
grouped_object_index_value = [*grouped_object_index[value]]
else:
data = [*key, grouped_object_index[value]]
grouped_object_index_value = [grouped_object_index[value]]
data.extend(grouped_object_index_value)
result_index_data.append(tuple(data))

result_index = MultiIndex.from_tuples(
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,3 +2134,25 @@ def test_groupby_column_index_name_lost_fill_funcs(func):
result = getattr(df_grouped, func)().columns
expected = pd.Index(["a", "b"], name="idx")
tm.assert_index_equal(result, expected)


@pytest.mark.parametrize("func", ["max", "min"])
def test_groupby_rolling_index_changed(func):
# GH: #36018 nlevels of MultiIndex changed
ds = pd.Series(
[1, 2, 2],
index=pd.MultiIndex.from_tuples(
[("a", "x"), ("a", "y"), ("c", "z")], names=["1", "2"]
),
name="a",
)

result = getattr(ds.groupby(ds).rolling(2), func)()
expected = pd.Series(
[np.nan, np.nan, 2.0],
index=pd.MultiIndex.from_tuples(
[(1, "a", "x"), (2, "a", "y"), (2, "c", "z")], names=["a", "1", "2"]
),
name="a",
)
tm.assert_series_equal(result, expected)