【发布时间】:2020-10-24 05:54:14
【问题描述】:
我正在编写将滚动窗口应用于返回多列的函数的代码。
输入:熊猫系列
预期输出: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)
资源内容:
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
我尝试过的:
- 在应用中更改 raw=True
- 在应用中使用 lambda 函数
- 以列表/numpy 数组/数据帧/系列的形式在 fun1 中返回结果。
我也浏览过很多关于 SO 的相关帖子,仅举几例:
- Pandas - Using `.rolling()` on multiple columns
- Returning two values from pandas.rolling_apply
- How to apply a function to two columns of Pandas dataframe
- Apply pandas function to column to create multiple new columns?
但是指定的解决方案都没有解决这个问题。
有没有直接的解决方案?
【问题讨论】:
标签: python pandas dataframe rolling-computation