Skip to content

Commit ec893fb

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

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Bug Fixes
4040
- Fixed issue with :meth:`DataFrame.style` where element id's were not unique (:issue:`16780`)
4141
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
4242
- 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`)
43-
- Fixed bug where computing the rolling covariance of a MultiIndexed ``DataFrame`` improperly raised a ``ValueError`` (:issue:`16789`)
43+
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)
4444

4545
Conversion
4646
^^^^^^^^^^

pandas/core/indexes/multi.py

+3
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,9 @@ def __getitem__(self, key):
13831383
# cannot be sure whether the result will be sorted
13841384
sortorder = None
13851385

1386+
if isinstance(key, Index):
1387+
key = np.asarray(key)
1388+
13861389
new_labels = [lab[key] for lab in self.labels]
13871390

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

pandas/core/window.py

+5-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,8 @@ 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
1926+
19261927
with warnings.catch_warnings(record=True):
19271928
p = Panel.from_dict(results).swapaxes('items', 'major')
19281929
if len(p.major_axis) > 0:
@@ -1945,8 +1946,8 @@ def dataframe_from_int_dict(data, frame_template):
19451946
# reset our index names to arg1 names
19461947
# reset our column names to arg2 names
19471948
# careful not to mutate the original names
1948-
result.columns = Index(result.columns).set_names(
1949-
arg2.columns.name)
1949+
result.columns = result.columns.set_names(
1950+
arg2.columns.names)
19501951
result.index = result.index.set_names(
19511952
arg1.index.names + arg1.columns.names)
19521953

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