How to calculate rolling cumulative product on Pandas DataFrame
rolling_apply
has been dropped in pandas and replaced by more versatile
window methods (e.g. rolling()
etc.)
# Both agg and apply will give you the same answer
(1+df).rolling(window=12).agg(np.prod) - 1
# BUT apply(raw=True) will be much FASTER!
(1+df).rolling(window=12).apply(np.prod, raw=True) - 1
will this do?
import pandas as pd
import numpy as np
# your DataFrame; df = ...
pd.rolling_apply(df, 12, lambda x: np.prod(1 + x) - 1)