Skip to content

Commit 1754f08

Browse files
committed
BUG: rolling.cov with multi-index columns should presever the MI
xref #16814
1 parent 9e55af2 commit 1754f08

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

pandas/core/window.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ def count(self):
836836

837837
return self._wrap_results(results, blocks, obj)
838838

839-
_shared_docs['apply'] = dedent("""
839+
_shared_docs['apply'] = dedent(r"""
840840
%(name)s function apply
841841
842842
Parameters
@@ -1922,7 +1922,7 @@ def dataframe_from_int_dict(data, frame_template):
19221922

19231923
# TODO: not the most efficient (perf-wise)
19241924
# though not bad code-wise
1925-
from pandas import Panel, MultiIndex, Index
1925+
from pandas import Panel, MultiIndex
19261926
with warnings.catch_warnings(record=True):
19271927
p = Panel.from_dict(results).swapaxes('items', 'major')
19281928
if len(p.major_axis) > 0:
@@ -1945,8 +1945,8 @@ def dataframe_from_int_dict(data, frame_template):
19451945
# reset our index names to arg1 names
19461946
# reset our column names to arg2 names
19471947
# careful not to mutate the original names
1948-
result.columns = Index(result.columns).set_names(
1949-
arg2.columns.name)
1948+
result.columns = result.columns.set_names(
1949+
arg2.columns.names)
19501950
result.index = result.index.set_names(
19511951
arg1.index.names + arg1.columns.names)
19521952

pandas/tests/indexes/test_multi.py

-9
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,6 @@ def f():
6161

6262
tm.assert_raises_regex(ValueError, 'The truth value of a', f)
6363

64-
def test_multi_index_names(self):
65-
66-
# GH 16789
67-
cols = pd.MultiIndex.from_product([['A', 'B'], ['C', 'D', 'E']],
68-
names=['1', '2'])
69-
df = pd.DataFrame(np.ones((10, 6)), columns=cols)
70-
rolling_result = df.rolling(3).cov()
71-
assert rolling_result.index.names == [None, '1', '2']
72-
7364
def test_labels_dtypes(self):
7465

7566
# GH 8456

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
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)