-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Adds triangular option to DataFrame.corr, closes #22840 #22842
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 4 commits
8a2eac3
9a95e8c
9acc618
c97fec1
9b0276a
b15436b
6be67f6
d619d50
9df2bcb
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 |
---|---|---|
|
@@ -6667,7 +6667,7 @@ def _series_round(s, decimals): | |
# ---------------------------------------------------------------------- | ||
# Statistical methods, etc. | ||
|
||
def corr(self, method='pearson', min_periods=1): | ||
def corr(self, method='pearson', min_periods=1, tri=None): | ||
""" | ||
Compute pairwise correlation of columns, excluding NA/null values | ||
|
||
|
@@ -6686,6 +6686,12 @@ def corr(self, method='pearson', min_periods=1): | |
to have a valid result. Currently only available for pearson | ||
and spearman correlation | ||
|
||
tri : {'upper', 'lower'} or None, default : None | ||
Whether or not to return the upper / lower triangular | ||
correlation matrix | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
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. Can you add a versionadded directive here? |
||
Returns | ||
------- | ||
y : DataFrame | ||
|
@@ -6741,7 +6747,23 @@ def corr(self, method='pearson', min_periods=1): | |
"'spearman', or 'kendall', '{method}' " | ||
"was supplied".format(method=method)) | ||
|
||
return self._constructor(correl, index=idx, columns=cols) | ||
corr_mat = self._constructor(correl, index=idx, columns=cols) | ||
|
||
if tri is not None: | ||
mask = np.tril(np.ones_like(corr_mat, | ||
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. Is it possible to use 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. Yes, nice catch. |
||
dtype=np.bool), | ||
k=-1) | ||
|
||
if tri == 'lower': | ||
return corr_mat.where(mask) | ||
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. Not the speed is probably important, but |
||
elif tri == 'upper': | ||
return corr_mat.where(mask.T) | ||
else: | ||
raise ValueError("tri must be either 'lower', " | ||
"or 'upper', '{tri_method}' " | ||
"was supplied".format(tri_method=tri)) | ||
else: | ||
return corr_mat | ||
|
||
def cov(self, min_periods=None): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,18 @@ def test_corr_invalid_method(self): | |
with tm.assert_raises_regex(ValueError, msg): | ||
df.corr(method="____") | ||
|
||
def test_corr_tril(self): | ||
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. For these tests, it's better to build an expected DataFrame output and compare it to a resulting DataFrame. You can build a DataFrame that gives a trivial correlation matrix (like a
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 updated the tests according to this idea. We're still using |
||
# GH PR #22840 | ||
df = pd.DataFrame(np.random.normal(size=(100, 5))) | ||
corr_mat = df.corr(tri='lower') | ||
assert corr_mat.notnull().sum().sum() == 10 | ||
|
||
def test_corr_triu(self): | ||
# GH PR #22840 | ||
df = pd.DataFrame(np.random.normal(size=(100, 5))) | ||
corr_mat = df.corr(tri='upper') | ||
assert corr_mat.notnull().sum().sum() == 10 | ||
|
||
def test_cov(self): | ||
# min_periods no NAs (corner case) | ||
expected = self.frame.cov() | ||
|
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.
Could you add an explanation for why a user might want to do this? And an example below using upper or lower?
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.
Sure, you mean within the docstring itself right?