Skip to content

Added 'pearson' to methods list in pandas/core/nanops.py #30603

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

Merged
merged 10 commits into from
Jan 7, 2020
6 changes: 6 additions & 0 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,8 +1243,14 @@ def nancorr(a, b, method="pearson", min_periods=None):
def get_corr_func(method):
if method in ["kendall", "spearman"]:
from scipy.stats import kendalltau, spearmanr
elif method in ["pearson"]:
pass
elif callable(method):
return method
else:
raise ValueError(
f"Unkown method '{method}', expected one of 'kendall', 'spearman'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test that hits this (likely just calling .corr(method='foo') would do it

)

def _pearson(a, b):
return np.corrcoef(a, b)[0, 1]
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,14 @@ def test_nancorr_spearman(self):
targ1 = spearmanr(self.arr_float_1d.flat, self.arr_float1_1d.flat)[0]
self.check_nancorr_nancov_1d(nanops.nancorr, targ0, targ1, method="spearman")

@td.skip_if_no_scipy
def test_invalid_method(self):
targ0 = np.corrcoef(self.arr_float_2d, self.arr_float1_2d)[0, 1]
targ1 = np.corrcoef(self.arr_float_2d.flat, self.arr_float1_2d.flat)[0, 1]
msg = "Unkown method 'foo', expected one of 'kendall', 'spearman'"
with pytest.raises(ValueError, match=msg):
self.check_nancorr_nancov_1d(nanops.nancorr, targ0, targ1, method="foo")

def test_nancov(self):
targ0 = np.cov(self.arr_float_2d, self.arr_float1_2d)[0, 1]
targ1 = np.cov(self.arr_float_2d.flat, self.arr_float1_2d.flat)[0, 1]
Expand Down