pandas.core.window.Rolling.var

Calculate unbiased rolling variance.

Normalized by N-1 by default. This can be changed using the ddof argument.

param ddof
int, default 1

Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. *args, **kwargs For NumPy compatibility. No additional arguments are used.

return

Series or DataFrame Returns the same object type as the caller of the rolling calculation.

Limitations

DataFrame/Series elements cannot be max/min float/integer. Otherwise SDC and Pandas results are different.

Examples

Calculate unbiased rolling variance.
import pandas as pd
from numba import njit


@njit
def series_rolling_var():
    series = pd.Series([4, 3, 5, 2, 6])  # Series of 4, 3, 5, 2, 6
    out_series = series.rolling(3).var()

    return out_series  # Expect series of NaN, NaN, 1.000000, 2.333333, 4.333333


print(series_rolling_var())
$ python ./series/rolling/series_rolling_var.py
0         NaN
1         NaN
2    1.000000
3    2.333333
4    4.333333
dtype: float64
Calculate unbiased rolling variance.
import pandas as pd
from numba import njit


@njit
def df_rolling_var():
    df = pd.DataFrame({'A': [4, 3, 5, 2, 6], 'B': [-4, -3, -5, -2, -6]})
    out_df = df.rolling(3).var()

    # Expect DataFrame of
    # {'A': [NaN, NaN, 1.000000, 2.333333, 4.333333],
    #  'B': [NaN, NaN, 1.000000, 2.333333, 4.333333]}
    return out_df


print(df_rolling_var())
$ python ./dataframe/rolling/dataframe_rolling_var.py
          A         B
0       NaN       NaN
1       NaN       NaN
2  1.000000  1.000000
3  2.333333  2.333333
4  4.333333  4.333333

See also

Series.rolling

Calling object with a Series.

DataFrame.rolling

Calling object with a DataFrame.

Series.var

Similar method for Series.

DataFrame.var

Similar method for DataFrame.