I try to create a new column CumReturn
in a Dataframe df
with the cumulative product over the month. I try to reset the cum_prod() to 1 at the end of each month (if EndMonth == 1) and start new with the cumulative product.
df:
Date EndMonth ID1 Return
2023-01-30 0 A 0.95
2023-01-30 0 B 0.98
2023-01-31 1 A 1.01
2023-01-31 1 B 1.02
2023-02-01 0 A 1.05
2023-02-01 0 B 0.92
2023-02-02 0 A 0.97
2023-02-02 0 B 0.99
I tried it to do with: df['CumReturn'] = np.where(df['EndMonth'] == 1, 1, df['Return'].groupby('ID1').cumprod())
When I do that, I get for 2023-02-02
the cumulative product over the whole period and not only since the start of February.
For reproducability:
import pandas as pd
df1 = pd.DataFrame({
'Date':['2023-01-30', '2023-01-30', '2023-01-31', '2023-01-31', '2023-02-01', '2023-02-01', '2023-02-02', '2023-02-02'],
'EndMonth':[0, 0, 1, 1, 0, 0, 0, 0],
'ID1':['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
'Return':[0.95, 0.98, 1.01, 1.02, 1.05, 0.92, 0.97, 0.99]})
df1 = df1.set_index('Date')
Many thanks!