Measure pairwise co-movement
Assess how numeric measures vary together in exploratory or time-series analysis.
Pandas · Module
Computes pairwise covariance among DataFrame columns and returns the results as a covariance matrix. Missing values are excluded, and min_periods can require a minimum number of observations per pair.
Call DataFrame.cov to create a covariance matrix describing pairwise covariance among DataFrame columns. Missing values are excluded pairwise, and min_periods can require enough observations before a result is produced.
DataFrame.cov(min_periods=None, ddof=1, numeric_only=False)| Name | Type | Description |
|---|---|---|
min_periods | int, optional | Minimum number of observations required per pair of columns to have a valid result. |
ddof | int, default 1 | Delta degrees of freedom; the divisor is N - ddof. |
numeric_only | bool, default False | Whether to include only float, int, or boolean data. |
DataFrame: the covariance matrix of the DataFrame's series.
import pandas as pd
sales = pd.DataFrame({
"online": [10, 12, 9, 15],
"store": [8, 11, 10, 13],
"region": ["N", "N", "S", "S"],
})
covariance = sales.cov(numeric_only=True)
print(covariance)Computes the covariance matrix for the numeric columns while excluding the text column.
Assess how numeric measures vary together in exploratory or time-series analysis.
Prevent sparse column pairs from producing misleading estimates.
Exclude labels or other nonnumeric columns from the matrix.
DataFrame.cov returns a DataFrame whose entries are pairwise covariances between the original columns. The calculation excludes NA and null values.
Follow-up: How can insufficient data be handled? Set min_periods so each pair needs a required number of observations; pairs below that threshold return NaN.
min_periods specifies the minimum number of observations required for each column pair to receive a valid result. It is useful when pairwise missing data could otherwise leave too few observations.
Follow-up: What happens when the threshold is not met? The corresponding matrix entry is NaN.
ddof is the delta degrees of freedom, and pandas divides by N - ddof. numeric_only controls whether only float, integer, and boolean columns are included.
Follow-up: What default should be remembered? ddof defaults to 1, while numeric_only defaults to False in the current documentation.
pandas.Series.covpandas.DataFrame.corr