pandas.core.window.rolling.Rolling.sum — pandas 2.2.3 documentation (original) (raw)

Rolling.sum(numeric_only=False, engine=None, engine_kwargs=None)[source]#

Calculate the rolling sum.

Parameters:

numeric_onlybool, default False

Include only float, int, boolean columns.

Added in version 1.5.0.

enginestr, default None

engine_kwargsdict, default None

Returns:

Series or DataFrame

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

Notes

See Numba engine and Numba (JIT compilation) for extended documentation and performance considerations for the Numba engine.

Examples

s = pd.Series([1, 2, 3, 4, 5]) s 0 1 1 2 2 3 3 4 4 5 dtype: int64

s.rolling(3).sum() 0 NaN 1 NaN 2 6.0 3 9.0 4 12.0 dtype: float64

s.rolling(3, center=True).sum() 0 NaN 1 6.0 2 9.0 3 12.0 4 NaN dtype: float64

For DataFrame, each sum is computed column-wise.

df = pd.DataFrame({"A": s, "B": s ** 2}) df A B 0 1 1 1 2 4 2 3 9 3 4 16 4 5 25

df.rolling(3).sum() A B 0 NaN NaN 1 NaN NaN 2 6.0 14.0 3 9.0 29.0 4 12.0 50.0