Skip to content

Commit d0a281f

Browse files
mroeschkejreback
authored andcommitted
BUG: DataFrame index & column returned by corr & cov are the same (#14617)
closes #14617 Author: Matt Roeschke <emailformattr@gmail.com> Closes #15528 from mroeschke/fix_14617 and squashes the following commits: 5a46f0a [Matt Roeschke] Bug:DataFrame index & column returned by corr & cov are the same (#14617)
1 parent dd368eb commit d0a281f

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Diff for: doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ Bug Fixes
631631

632632

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

636636

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

Diff for: 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
"""

Diff for: pandas/tests/frame/test_analytics.py

+9
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ 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+
assert result.index is not result.columns
128+
assert result.index.equals(result.columns)
129+
121130
def test_cov(self):
122131
# min_periods no NAs (corner case)
123132
expected = self.frame.cov()

0 commit comments

Comments
 (0)