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 all commits
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
43 changes: 23 additions & 20 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,30 +1332,33 @@ def nancorr(


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"Unknown method '{method}', expected one of 'kendall', 'spearman'"
)
if method == "kendall":
from scipy.stats import kendalltau

def func(a, b):
return kendalltau(a, b)[0]

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

Choose a reason for hiding this comment

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

i would make all of these lambda, e.g.(for all 3 cases)

return lambda a, b: kendaltau(a, b)[0]

Copy link
Member

Choose a reason for hiding this comment

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

hah, i asked for these to be changed from lambda to non-lambdas for nicer tracebacks

Copy link
Contributor

Choose a reason for hiding this comment

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

oh really it does? hmm, ok, then that's fine

elif method == "spearman":
from scipy.stats import spearmanr

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 func(a, b):
return spearmanr(a, b)[0]

def _spearman(a, b):
return spearmanr(a, b)[0]
return func
elif method == "pearson":

_cor_methods = {"pearson": _pearson, "kendall": _kendall, "spearman": _spearman}
return _cor_methods[method]
def func(a, b):
return np.corrcoef(a, b)[0, 1]

return func
elif callable(method):
return method

raise ValueError(
f"Unknown method '{method}', expected one of "
"'kendall', 'spearman', 'pearson', or callable"
)


@disallow("M8", "m8")
Expand Down