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
…das-dev#16825)

xref pandas-dev#16814

(cherry picked from commit 04de578)
  • Loading branch information
jreback authored and TomAugspurger committed Jul 6, 2017
1 parent 1081d90 commit ca3d4e6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 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 @@ -39,7 +39,7 @@ Bug Fixes
- 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`)
- 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 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 @@ -1388,6 +1388,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
11 changes: 6 additions & 5 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,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 @@ -1911,7 +1911,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 @@ -1934,10 +1935,10 @@ 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.name, arg1.columns.name])
arg1.index.names + arg1.columns.names)

return result

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 ca3d4e6

Please sign in to comment.