From 996391694d354a493952b3f3cba9deec6cdd6ae8 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Thu, 2 Apr 2020 12:58:00 -0500 Subject: [PATCH 1/3] CLN: Clean nanops.get_corr_func --- pandas/core/nanops.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 822ab775e7e46..6d39c40380289 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -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 - 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): From 137ddddcb8c5d3f2cd85953cf1cfb0da20436c01 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Thu, 2 Apr 2020 13:16:27 -0500 Subject: [PATCH 2/3] Don't use lambda --- pandas/core/nanops.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 6d39c40380289..b230a5650c42f 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -1335,13 +1335,23 @@ def get_corr_func(method): if method == "kendall": from scipy.stats import kendalltau - return lambda a, b: kendalltau(a, b)[0] + def func(a, b): + return kendalltau(a, b)[0] + + return func elif method == "spearman": from scipy.stats import spearmanr - return lambda a, b: spearmanr(a, b)[0] + def func(a, b): + return spearmanr(a, b)[0] + + return func elif method == "pearson": - return lambda a, b: np.corrcoef(a, b)[0, 1] + + def func(a, b): + return np.corrcoef(a, b)[0, 1] + + return func elif callable(method): return method else: From f8b3617b525bd08aa5fe428ce63f695dbeefdffc Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 5 Apr 2020 16:06:25 -0500 Subject: [PATCH 3/3] No else --- pandas/core/nanops.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index b230a5650c42f..9494248a423a8 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -1354,11 +1354,11 @@ def func(a, b): return func elif callable(method): return method - else: - raise ValueError( - f"Unknown method '{method}', expected one of " - "'kendall', 'spearman', 'pearson', or callable" - ) + + raise ValueError( + f"Unknown method '{method}', expected one of " + "'kendall', 'spearman', 'pearson', or callable" + ) @disallow("M8", "m8")