我正在试着以滚动的方式计算数量加权平均价格。
为此,我有一个函数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。。我做这件事的方式。
有没有办法使用滚动应用到数据帧来解决我的问题?
修改@mathick的答案以包含
na_fill
。还要注意,函数f
需要返回一个值,这不能返回包含多个列的数据帧。清理后的版本供参考,希望索引正确:
这不是直接启用的,但是您可以这样做
相关问题 更多 >
编程相关推荐