I'm trying to calculate a 3 month weighted average.
I can perform a simple average, like so:
df.rolling(3)['B'].mean()
However when I pass in a custom function to get a weighted average :
df.rolling(3).apply(lambda x: sum(x['A'] * x['B']) / sum(x['A']),raw='false')
I'm expecting the rolling function to first set the scope of the operation, then then the function to be applied to said scope. However, I'm getting an 'IndexError'
Any ideas on how to resolve (and why conceptually, this doesn't work)?