pandas.tseries.resample.Resampler.apply

Resampler.apply(arg, *args, **kwargs)[source]

对重采样组应用聚合函数或函数,产生最可能的序列,但在某些情况下,根据聚合函数的输出,DataFrame

参数:

func_or_funcs:function或list / dict函数

函数的List / dict将产生具有由函数名称自身(列表)或dict中的键确定的列名称的DataFrame

返回:

系列或DataFrame

也可以看看

transform

笔记

agg是聚合的别名。用它。

例子

>>> s = Series([1,2,3,4,5],
                index=pd.date_range('20130101',
                                    periods=5,freq='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')
DatetimeIndexResampler [freq=<2 * Seconds>, axis=0, closed=left,
                        label=left, convention=start, base=0]
>>> 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})
                     total    result
2013-01-01 00:00:00      3  2.121320
2013-01-01 00:00:02      7  4.949747
2013-01-01 00:00:04      5       NaN
Scroll To Top