From 5a46f0a52cc4109b4c71afc03f089e6f9e305ab3 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Mon, 27 Feb 2017 21:36:07 -0800 Subject: [PATCH] Bug:DataFrame index & column returned by corr & cov are the same (#14617) --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/core/frame.py | 6 ++++-- pandas/tests/frame/test_analytics.py | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 9b4e6fbe3be10..262e69add3e59 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -590,7 +590,7 @@ Bug Fixes - Bug in ``.rank()`` which incorrectly ranks ordered categories (:issue:`15420`) - +- Bug in ``.corr()`` and ``.cov()`` where the column and index were the same object (:issue:`14617`) - Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0963d14762ce5..6f79cc5e49ed8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4725,6 +4725,7 @@ def corr(self, method='pearson', min_periods=1): """ numeric_df = self._get_numeric_data() cols = numeric_df.columns + idx = cols.copy() mat = numeric_df.values if method == 'pearson': @@ -4757,7 +4758,7 @@ def corr(self, method='pearson', min_periods=1): correl[i, j] = c correl[j, i] = c - return self._constructor(correl, index=cols, columns=cols) + return self._constructor(correl, index=idx, columns=cols) def cov(self, min_periods=None): """ @@ -4780,6 +4781,7 @@ def cov(self, min_periods=None): """ numeric_df = self._get_numeric_data() cols = numeric_df.columns + idx = cols.copy() mat = numeric_df.values if notnull(mat).all(): @@ -4793,7 +4795,7 @@ def cov(self, min_periods=None): baseCov = _algos.nancorr(_ensure_float64(mat), cov=True, minp=min_periods) - return self._constructor(baseCov, index=cols, columns=cols) + return self._constructor(baseCov, index=idx, columns=cols) def corrwith(self, other, axis=0, drop=False): """ diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 1f0d16e959cd7..143eafc294aa8 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -118,6 +118,14 @@ def test_corr_int_and_boolean(self): for meth in ['pearson', 'kendall', 'spearman']: tm.assert_frame_equal(df.corr(meth), expected) + def test_corr_cov_independent_index_column(self): + # GH 14617 + df = pd.DataFrame(np.random.randn(4 * 10).reshape(10, 4), + columns=list("abcd")) + for method in ['cov', 'corr']: + result = getattr(df, method)() + self.assertFalse(result.index is result.columns) + def test_cov(self): # min_periods no NAs (corner case) expected = self.frame.cov()