Skip to content

Commit 5a46f0a

Browse files
committed
Bug:DataFrame index & column returned by corr & cov are the same (pandas-dev#14617)
1 parent edd2939 commit 5a46f0a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ Bug Fixes
590590

591591

592592
- Bug in ``.rank()`` which incorrectly ranks ordered categories (:issue:`15420`)
593-
593+
- Bug in ``.corr()`` and ``.cov()`` where the column and index were the same object (:issue:`14617`)
594594

595595

596596
- Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`)

pandas/core/frame.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4725,6 +4725,7 @@ def corr(self, method='pearson', min_periods=1):
47254725
"""
47264726
numeric_df = self._get_numeric_data()
47274727
cols = numeric_df.columns
4728+
idx = cols.copy()
47284729
mat = numeric_df.values
47294730

47304731
if method == 'pearson':
@@ -4757,7 +4758,7 @@ def corr(self, method='pearson', min_periods=1):
47574758
correl[i, j] = c
47584759
correl[j, i] = c
47594760

4760-
return self._constructor(correl, index=cols, columns=cols)
4761+
return self._constructor(correl, index=idx, columns=cols)
47614762

47624763
def cov(self, min_periods=None):
47634764
"""
@@ -4780,6 +4781,7 @@ def cov(self, min_periods=None):
47804781
"""
47814782
numeric_df = self._get_numeric_data()
47824783
cols = numeric_df.columns
4784+
idx = cols.copy()
47834785
mat = numeric_df.values
47844786

47854787
if notnull(mat).all():
@@ -4793,7 +4795,7 @@ def cov(self, min_periods=None):
47934795
baseCov = _algos.nancorr(_ensure_float64(mat), cov=True,
47944796
minp=min_periods)
47954797

4796-
return self._constructor(baseCov, index=cols, columns=cols)
4798+
return self._constructor(baseCov, index=idx, columns=cols)
47974799

47984800
def corrwith(self, other, axis=0, drop=False):
47994801
"""

pandas/tests/frame/test_analytics.py

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ def test_corr_int_and_boolean(self):
118118
for meth in ['pearson', 'kendall', 'spearman']:
119119
tm.assert_frame_equal(df.corr(meth), expected)
120120

121+
def test_corr_cov_independent_index_column(self):
122+
# GH 14617
123+
df = pd.DataFrame(np.random.randn(4 * 10).reshape(10, 4),
124+
columns=list("abcd"))
125+
for method in ['cov', 'corr']:
126+
result = getattr(df, method)()
127+
self.assertFalse(result.index is result.columns)
128+
121129
def test_cov(self):
122130
# min_periods no NAs (corner case)
123131
expected = self.frame.cov()

0 commit comments

Comments
 (0)