I am trying to reset a rolling average based on a signal condition. From the point where the condition is true onwards, the previous values should be ignored. This can be done by replacing the history of prior values by the current value at the signal.
idx = pd.date_range(start='2000-01-01',end='2000-01-11')
#df = pd.DataFrame(np.random.normal(size=(len(idx),1)).cumsum(), index=idx, columns=['value'])
df = pd.DataFrame(range(0, 11), index=idx, columns=['value'])
df['signal'] = [0] * 5 + [1] + [0] * 5
df['ma'] = df.value.rolling(3).mean()
df['value2'] = [5] * 6 + list(range(6, 11))
df['ma2'] = df.value2.rolling(3).mean()
value signal ma value2 ma2
2000-01-01 0 0 NaN 5 NaN
2000-01-02 1 0 NaN 5 NaN
2000-01-03 2 0 1.0 5 5.000000
2000-01-04 3 0 2.0 5 5.000000
2000-01-05 4 0 3.0 5 5.000000
2000-01-06 5 1 4.0 5 5.000000
2000-01-07 6 0 5.0 6 5.333333
2000-01-08 7 0 6.0 7 6.000000
2000-01-09 8 0 7.0 8 7.000000
2000-01-10 9 0 8.0 9 8.000000
2000-01-11 10 0 9.0 10 9.000000
In this example value2 is the value the moving average should be calculated on once the signal fires, and ma2 would be the expected result from index 2000-01-06 onward. Up to 2000-01-05 it should keep the original 'ma' values. (values predating the signal should not be affected)
I found a similar request here pandas rolling average with a rolling mask / excluding entries that may be useful but I can't figure out how to apply it to my need.