apply custom function on pandas dataframe on a rolling window

2k views Asked by At

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

There are 1 answers

1
comte On BEST ANSWER

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.