我正在编写一段代码,它将对一个函数应用滚动窗口,并返回多列数据。
输入:Pandas序列
期望输出:三列DataFrame
输入:Pandas序列
期望输出:三列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)
res的内容:
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
错误:
TypeError: must be real number, not dict
我尝试过以下方法:
- 在apply中更改raw=True
- 在apply中使用lambda函数
- 将fun1中的结果作为列表/NumPy数组/数据帧/系列返回。
我还查阅了许多相关的SO帖子,其中包括:
但是,没有一个特定的解决方案可以解决这个问题。
有没有一种简单的解决方案?