I want to get the streak since a value in a specific column changed:
datetime val1 val2 val1-streak val2-streak
2018-04-01 00:00:00 4 1 0 0
2018-05-01 00:00:00 5 2 0 0
2018-06-01 00:00:00 5 2 1 1
2018-07-01 00:00:00 6 2 0 2
2018-08-01 00:00:00 7 2 0 3
2018-09-01 00:00:00 7 3 1 0
2018-10-01 00:00:00 7 3 2 1
2018-11-01 00:00:00 5 2 0 0
For now it is ok to assume that the dattime column is equaly spaced. That way i can just count the "periods" since the value in a target column changed. Ideally the function can additionally count the difference in datetinme since the value changed last.
I found a few solutions on this site but when i looked more closely they did not really describe my problem:
Pandas - count since last transaction
Counting changes in pandas row values?
In pandas, how do you find the time since a column by value changes, when grouped by another column?
Pythonic way to calculate streaks in pandas dataframe
finding streaks in pandas dataframe
https://joshdevlin.com/blog/calculate-streaks-in-pandas/
.... at least I was not able to transfer the given answers to my problem.