相关疑难解决方法(0)

在DataFrame对象上使用rolling_apply

我正在尝试以滚动方式计算交易量加权平均价格.

要做到这一点,我有一个函数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来解决我的问题?

python pandas

10
推荐指数
1
解决办法
7901
查看次数

Pandas 适用于多列输出的滚动

我正在编写一个代码,它将滚动窗口应用于将返回多列的函数。

输入: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)

我尝试过的:

  • 在 apply 中更改 raw=True
  • 在 apply 中使用 …

python dataframe pandas rolling-computation

7
推荐指数
1
解决办法
4104
查看次数

标签 统计

pandas ×2

python ×2

dataframe ×1

rolling-computation ×1