Skip to content

Commit

Permalink
BUG: rolling.cov with multi-index columns should presever the MI (pan…
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback authored Jul 4, 2017
1 parent 9e55af2 commit 04de578
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Bug Fixes
- Fixed issue with :meth:`DataFrame.style` where element id's were not unique (:issue:`16780`)
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
- 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`)
- Fixed bug where computing the rolling covariance of a MultiIndexed ``DataFrame`` improperly raised a ``ValueError`` (:issue:`16789`)
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)

Conversion
^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,9 @@ def __getitem__(self, key):
# cannot be sure whether the result will be sorted
sortorder = None

if isinstance(key, Index):
key = np.asarray(key)

new_labels = [lab[key] for lab in self.labels]

return MultiIndex(levels=self.levels, labels=new_labels,
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def count(self):

return self._wrap_results(results, blocks, obj)

_shared_docs['apply'] = dedent("""
_shared_docs['apply'] = dedent(r"""
%(name)s function apply
Parameters
Expand Down Expand Up @@ -1922,7 +1922,8 @@ def dataframe_from_int_dict(data, frame_template):

# TODO: not the most efficient (perf-wise)
# though not bad code-wise
from pandas import Panel, MultiIndex, Index
from pandas import Panel, MultiIndex

with warnings.catch_warnings(record=True):
p = Panel.from_dict(results).swapaxes('items', 'major')
if len(p.major_axis) > 0:
Expand All @@ -1945,8 +1946,8 @@ def dataframe_from_int_dict(data, frame_template):
# reset our index names to arg1 names
# reset our column names to arg2 names
# careful not to mutate the original names
result.columns = Index(result.columns).set_names(
arg2.columns.name)
result.columns = result.columns.set_names(
arg2.columns.names)
result.index = result.index.set_names(
arg1.index.names + arg1.columns.names)

Expand Down
9 changes: 0 additions & 9 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ def f():

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

def test_multi_index_names(self):

# GH 16789
cols = pd.MultiIndex.from_product([['A', 'B'], ['C', 'D', 'E']],
names=['1', '2'])
df = pd.DataFrame(np.ones((10, 6)), columns=cols)
rolling_result = df.rolling(3).cov()
assert rolling_result.index.names == [None, '1', '2']

def test_labels_dtypes(self):

# GH 8456
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ def tests_empty_df_rolling(self, roller):
result = DataFrame(index=pd.DatetimeIndex([])).rolling(roller).sum()
tm.assert_frame_equal(result, expected)

def test_multi_index_names(self):

# GH 16789, 16825
cols = pd.MultiIndex.from_product([['A', 'B'], ['C', 'D', 'E']],
names=['1', '2'])
df = pd.DataFrame(np.ones((10, 6)), columns=cols)
result = df.rolling(3).cov()

tm.assert_index_equal(result.columns, df.columns)
assert result.index.names == [None, '1', '2']


class TestExpanding(Base):

Expand Down Expand Up @@ -501,9 +512,10 @@ def test_numpy_compat(self):
'expander',
[1, pytest.mark.xfail(
reason='GH 16425 expanding with offset not supported')('1s')])
def tests_empty_df_expanding(self, expander):
def test_empty_df_expanding(self, expander):
# GH 15819 Verifies that datetime and integer expanding windows can be
# applied to empty DataFrames

expected = DataFrame()
result = DataFrame().expanding(expander).sum()
tm.assert_frame_equal(result, expected)
Expand Down

0 comments on commit 04de578

Please sign in to comment.