From 7d93a1c779f7ed6881f191cac13eae01e4e72f39 Mon Sep 17 00:00:00 2001 From: Sam Waterbury Date: Mon, 16 Jul 2018 21:44:52 -0500 Subject: [PATCH] Added option for alternative correlation types to `DataFrame.corrwith()` --- pandas/core/frame.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6380944338010..0814e6ce976ab 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6891,7 +6891,7 @@ def cov(self, min_periods=None): return self._constructor(baseCov, index=idx, columns=cols) - def corrwith(self, other, axis=0, drop=False): + def corrwith(self, other, axis=0, method='pearson', drop=False): """ Compute pairwise correlation between rows or columns of two DataFrame objects. @@ -6901,6 +6901,10 @@ def corrwith(self, other, axis=0, drop=False): other : DataFrame, Series axis : {0 or 'index', 1 or 'columns'}, default 0 0 or 'index' to compute column-wise, 1 or 'columns' for row-wise + method : {'pearson', 'kendall', 'spearman'} + * pearson : standard correlation coefficient + * kendall : Kendall Tau correlation coefficient + * spearman : Spearman rank correlation drop : boolean, default False Drop missing indices from result, default returns union of all @@ -6912,10 +6916,9 @@ def corrwith(self, other, axis=0, drop=False): this = self._get_numeric_data() if isinstance(other, Series): - return this.apply(other.corr, axis=axis) + return this.apply(other.corr, axis=axis, method=method) other = other._get_numeric_data() - left, right = this.align(other, join='inner', copy=False) # mask missing values @@ -6926,14 +6929,9 @@ def corrwith(self, other, axis=0, drop=False): left = left.T right = right.T - # demeaned data - ldem = left - left.mean() - rdem = right - right.mean() - - num = (ldem * rdem).sum() - dom = (left.count() - 1) * left.std() * right.std() - - correl = num / dom + correl = Series(index=left.columns) + for col in left.columns: + correl[col] = nanops.nancorr(left[col], right[col], method=method) if not drop: raxis = 1 if axis == 0 else 0