根据文件记录,大熊猫的指数加权移动平均值采用“聚合”方法:
exp_window.aggregate?
Signature: exp_window.aggregate(arg, *args, **kwargs)
Docstring:
Aggregate using input function or dict of {column ->
function}
Parameters
----------
arg : function or dict
Function to use for aggregating groups. If a function, must either
work when passed a DataFrame or when passed to DataFrame.apply. If
passed a dict, the keys must be DataFrame column names.
Accepted Combinations are:
- string cythonized function name
- function
- list of functions
- dict of columns -> functions
- nested dict of names -> dicts of functions
但它似乎不起作用。 这是我的例子:
import numpy as np
import pandas as pd
def my_mean(frame):
'''
Calculate the mean excluding min and max observations
'''
return frame.mean()**2
df = pd.DataFrame(np.random.rand(100), columns = ['A'])
exp_window = df.ewm(halflife = 21)
exp_window.agg(my_mean)
我收到以下错误:
Traceback (most recent call last):
File "<ipython-input-116-22166e93e6ea>", line 14, in <module>
exp_window.agg(my_mean)
File "C:\Users\flabriol\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\window.py", line 1228, in aggregate
return super(EWM, self).aggregate(arg, *args, **kwargs)
File "C:\Users\flabriol\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\window.py", line 240, in aggregate
return self.apply(arg, args=args, kwargs=kwargs)
File "C:\Users\flabriol\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\window.py", line 124, in __getattr__
(type(self).__name__, attr))
AttributeError: 'EWM' object has no attribute 'apply'
我知道EWM已经支持.mean()和.std()方法,但在这里我想应用自定义函数。