I would like to use the pandas.rolling_apply
function to apply my own custom function on a rolling window basis.
but my function requires two arguments, and also has two outputs. Is this possible?
Below is a minimum reproducible example...
import pandas as pd
import numpy as np
import random
tmp = pd.DataFrame(np.random.randn(2000,2)/10000,
index=pd.date_range('2001-01-01',periods=2000),
columns=['A','B'])
def gm(df,p):
v =(((df+1).cumprod())-1)*p
return v.iloc[-1]
# an example output when subsetting for just 2001
gm(tmp['2001'],5)
# the aim is to do it on a rolling basis over a 50 day window
# whilst also getting both outputs and also allows me to add in the parameter p=5
# or any other number I want p to be...
pd.rolling_apply(tmp,50,gm)
which leads to an error...since gm takes two arguments...
any help would be greatly appreciated...
EDIT
Following Jeff's comment I have progressed, but am still struggling with two or more column outputs, so if instead i make a new function (below) which just returns two random numbers (unconnected to the previous calculation) instead rather than the last rows of v, I get an error of TypeError: only length-1 arrays can be converted to Python scalars
. This function works if
def gm2(df,p):
df = pd.DataFrame(df)
v =(((df+1).cumprod())-1)*p
return np.random.rand(2)
pd.rolling_apply(tmp,50,lambda x: gm2(x,5)).tail(20)
This function works if 2 is changed to 1...