pandas.core.window.Rolling.corr

Calculate rolling correlation.

param other
Series, DataFrame, or ndarray, optional

If not supplied then will default to self.

param pairwise
bool, default None

Calculate pairwise combinations of columns within a DataFrame. If other is not specified, defaults to True, otherwise defaults to False. Not relevant for Series. **kwargs Unused.

return

Series or DataFrame Returned object type is determined by the caller of the rolling calculation.

Limitations

DataFrame/Series elements cannot be max/min float/integer. Otherwise SDC and Pandas results are different. Resulting DataFrame/Series has default index and name.

Examples

Calculate rolling correlation.
import pandas as pd
from numba import njit


@njit
def series_rolling_corr():
    series = pd.Series([3, 3, 3, 5, 8])  # Series of 3, 3, 3, 5, 8
    other = pd.Series([3, 4, 4, 4, 8])  # Series of 3, 4, 4, 4, 8
    out_series = series.rolling(4).corr(other)

    return out_series  # Expect series of NaN, NaN, NaN, 0.333333, 0.916949


print(series_rolling_corr())
$ python ./series/rolling/series_rolling_corr.py
0         NaN
1         NaN
2         NaN
3    0.333333
4    0.916949
dtype: float64
Calculate rolling correlation.
import pandas as pd
from numba import njit


@njit
def df_rolling_corr():
    df = pd.DataFrame({'A': [3, 3, 3, 5, 8], 'B': [-3, -3, -3, -5, -8]})
    other = pd.DataFrame({'A': [3, 4, 4, 4, 8], 'B': [-3, -4, -4, -4, -8]})
    out_df = df.rolling(4).corr(other)

    # Expect DataFrame of
    # {'A': [NaN, NaN, NaN, 0.333333, 0.916949],
    #  'B': [NaN, NaN, NaN, 0.333333, 0.916949]}
    return out_df


print(df_rolling_corr())
$ python ./dataframe/rolling/dataframe_rolling_corr.py
          A         B
0       NaN       NaN
1       NaN       NaN
2       NaN       NaN
3  0.333333  0.333333
4  0.916949  0.916949

See also

Series.rolling

Calling object with a Series.

DataFrame.rolling

Calling object with a DataFrame.

Series.corr

Similar method for Series.

DataFrame.corr

Similar method for DataFrame.