Skip to content

Commit 4640f3c

Browse files
jrebackTomAugspurger
authored andcommitted
BUG: rolling.cov with multi-index columns should presever the MI (pandas-dev#16825)
xref pandas-dev#16814 (cherry picked from commit 04de578)
1 parent 4ae6666 commit 4640f3c

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Bug Fixes
3939
- Fixed issue with dataframe scatter plot for categorical data that reports incorrect column key not found when categorical data is used for plotting (:issue:`16199`)
4040
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
4141
- Fixed compat with loading a ``DataFrame`` with a ``PeriodIndex``, from a ``format='fixed'`` HDFStore, in Python 3, that was written in Python 2 (:issue:`16781`)
42-
42+
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)
4343

4444
Conversion
4545
^^^^^^^^^^

pandas/core/indexes/multi.py

+3
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,9 @@ def __getitem__(self, key):
13881388
# cannot be sure whether the result will be sorted
13891389
sortorder = None
13901390

1391+
if isinstance(key, Index):
1392+
key = np.asarray(key)
1393+
13911394
new_labels = [lab[key] for lab in self.labels]
13921395

13931396
return MultiIndex(levels=self.levels, labels=new_labels,

pandas/core/window.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def count(self):
831831

832832
return self._wrap_results(results, blocks, obj)
833833

834-
_shared_docs['apply'] = dedent("""
834+
_shared_docs['apply'] = dedent(r"""
835835
%(name)s function apply
836836
837837
Parameters
@@ -1911,7 +1911,8 @@ def dataframe_from_int_dict(data, frame_template):
19111911

19121912
# TODO: not the most efficient (perf-wise)
19131913
# though not bad code-wise
1914-
from pandas import Panel, MultiIndex, Index
1914+
from pandas import Panel, MultiIndex
1915+
19151916
with warnings.catch_warnings(record=True):
19161917
p = Panel.from_dict(results).swapaxes('items', 'major')
19171918
if len(p.major_axis) > 0:
@@ -1934,8 +1935,8 @@ def dataframe_from_int_dict(data, frame_template):
19341935
# reset our index names to arg1 names
19351936
# reset our column names to arg2 names
19361937
# careful not to mutate the original names
1937-
result.columns = Index(result.columns).set_names(
1938-
arg2.columns.name)
1938+
result.columns = result.columns.set_names(
1939+
arg2.columns.names)
19391940
result.index = result.index.set_names(
19401941
[arg1.index.name, arg1.columns.name])
19411942

pandas/tests/test_window.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,17 @@ def tests_empty_df_rolling(self, roller):
455455
result = DataFrame(index=pd.DatetimeIndex([])).rolling(roller).sum()
456456
tm.assert_frame_equal(result, expected)
457457

458+
def test_multi_index_names(self):
459+
460+
# GH 16789, 16825
461+
cols = pd.MultiIndex.from_product([['A', 'B'], ['C', 'D', 'E']],
462+
names=['1', '2'])
463+
df = pd.DataFrame(np.ones((10, 6)), columns=cols)
464+
result = df.rolling(3).cov()
465+
466+
tm.assert_index_equal(result.columns, df.columns)
467+
assert result.index.names == [None, '1', '2']
468+
458469

459470
class TestExpanding(Base):
460471

@@ -501,9 +512,10 @@ def test_numpy_compat(self):
501512
'expander',
502513
[1, pytest.mark.xfail(
503514
reason='GH 16425 expanding with offset not supported')('1s')])
504-
def tests_empty_df_expanding(self, expander):
515+
def test_empty_df_expanding(self, expander):
505516
# GH 15819 Verifies that datetime and integer expanding windows can be
506517
# applied to empty DataFrames
518+
507519
expected = DataFrame()
508520
result = DataFrame().expanding(expander).sum()
509521
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)