apply custom function on pandas dataframe on a rolling window

回眸只為那壹抹淺笑 提交于 2020-07-09 19:37:02

问题


Suppose you have a dataframe with 1000 closing prices. You want to apply a risk calculation function (let's say VaR) named compute_var() on last 90 closing prices, on a rolling basis. How would you do it? I presume with apply():

def compute_var(df):
       return do_calculations_on(df[-90:])

def compute_rolling_var(self):
       self.var = self.closing.apply(compute_var)

Problem is that .apply only passes 1 day closing to compute_var, and not a dataframe. So it gives an error.

The only working solution I found is with iteration-style algo (.iterrow()): I pass the iteration index to compute_var and it crops the closing dataframe self.closing[:i] before performing calculation on the last 90 rows, then it populates the df.var dataframe via .loc(i) = computer_var_value.

I suspect there is a better way.


回答1:


answer is apply_rolling as underlined by EdChum + min_periods adjustment

Problem came from a few NaN values in input data, and min_periods=None by default, which reacts as if no NaN value is allowed in your window (90 days here). Seems very counter-intuitive to me, but setting min_periods=1 resolved my issue.



来源:https://stackoverflow.com/questions/30806838/apply-custom-function-on-pandas-dataframe-on-a-rolling-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!