This question is unlike other similar ones that I could find because I am trying to combine a lookback window and a threshold into one rolling sum. I'm not actually sure what I'm trying to do is achievable in one step:
I have a pandas dataframe with a datetime column and a value column. I have created a column that sums the value column (V) over a rolling time window. However I would like this rolling sum to reset to 0 once it reaches a certain threshold.
I don't know if it's possible to do this in one column manipulation step since there are two conditions at play at each step in the sum- the lookback window and the threshold. If anyone has any ideas about if this is possible and how I might be able to achieve it please let me know. I know how to do this iteratively however it is very very slow (my dataframe has >1 million entries).
Example:
Lookback time: 3 minutes
Threshold: 3
+---+-----------------------+-------+--------------------------+
| | myDate | V | rolling | desired_column |
+---+-----------------------+-------+---------+----------------+
| 1 | 2020-04-01 10:00:00 | 0 | 0 | 0 |
| 2 | 2020-04-01 10:01:00 | 1 | 1 | 1 |
| 3 | 2020-04-01 10:02:00 | 2 | 3 | 3 |
| 4 | 2020-04-01 10:03:00 | 1 | 4 | 1 |
| 5 | 2020-04-01 10:04:00 | 0 | 4 | 1 |
| 6 | 2020-04-01 10:05:00 | 4 | 7 | 5 |
| 7 | 2020-04-01 10:06:00 | 1 | 6 | 1 |
| 8 | 2020-04-01 10:07:00 | 1 | 6 | 2 |
| 9 | 2020-04-01 10:08:00 | 0 | 6 | 0 |
| 10| 2020-04-01 10:09:00 | 3 | 5 | 5 |
+---+-----------------------+-------+---------+----------------+
In this example the sum rulling sum will not take into account any values on or before a row that breaches (or is equal to) the threshold of 3.