Suppose I have a Pandas series of boolean values like so.
vals = pd.Series([0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1]).astype(bool)
>>> vals
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 False
8 False
9 True
10 True
11 False
12 True
13 True
14 True
dtype: bool
I want to turn this boolean Series into a series where each group of 1's is properly enumerated, like so
0 0
1 0
2 0
3 1
4 1
5 1
6 1
7 0
8 0
9 2
10 2
11 0
12 3
13 3
14 3
How can I do this efficiently?
I have been able to do so manually, looping over the series on a Python level and incrementing, but this is obviously slow. I'm looking for a vectorized solution - I saw this answer from unutbu concerning splitting on increasing groups in NumPy, and was trying to get that to work with a cumsum
of some sort but have been unsuccessful so far.