-
-
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 2 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=False): | ||
""" | ||
Compute pairwise correlation of columns, excluding NA/null values | ||
|
||
|
@@ -6686,6 +6686,10 @@ def corr(self, method='pearson', min_periods=1): | |
to have a valid result. Currently only available for pearson | ||
and spearman correlation | ||
|
||
tri : boolean, default : False | ||
Whether or not to return the lower triangular correlation | ||
matrix | ||
|
||
Returns | ||
------- | ||
y : DataFrame | ||
|
@@ -6741,7 +6745,15 @@ 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: | ||
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) | ||
return corr_mat.where(mask) | ||
else: | ||
return corr_mat | ||
|
||
def cov(self, min_periods=None): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,12 @@ def test_corr_invalid_method(self): | |
with tm.assert_raises_regex(ValueError, msg): | ||
df.corr(method="____") | ||
|
||
def test_corr_tri(self): | ||
# GH PR | ||
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 should reference this issue number (22840) |
||
df = pd.DataFrame(np.random.normal(size=(100, 5))) | ||
corr_mat = df.corr(tri=True) | ||
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.
Can you add a versionadded directive here?