Skip to content

BUG: rolling.corr with MultiIndex columns #43261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all 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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ Groupby/resample/rolling
- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`)
- Bug in :meth:`Resampler.aggregate` did not allow the use of Named Aggregation (:issue:`32803`)
- Bug in :meth:`Series.rolling` when the :class:`Series` ``dtype`` was ``Int64`` (:issue:`43016`)
- Bug in :meth:`DataFrame.rolling.corr` when the :class:`DataFrame` columns was a :class:`MultiIndex` (:issue:`21157`)
- Bug in :meth:`DataFrame.groupby.rolling` when specifying ``on`` and calling ``__getitem__`` would subsequently return incorrect results (:issue:`43355`)

Reshaping
Expand Down
20 changes: 18 additions & 2 deletions pandas/core/window/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,24 @@ def dataframe_from_int_dict(data, frame_template):
# mypy needs to know columns is a MultiIndex, Index doesn't
# have levels attribute
arg2.columns = cast(MultiIndex, arg2.columns)
result.index = MultiIndex.from_product(
arg2.columns.levels + [result_index]
# GH 21157: Equivalent to MultiIndex.from_product(
# [result_index], <unique combinations of arg2.columns.levels>,
# )
# A normal MultiIndex.from_product will produce too many
# combinations.
result_level = np.tile(
result_index, len(result) // len(result_index)
)
arg2_levels = (
np.repeat(
arg2.columns.get_level_values(i),
len(result) // len(arg2.columns),
)
for i in range(arg2.columns.nlevels)
)
result_names = list(arg2.columns.names) + [result_index.name]
result.index = MultiIndex.from_arrays(
[*arg2_levels, result_level], names=result_names
)
# GH 34440
num_levels = len(result.index.levels)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/window/test_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,18 @@ def test_cov_mulittindex(self):
)

tm.assert_frame_equal(result, expected)

def test_multindex_columns_pairwise_func(self):
# GH 21157
columns = MultiIndex.from_arrays([["M", "N"], ["P", "Q"]], names=["a", "b"])
df = DataFrame(np.ones((5, 2)), columns=columns)
result = df.rolling(3).corr()
expected = DataFrame(
np.nan,
index=MultiIndex.from_arrays(
[np.repeat(np.arange(5), 2), ["M", "N"] * 5, ["P", "Q"] * 5],
names=[None, "a", "b"],
),
columns=columns,
)
tm.assert_frame_equal(result, expected)