diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 822ab775e7e46..9494248a423a8 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -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 + elif method == "spearman": + from scipy.stats import spearmanr - def _kendall(a, b): - # kendallttau returns a tuple of the tau statistic and pvalue - 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")