pandas.core.resample.Resampler.apply¶
- Resampler.apply(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 either work 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 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
DataFrame.groupby.aggregate
Aggregate using callable, string, dict, or list of string/callables.
DataFrame.resample.transform
Transforms the Series on each group based on the given function.
DataFrame.aggregate
Aggregate using one or more operations over the specified axis.
Notes
agg is an alias for aggregate. Use the alias.
Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See Mutating with User Defined Function (UDF) methods for 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')) >>> s 2013-01-01 00:00:00 1 2013-01-01 00:00:01 2 2013-01-01 00:00:02 3 2013-01-01 00:00:03 4 2013-01-01 00:00:04 5 Freq: S, dtype: int64
>>> r = s.resample('2s')
>>> r.agg(np.sum) 2013-01-01 00:00:00 3 2013-01-01 00:00:02 7 2013-01-01 00:00:04 5 Freq: 2S, dtype: int64
>>> r.agg(['sum', 'mean', 'max']) sum mean max 2013-01-01 00:00:00 3 1.5 2 2013-01-01 00:00:02 7 3.5 4 2013-01-01 00:00:04 5 5.0 5
>>> r.agg({'result': lambda x: x.mean() / x.std(), ... 'total': np.sum}) result total 2013-01-01 00:00:00 2.121320 3 2013-01-01 00:00:02 4.949747 7 2013-01-01 00:00:04 NaN 5
>>> r.agg(average="mean", total="sum") average total 2013-01-01 00:00:00 1.5 3 2013-01-01 00:00:02 3.5 7 2013-01-01 00:00:04 5.0 5