-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add axis argument to Dataframe.corr #35984
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6ef933
Added axis argument to Dataframe.corr
kc611 3648aeb
Minor Linting Issues
kc611 c588bc4
Minor Whitespace Issue
kc611 0dbde9d
Changed Failing Test
kc611 feecd8d
Updated Documantation
kc611 91c1e3e
Debug Modified Dataframe.corr function
kc611 fb89cbe
Removed PEP 8 Issues-Trailing Whitespaces
kc611 d2a87f8
Made suitable changes according to review
kc611 725f36a
Added whatsnew note
kc611 f1c884b
Improved tests
kc611 0552fa4
Update doc/source/whatsnew/v1.2.0.rst
kc611 3c4f88c
Added version added directive
kc611 0f1e817
Merge branch 'issue_35002' of https://github.com/kc611/pandas into is…
kc611 5871508
Added type annotations
kc611 b98401b
Resolved Pep8 whitespace issues
kc611 8fc6cda
Solved linter Issue
kc611 f2e6e84
Solved Linter Issues
kc611 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8116,9 +8116,14 @@ def _series_round(s, decimals): | |
# ---------------------------------------------------------------------- | ||
# Statistical methods, etc. | ||
|
||
def corr(self, method="pearson", min_periods=1) -> "DataFrame": | ||
def corr(self, method="pearson", min_periods=1, axis=0) -> "DataFrame": | ||
""" | ||
Compute pairwise correlation of columns, excluding NA/null values. | ||
Compute pairwise correlation of rows or columns, excluding NA/null values. | ||
|
||
Pairwise correlation is computed between rows or columns of | ||
a DataFrame. Returned Dataframe is a Correlation matrix between | ||
pairwise rows or columns. Both NA and null values are automatically | ||
excluded from the calculation. | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Parameters | ||
---------- | ||
|
@@ -8140,6 +8145,10 @@ def corr(self, method="pearson", min_periods=1) -> "DataFrame": | |
to have a valid result. Currently only available for Pearson | ||
and Spearman correlation. | ||
|
||
axis : {0 or 'index', 1 or 'columns'}, default 0 | ||
The axis to use. 0 or 'index' to compute column-wise, 1 or 'columns' for | ||
row-wise. | ||
kc611 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Returns | ||
------- | ||
DataFrame | ||
|
@@ -8162,12 +8171,20 @@ def corr(self, method="pearson", min_periods=1) -> "DataFrame": | |
dogs cats | ||
dogs 1.0 0.3 | ||
cats 0.3 1.0 | ||
>>> df.T.corr(method=histogram_intersection, axis=1) | ||
dogs cats | ||
dogs 1.0 0.3 | ||
cats 0.3 1.0 | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
numeric_df = self._get_numeric_data() | ||
cols = numeric_df.columns | ||
axis = numeric_df._get_axis_number(axis) | ||
cols = numeric_df._get_agg_axis(axis) | ||
idx = cols.copy() | ||
mat = numeric_df.to_numpy(dtype=float, na_value=np.nan, copy=False) | ||
|
||
if axis == 1: | ||
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. don't we have to transpose the results? 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 don't think we do since the result is symmetric |
||
mat = mat.transpose() | ||
|
||
if method == "pearson": | ||
correl = libalgos.nancorr(mat, minp=min_periods) | ||
elif method == "spearman": | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.