-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Use a more helpful error message for invalid correlation methods in DataFrame.corr #22298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
e5fc074
ddc0f14
0fce19f
6490af3
97da60c
156dcac
e7d49a1
5ba0bc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1931,8 +1931,14 @@ def corr(self, other, method='pearson', min_periods=None): | |
this, other = self.align(other, join='inner', copy=False) | ||
if len(this) == 0: | ||
return np.nan | ||
return nanops.nancorr(this.values, other.values, method=method, | ||
min_periods=min_periods) | ||
|
||
if method in ['pearson', 'spearman', 'kendall']: | ||
return nanops.nancorr(this.values, other.values, method=method, | ||
min_periods=min_periods) | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the else here |
||
raise ValueError("method must be either 'pearson', " | ||
"'spearman', or 'kendall', '{method}' " | ||
"was supplied".format(method=method)) | ||
|
||
def cov(self, other, min_periods=None): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,13 @@ def test_corr_cov_independent_index_column(self): | |
assert result.index is not result.columns | ||
assert result.index.equals(result.columns) | ||
|
||
def test_corr_invalid_method(self): | ||
df = pd.DataFrame(np.random.normal(size=(10, 2))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the gh issue number as a comment |
||
pttrn = ("method must be either 'pearson', 'spearman', " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pttrn -> msg |
||
"or 'kendall'") | ||
with tm.assert_raises_regex(ValueError, pttrn): | ||
df.corr(method="____") | ||
|
||
def test_cov(self): | ||
# min_periods no NAs (corner case) | ||
expected = self.frame.cov() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -778,6 +778,14 @@ def test_corr_rank(self): | |
tm.assert_almost_equal(A.corr(B, method='kendall'), kexp) | ||
tm.assert_almost_equal(A.corr(B, method='spearman'), sexp) | ||
|
||
def test_corr_invalid_method(self): | ||
s1 = pd.Series(np.random.randn(10)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
s2 = pd.Series(np.random.randn(10)) | ||
pttrn = ("method must be either 'pearson', 'spearman', " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pttrn -> msg |
||
"or 'kendall'") | ||
with tm.assert_raises_regex(ValueError, pttrn): | ||
s1.corr(s2, method="____") | ||
|
||
def test_cov(self): | ||
# full overlap | ||
tm.assert_almost_equal(self.ts.cov(self.ts), self.ts.std() ** 2) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add the issue number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the PR number as we don't have an issue number