Compute product with rolling window
In case somebody comes to this: I have solved in the following way:
df = df.rolling(window=5, min_periods=1).apply(lambda x: np.prod(1 + x) - 1)
I have a df:




month
A
B
C
D




1994-07
1
2
NAN
NAN


1994-08
5
2
3
4


1994-09
1
2
1
1


1994-10
1
2
3
1


1994-11
1
NAN
3
1


1995-07
1
2
2
4


1995-08
1
2
3
4




I want, for each column, to get the product of a rolling window of size 5, ignoring NAN values. Which means, in this case:




month
A
B
C
D




1994-11
5
16
27
4


1995-07
5
16
54
16


1995-08
1
16
54
16




For D(1994-11), for example, I would get 4 (4111), and C (1995-07) results in 54 (2331*3). I have tried:
df = df.rolling(window=5,axis=0).apply(prod(min_count=1))
It is an attempt of applying the function product from pandas.
But I get the error "NameError: name 'prod' is not defined"