- API reference
- Resampling
- pandas.core....
pandas.core.resample.Resampler.aggregate#
- finalResampler.aggregate(func=None,*args,**kwargs)[source]#
Aggregate using one or more operations over the specified axis.
- Parameters:
- funcfunction, str, list or dict
Function to use for aggregating the data. If a function, must eitherwork when passed a DataFrame or when passed to 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 tofunc.
- **kwargs
Keyword arguments to pass tofunc.
- 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
See also
DataFrame.groupby.aggregateAggregate using callable, string, dict, or list of string/callables.
DataFrame.resample.transformTransforms the Series on each group based on the given function.
DataFrame.aggregateAggregate using one or more operations over the specified axis.
Notes
The aggregation operations are always performed over an axis, either theindex (default) or the column axis. This behavior is different fromnumpy aggregation functions (mean,median,prod,sum,std,var), where the default is to compute the aggregation of the flattenedarray, e.g.,
numpy.mean(arr_2d)as opposed tonumpy.mean(arr_2d,axis=0).agg is an alias foraggregate. Use the alias.
Functions that mutate the passed object can produce unexpectedbehavior or errors and are not supported. SeeMutating with User Defined Function (UDF) methodsfor more details.
A passed user-defined-function will be passed a Series for evaluation.
Examples
>>>s=pd.Series([1,2,3,4,5],...index=pd.date_range('20130101',periods=5,freq='s'))>>>s2013-01-01 00:00:00 12013-01-01 00:00:01 22013-01-01 00:00:02 32013-01-01 00:00:03 42013-01-01 00:00:04 5Freq: s, dtype: int64
>>>r=s.resample('2s')
>>>r.agg("sum")2013-01-01 00:00:00 32013-01-01 00:00:02 72013-01-01 00:00:04 5Freq: 2s, dtype: int64
>>>r.agg(['sum','mean','max']) sum mean max2013-01-01 00:00:00 3 1.5 22013-01-01 00:00:02 7 3.5 42013-01-01 00:00:04 5 5.0 5
>>>r.agg({'result':lambdax:x.mean()/x.std(),...'total':"sum"}) result total2013-01-01 00:00:00 2.121320 32013-01-01 00:00:02 4.949747 72013-01-01 00:00:04 NaN 5
>>>r.agg(average="mean",total="sum") average total2013-01-01 00:00:00 1.5 32013-01-01 00:00:02 3.5 72013-01-01 00:00:04 5.0 5