我正在尝试以滚动方式计算交易量加权平均价格.
要做到这一点,我有一个函数vwap为我这样做,像这样:
def vwap(bars):
return ((bars.Close*bars.Volume).sum()/bars.Volume.sum()).round(2)
Run Code Online (Sandbox Code Playgroud)
当我尝试将此函数与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'
Run Code Online (Sandbox Code Playgroud)
这个错误对我有意义,因为rolling_apply不需要DataSeries或ndarray作为输入而不是dataFrame ..我正在这样做.
有没有办法使用rolling_apply到DataFrame来解决我的问题?
我正在编写一个代码,它将滚动窗口应用于将返回多列的函数。
输入:Pandas Series
预期输出:3 列 DataFrame
def fun1(series, ):
# Some calculations producing numbers a, b and c
return {"a": a, "b": b, "c": c}
res.rolling('21 D').apply(fun1)
Run Code Online (Sandbox Code Playgroud)
资源内容:
time
2019-09-26 16:00:00 0.674969
2019-09-26 16:15:00 0.249569
2019-09-26 16:30:00 -0.529949
2019-09-26 16:45:00 -0.247077
2019-09-26 17:00:00 0.390827
...
2019-10-17 22:45:00 0.232998
2019-10-17 23:00:00 0.590827
2019-10-17 23:15:00 0.768991
2019-10-17 23:30:00 0.142661
2019-10-17 23:45:00 -0.555284
Length: 1830, dtype: float64
Run Code Online (Sandbox Code Playgroud)
错误:
TypeError: must be real number, not dict
Run Code Online (Sandbox Code Playgroud)
我尝试过的: