From 9e55af2552664267ce27c7fc6c932acb0651c259 Mon Sep 17 00:00:00 2001 From: Tuan Date: Tue, 4 Jul 2017 03:23:45 +1000 Subject: [PATCH] fix BUG: ValueError when performing rolling covariance on multi indexed DataFrame (#16814) * fix multi index names * fix line length to pep8 * added what's new entry and reference issue number in test * Update test_multi.py * Update v0.20.3.txt --- doc/source/whatsnew/v0.20.3.txt | 2 +- pandas/core/window.py | 2 +- pandas/tests/indexes/test_multi.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.20.3.txt b/doc/source/whatsnew/v0.20.3.txt index f9bb198abf6b7..dcce427f8dd84 100644 --- a/doc/source/whatsnew/v0.20.3.txt +++ b/doc/source/whatsnew/v0.20.3.txt @@ -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`) Conversion ^^^^^^^^^^ diff --git a/pandas/core/window.py b/pandas/core/window.py index 01b1bdc3e5054..1e16eff7d56cc 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -1948,7 +1948,7 @@ def dataframe_from_int_dict(data, frame_template): result.columns = Index(result.columns).set_names( arg2.columns.name) result.index = result.index.set_names( - [arg1.index.name, arg1.columns.name]) + arg1.index.names + arg1.columns.names) return result diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index ef8806246c2c5..03f90b25415bb 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -61,6 +61,15 @@ 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