Skip to content

Commit fc98ab5

Browse files
authored
CLN: Clean nanops.get_corr_func (#33244)
1 parent cdc4d97 commit fc98ab5

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

pandas/core/nanops.py

+23-20
Original file line numberDiff line numberDiff line change
@@ -1332,30 +1332,33 @@ def nancorr(
13321332

13331333

13341334
def get_corr_func(method):
1335-
if method in ["kendall", "spearman"]:
1336-
from scipy.stats import kendalltau, spearmanr
1337-
elif method in ["pearson"]:
1338-
pass
1339-
elif callable(method):
1340-
return method
1341-
else:
1342-
raise ValueError(
1343-
f"Unknown method '{method}', expected one of 'kendall', 'spearman'"
1344-
)
1335+
if method == "kendall":
1336+
from scipy.stats import kendalltau
1337+
1338+
def func(a, b):
1339+
return kendalltau(a, b)[0]
13451340

1346-
def _pearson(a, b):
1347-
return np.corrcoef(a, b)[0, 1]
1341+
return func
1342+
elif method == "spearman":
1343+
from scipy.stats import spearmanr
13481344

1349-
def _kendall(a, b):
1350-
# kendallttau returns a tuple of the tau statistic and pvalue
1351-
rs = kendalltau(a, b)
1352-
return rs[0]
1345+
def func(a, b):
1346+
return spearmanr(a, b)[0]
13531347

1354-
def _spearman(a, b):
1355-
return spearmanr(a, b)[0]
1348+
return func
1349+
elif method == "pearson":
13561350

1357-
_cor_methods = {"pearson": _pearson, "kendall": _kendall, "spearman": _spearman}
1358-
return _cor_methods[method]
1351+
def func(a, b):
1352+
return np.corrcoef(a, b)[0, 1]
1353+
1354+
return func
1355+
elif callable(method):
1356+
return method
1357+
1358+
raise ValueError(
1359+
f"Unknown method '{method}', expected one of "
1360+
"'kendall', 'spearman', 'pearson', or callable"
1361+
)
13591362

13601363

13611364
@disallow("M8", "m8")

0 commit comments

Comments
 (0)