【发布时间】:2013-10-07 22:44:23
【问题描述】:
我正在尝试滚动计算成交量加权平均价格。
为此,我有一个函数 vwap 可以为我执行此操作,如下所示:
def vwap(bars):
return ((bars.Close*bars.Volume).sum()/bars.Volume.sum()).round(2)
当我尝试将此函数与 rolling_apply 一起使用时,如图所示,出现错误:
import pandas.io.data as web
bars = web.DataReader('AAPL','yahoo')
print pandas.rolling_apply(bars,30,vwap)
AttributeError: 'numpy.ndarray' object has no attribute 'Close'
这个错误对我来说很有意义,因为 rolling_apply 不需要 DataSeries 或 ndarray 作为输入,而不是 dataFrame.. 我这样做的方式。
有没有办法使用 rolling_apply 到 DataFrame 来解决我的问题?
【问题讨论】:
-
stackoverflow.com/questions/37486502/… 的可能重复项。在那里查看我的答案。