I have a pandas time series which contains cumulative monthly values.
If in a month on a certain date, the value becomes a certain number, I need to set rest of the days to 1000.
E.g.
df:
Date cummulative_value
1/8/2017 -3
1/9/2017 -6
1/10/2017 -72
1/11/2017 500
1/26/2017 575
2/7/2017 -5
2/14/2017 -6
2/21/2017 -6
My cutoff value is -71 so in above example I need to achieve the following:
Date cummulative_value
1/8/2017 -3
1/9/2017 -6
1/10/2017 1000
1/11/2017 1000
1/26/2017 1000
2/7/2017 -5
2/14/2017 -6
2/21/2017 -6
I am trying to leverage groupby
in pandas but I am not sure how to go about it. Any other more efficient way will help also.
Here's one approach that involves creating a mask:
Use
groupby
andcumprod
:Output: