pandas.Series.expanding#

Series.expanding(min_periods=1, method='single')[source]#

Provide expanding window calculations.

An expanding window yields the value of an aggregation statistic with all the data available up to that point in time.

Parameters:
min_periodsint, default 1

Minimum number of observations in window required to have a value; otherwise, result is np.nan.

methodstr {‘single’, ‘table’}, default ‘single’

Execute the rolling operation per single column or row ('single') or over the entire object ('table').

This argument is only implemented when specifying engine='numba' in the method call.

New in version 1.3.0.

Returns:
pandas.api.typing.Expanding

An instance of Expanding for further expanding window calculations, e.g. using the sum method.

See also

rolling

Provides rolling window calculations.

ewm

Provides exponential weighted functions.

Notes

See Windowing Operations for further usage details and examples.

Examples

>>> df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
>>> df
     B
0  0.0
1  1.0
2  2.0
3  NaN
4  4.0

min_periods

Expanding sum with 1 vs 3 observations needed to calculate a value.

>>> df.expanding(1).sum()
     B
0  0.0
1  1.0
2  3.0
3  3.0
4  7.0
>>> df.expanding(3).sum()
     B
0  NaN
1  NaN
2  3.0
3  3.0
4  7.0