pandas.core.window.expanding.Expanding.corr#

Expanding.corr(other=None, pairwise=None, ddof=1, numeric_only=False)[source]#

Calculate the expanding correlation.

Parameters:
otherSeries or DataFrame, optional

If not supplied then will default to self and produce pairwise output.

pairwisebool, default None

If False then only matching columns between self and other will be used and the output will be a DataFrame. If True then all pairwise combinations will be calculated and the output will be a MultiIndexed DataFrame in the case of DataFrame inputs. In the case of missing elements, only complete pairwise observations will be used.

ddofint, default 1

Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements.

numeric_onlybool, default False

Include only float, int, boolean columns.

Added in version 1.5.0.

Returns:
Series or DataFrame

Return type is the same as the original object with np.float64 dtype.

See also

cov

Similar method to calculate covariance.

numpy.corrcoef

NumPy Pearson’s correlation calculation.

Series.expanding

Calling expanding with Series data.

DataFrame.expanding

Calling expanding with DataFrames.

Series.corr

Aggregating corr for Series.

DataFrame.corr

Aggregating corr for DataFrame.

Notes

This function uses Pearson’s definition of correlation (https://en.wikipedia.org/wiki/Pearson_correlation_coefficient).

When other is not specified, the output will be self correlation (e.g. all 1’s), except for DataFrame inputs with pairwise set to True.

Function will return NaN for correlations of equal valued sequences; this is the result of a 0/0 division error.

When pairwise is set to False, only matching columns between self and other will be used.

When pairwise is set to True, the output will be a MultiIndex DataFrame with the original index on the first level, and the other DataFrame columns on the second level.

In the case of missing elements, only complete pairwise observations will be used.

Examples

>>> ser1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
>>> ser2 = pd.Series([10, 11, 13, 16], index=['a', 'b', 'c', 'd'])
>>> ser1.expanding().corr(ser2)
a         NaN
b    1.000000
c    0.981981
d    0.975900
dtype: float64