Skip to content

Commit 92dcf5f

Browse files
Daniel SaxtonTomAugspurger
Daniel Saxton
authored andcommitted
ERR: Error message for invalid method in DataFrame.corr (pandas-dev#22298)
1 parent a3e9039 commit 92dcf5f

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ Other API Changes
496496
- :meth:`PeriodIndex.tz_convert` and :meth:`PeriodIndex.tz_localize` have been removed (:issue:`21781`)
497497
- :class:`Index` subtraction will attempt to operate element-wise instead of raising ``TypeError`` (:issue:`19369`)
498498
- :class:`pandas.io.formats.style.Styler` supports a ``number-format`` property when using :meth:`~pandas.io.formats.style.Styler.to_excel` (:issue:`22015`)
499+
- :meth:`DataFrame.corr` and :meth:`Series.corr` now raise a ``ValueError`` along with a helpful error message instead of a ``KeyError`` when supplied with an invalid method (:issue:`22298`)
499500

500501
.. _whatsnew_0240.deprecations:
501502

pandas/core/frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -6664,7 +6664,7 @@ def corr(self, method='pearson', min_periods=1):
66646664
elif method == 'spearman':
66656665
correl = libalgos.nancorr_spearman(ensure_float64(mat),
66666666
minp=min_periods)
6667-
else:
6667+
elif method == 'kendall':
66686668
if min_periods is None:
66696669
min_periods = 1
66706670
mat = ensure_float64(mat).T
@@ -6688,6 +6688,10 @@ def corr(self, method='pearson', min_periods=1):
66886688
c = corrf(ac, bc)
66896689
correl[i, j] = c
66906690
correl[j, i] = c
6691+
else:
6692+
raise ValueError("method must be either 'pearson', "
6693+
"'spearman', or 'kendall', '{method}' "
6694+
"was supplied".format(method=method))
66916695

66926696
return self._constructor(correl, index=idx, columns=cols)
66936697

pandas/core/series.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1932,8 +1932,14 @@ def corr(self, other, method='pearson', min_periods=None):
19321932
this, other = self.align(other, join='inner', copy=False)
19331933
if len(this) == 0:
19341934
return np.nan
1935-
return nanops.nancorr(this.values, other.values, method=method,
1936-
min_periods=min_periods)
1935+
1936+
if method in ['pearson', 'spearman', 'kendall']:
1937+
return nanops.nancorr(this.values, other.values, method=method,
1938+
min_periods=min_periods)
1939+
1940+
raise ValueError("method must be either 'pearson', "
1941+
"'spearman', or 'kendall', '{method}' "
1942+
"was supplied".format(method=method))
19371943

19381944
def cov(self, other, min_periods=None):
19391945
"""

pandas/tests/frame/test_analytics.py

+8
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ def test_corr_cov_independent_index_column(self):
130130
assert result.index is not result.columns
131131
assert result.index.equals(result.columns)
132132

133+
def test_corr_invalid_method(self):
134+
# GH PR #22298
135+
df = pd.DataFrame(np.random.normal(size=(10, 2)))
136+
msg = ("method must be either 'pearson', 'spearman', "
137+
"or 'kendall'")
138+
with tm.assert_raises_regex(ValueError, msg):
139+
df.corr(method="____")
140+
133141
def test_cov(self):
134142
# min_periods no NAs (corner case)
135143
expected = self.frame.cov()

pandas/tests/series/test_analytics.py

+9
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,15 @@ def test_corr_rank(self):
780780
tm.assert_almost_equal(A.corr(B, method='kendall'), kexp)
781781
tm.assert_almost_equal(A.corr(B, method='spearman'), sexp)
782782

783+
def test_corr_invalid_method(self):
784+
# GH PR #22298
785+
s1 = pd.Series(np.random.randn(10))
786+
s2 = pd.Series(np.random.randn(10))
787+
msg = ("method must be either 'pearson', 'spearman', "
788+
"or 'kendall'")
789+
with tm.assert_raises_regex(ValueError, msg):
790+
s1.corr(s2, method="____")
791+
783792
def test_cov(self):
784793
# full overlap
785794
tm.assert_almost_equal(self.ts.cov(self.ts), self.ts.std() ** 2)

0 commit comments

Comments
 (0)