pandas.core.resample.Resampler.apply#

Resampler.apply(func=None, *args, **kwargs)[源代码]#

沿指定轴使用一个或多个操作进行聚合。

Parameters:
funcfunction, str, list 或 dict

用于聚合数据的函数。如果是函数,则必须能在传递 DataFrame 时工作,或者能在传递给 DataFrame.apply 时工作。

可接受的组合有:

  • function

  • 字符串函数名

  • 函数和/或函数名的列表,例如 [np.sum, 'mean']

  • 轴标签 -> 函数、函数名或其列表的字典。

*args

传递给 func 的位置参数。

**kwargs

传递给 func 的关键字参数。

Returns:
scalar, Series 或 DataFrame

返回值可以是:

  • scalar :当 Series.agg 用单个函数调用时

  • Series :当 DataFrame.agg 用单个函数调用时

  • DataFrame :当 DataFrame.agg 用多个函数调用时

参见

DataFrame.groupby.aggregate

使用可调用对象、字符串、字典或字符串/可调用对象的列表进行聚合。

DataFrame.resample.transform

根据给定函数对每个组中的 Series 进行转换。

DataFrame.aggregate

沿指定轴使用一个或多个操作进行聚合。

Notes

聚合操作始终在某个轴上执行,可以是索引轴(默认)或列轴。此行为与 numpy 聚合函数(meanmedianprodsumstdvar)不同,后者默认计算展平数组的聚合,例如 numpy.mean(arr_2d) 而不是 numpy.mean(arr_2d, axis=0)

aggaggregate 的别名。请使用别名。

修改传入对象的函数可能会导致意外行为或错误,因此不受支持。有关详细信息,请参阅 使用用户定义函数 (UDF) 方法进行变异

传入的用户定义函数将接收一个 Series 进行计算。

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("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': "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