pandas.core.window.Rolling.aggregate

Rolling.aggregate(self, arg, *args, **kwargs)[source]

Aggregate using one or more operations over the specified axis.

Parameters:
func : function, str, list or dict

Function to use for aggregating the data. If a function, must either work when passed a Series/Dataframe or when passed to Series/Dataframe.apply.

Accepted combinations are:

  • function
  • string function name
  • list of functions and/or function names, e.g. [np.sum, 'mean']
  • dict of axis labels -> functions, function names or list of such.
*args

Positional arguments to pass to func.

**kwargs

Keyword arguments to pass to func.

Returns:
scalar, Series or DataFrame

The return can be:

  • scalar : when Series.agg is called with single function
  • Series : when DataFrame.agg is called with a single function
  • DataFrame : when DataFrame.agg is called with several functions

Return scalar, Series or DataFrame.

See also

Series.rolling
DataFrame.rolling

Notes

agg is an alias for aggregate. Use the alias.

A passed user-defined-function will be passed a Series for evaluation.

Examples

>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
>>> df
          A         B         C
0 -2.385977 -0.102758  0.438822
1 -1.004295  0.905829 -0.954544
2  0.735167 -0.165272 -1.619346
3 -0.702657 -1.340923 -0.706334
4 -0.246845  0.211596 -0.901819
5  2.463718  3.157577 -1.380906
6 -1.142255  2.340594 -0.039875
7  1.396598 -1.647453  1.677227
8 -0.543425  1.761277 -0.220481
9 -0.640505  0.289374 -1.550670
>>> df.rolling(3).sum()
          A         B         C
0       NaN       NaN       NaN
1       NaN       NaN       NaN
2 -2.655105  0.637799 -2.135068
3 -0.971785 -0.600366 -3.280224
4 -0.214334 -1.294599 -3.227500
5  1.514216  2.028250 -2.989060
6  1.074618  5.709767 -2.322600
7  2.718061  3.850718  0.256446
8 -0.289082  2.454418  1.416871
9  0.212668  0.403198 -0.093924
>>> df.rolling(3).agg({'A':'sum', 'B':'min'})
          A         B
0       NaN       NaN
1       NaN       NaN
2 -2.655105 -0.165272
3 -0.971785 -1.340923
4 -0.214334 -1.340923
5  1.514216 -1.340923
6  1.074618  0.211596
7  2.718061 -1.647453
8 -0.289082 -1.647453
9  0.212668 -1.647453
Scroll To Top