-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Allow parameters method and min_periods in DataFrame.corrwith() #15573
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4819,7 +4819,8 @@ 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, drop=False, method='pearson', | ||
min_periods=1): | ||
""" | ||
Compute pairwise correlation between rows or columns of two DataFrame | ||
objects. | ||
|
@@ -4831,36 +4832,41 @@ def corrwith(self, other, axis=0, drop=False): | |
0 or 'index' to compute column-wise, 1 or 'columns' for row-wise | ||
drop : boolean, default False | ||
Drop missing indices from result, default returns union of all | ||
method : {'pearson', 'kendall', 'spearman'} | ||
* pearson : standard correlation coefficient | ||
* kendall : Kendall Tau correlation coefficient | ||
* spearman : Spearman rank correlation | ||
|
||
.. versionadded:: 0.20.0 | ||
|
||
min_periods : int, optional | ||
Minimum number of observations needed to have a valid result | ||
|
||
.. versionadded:: 0.20.0 | ||
|
||
Returns | ||
------- | ||
correls : Series | ||
""" | ||
axis = self._get_axis_number(axis) | ||
if isinstance(other, Series): | ||
return self.apply(other.corr, axis=axis) | ||
return self.apply(other.corr, axis=axis, | ||
method=method, min_periods=min_periods) | ||
|
||
this = self._get_numeric_data() | ||
other = other._get_numeric_data() | ||
|
||
left, right = this.align(other, join='inner', copy=False) | ||
|
||
# mask missing values | ||
left = left + right * 0 | ||
right = right + left * 0 | ||
|
||
if axis == 1: | ||
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({col: nanops.nancorr(left[col].values, | ||
right[col].values, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is going to be very slow we need to rework nancorr to do this instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the new implementation (which calls nancorr which in turns calls numpy/scipy correlation functions) is actually significantly faster than the current implementation (manually computing Pearson correlation using For example: Current implementation: >>> import pandas as pd; import timeit
>>> pd.__version__
u'0.19.2'
>>> iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
>>> timeit.timeit(lambda: iris.corrwith(iris), number=10000)
50.891642808914185
>>> timeit.timeit(lambda: iris.T.corrwith(iris.T), number=10000)
42.0677649974823 New implementation: >>> import pandas as pd; import timeit
>>> pd.__version__
'0.19.0+539.g0b77680.dirty'
>>> iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
>>> timeit.timeit(lambda: iris.corrwith(iris, method='pearson'), number=10000)
28.622286081314087
>>> timeit.timeit(lambda: iris.T.corrwith(iris.T, method='pearson'), number=10000)
21.898916959762573 I'm pretty new to this, so please let me know if I'm missing anything here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. look thru the benchmarks and pls add some asv as appropriate include wide and talk data on wide data this will be slower |
||
method=method, | ||
min_periods=min_periods) | ||
for col in left.columns}) | ||
|
||
if not drop: | ||
raxis = 1 if axis == 0 else 0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add versionsddes tag (and small comment here)