-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: update the DataFrame.cov docstring #20245
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -5501,7 +5501,22 @@ def corr(self, method='pearson', min_periods=1): | |
|
||
def cov(self, min_periods=None): | ||
""" | ||
Compute pairwise covariance of columns, excluding NA/null values | ||
Compute pairwise covariance of columns, excluding NA/null values. | ||
|
||
Compute the pairwise covariance among the series of a DataFrame. | ||
The returned data frame is the `covariance matrix | ||
<https://en.wikipedia.org/wiki/Covariance_matrix>`__ of the columns | ||
of the DataFrame. | ||
|
||
Both NA and null values are automatically excluded from the | ||
calculation. (See the note below about bias from missing values.) | ||
A threshold can be set for the minimum number of | ||
observations for each value created. Comparisons with observations | ||
below this threshold will be returned as ``NaN``. | ||
|
||
This method is generally used for the analysis of time series data to | ||
understand the relationship between different measures | ||
across time. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -5511,12 +5526,71 @@ def cov(self, min_periods=None): | |
|
||
Returns | ||
------- | ||
y : DataFrame | ||
DataFrame | ||
The covariance matrix of the series of the DataFrame. | ||
|
||
See Also | ||
-------- | ||
Series.cov : compute covariance with another Series | ||
core.window.EWM.cov: expoential weighted sample covariance | ||
core.window.Expanding.cov : expanding sample covariance | ||
core.window.Rolling.cov : rolling sample covariance | ||
|
||
Notes | ||
----- | ||
`y` contains the covariance matrix of the DataFrame's time series. | ||
The covariance is normalized by N-1 (unbiased estimator). | ||
Returns the covariance matrix of the DataFrame's time series. | ||
The covariance is normalized by N-1. | ||
|
||
For DataFrames that have Series that are missing data (assuming that | ||
data is `missing at random | ||
<https://en.wikipedia.org/wiki/Missing_data#Missing_at_random>`__) | ||
the returned covariance matrix will be an unbiased estimate | ||
of the variance and covariance between the member Series. | ||
|
||
However, for many applications this estimate may not be acceptable | ||
because the estimate covariance matrix is not guaranteed to be positive | ||
semi-definite. This could lead to estimate correlations having | ||
absolute values which are greater than one, and/or a non-invertible | ||
covariance matrix. See `Estimation of covariance matrices | ||
<http://en.wikipedia.org/w/index.php?title=Estimation_of_covariance_ | ||
matrices>`__ for more details. | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame([(1, 2), (0, 3), (2, 0), (1, 1)], | ||
... columns=['dogs', 'cats']) | ||
>>> df.cov() | ||
dogs cats | ||
dogs 0.666667 -1.000000 | ||
cats -1.000000 1.666667 | ||
|
||
>>> np.random.seed(42) | ||
>>> df = pd.DataFrame(np.random.randn(1000, 5), | ||
... columns=['a', 'b', 'c', 'd', 'e']) | ||
>>> df.cov() | ||
a b c d e | ||
a 0.998438 -0.020161 0.059277 -0.008943 0.014144 | ||
b -0.020161 1.059352 -0.008543 -0.024738 0.009826 | ||
c 0.059277 -0.008543 1.010670 -0.001486 -0.000271 | ||
d -0.008943 -0.024738 -0.001486 0.921297 -0.013692 | ||
e 0.014144 0.009826 -0.000271 -0.013692 0.977795 | ||
|
||
**Minimum number of periods** | ||
|
||
This method also supports an optional ``min_periods`` keyword | ||
that specifies the required minimum number of observations for each | ||
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. maybe non-NA observations, just to be 100% clear :) |
||
column pair in order to have a valid result: | ||
|
||
>>> np.random.seed(42) | ||
>>> df = pd.DataFrame(np.random.randn(20, 3), | ||
... columns=['a', 'b', 'c']) | ||
>>> df.loc[df.index[:5], 'a'] = np.nan | ||
>>> df.loc[df.index[5:10], 'b'] = np.nan | ||
>>> df.cov(min_periods=12) | ||
a b c | ||
a 0.316741 NaN -0.150812 | ||
b NaN 1.248003 0.191417 | ||
c -0.150812 0.191417 0.895202 | ||
""" | ||
numeric_df = self._get_numeric_data() | ||
cols = numeric_df.columns | ||
|
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.
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.
These should all start with
pandas.
so that the are linked correctly.