Skip to content

CLN: Clean nanops.get_corr_func #33244

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 6 commits into from
Apr 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,31 +1332,24 @@ def nancorr(


def get_corr_func(method):
if method in ["kendall", "spearman"]:
from scipy.stats import kendalltau, spearmanr
elif method in ["pearson"]:
pass
if method == "kendall":
from scipy.stats import kendalltau

return lambda a, b: kendalltau(a, b)[0]
elif method == "spearman":
from scipy.stats import spearmanr

return lambda a, b: spearmanr(a, b)[0]
elif method == "pearson":
return lambda a, b: np.corrcoef(a, b)[0, 1]
elif callable(method):
return method
else:
raise ValueError(
f"Unknown method '{method}', expected one of 'kendall', 'spearman'"
f"Unknown method '{method}', expected one of "
"'kendall', 'spearman', 'pearson', or callable"
)

def _pearson(a, b):
return np.corrcoef(a, b)[0, 1]

def _kendall(a, b):
# kendallttau returns a tuple of the tau statistic and pvalue
Copy link
Member

Choose a reason for hiding this comment

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

is this comment worth retaining?

Copy link
Member Author

Choose a reason for hiding this comment

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

Might as well, should we also have a comment for spearmanr and np.corrcoef?

rs = kendalltau(a, b)
return rs[0]

def _spearman(a, b):
return spearmanr(a, b)[0]

_cor_methods = {"pearson": _pearson, "kendall": _kendall, "spearman": _spearman}
return _cor_methods[method]


@disallow("M8", "m8")
def nancov(a: np.ndarray, b: np.ndarray, min_periods: Optional[int] = None):
Expand Down