Bill
Bill

Reputation: 11663

Is there a simple way to find the last consecutive True value in a Pandas series?

I have a small dataframe with bool values representing the ranges over which three parameters are optimum.

It looks like this in the debugger:

(Pdb) p insensistive_ranges.loc[-0.2:0.2]
           P      Q      n
-0.20  False   True  False
-0.16  False   True  False
-0.12   True   True  False
-0.08   True   True  False
-0.04   True  False  False
 0.00   True   True   True
 0.04  False  False   True
 0.08  False   True   True
 0.12  False   True  False
 0.16  False   True  False

(The optimum values are when the index == 0.00)

I want to return the index of the last consecutive True value counting up from 0.0 and also counting down from 0.0 for each parameter. In other words, this:

(Pdb) p highest
P    0.00
Q    0.00
n    0.08
(Pdb) p lowest
P   -0.12
Q    0.00
n    0.00

The closest I have come is this, but it goes one step too far in each direction (finds the first non-True value not the last consecutive True value):

(Pdb) p insensistive_ranges.loc[0.0:delta].idxmin()
P    0.04
Q    0.04
n    0.12
(Pdb) p insensistive_ranges.loc[0.0:-delta:-1].idxmin()
P   -0.16
Q   -0.04
n   -0.04

Any ideas?

(Note that you can't start at the beginning or end of insensistive_ranges because there might be other True values in each series that are not consecutive starting from 0.0.

This question has some innovative solutions with numpy array methods but they look pretty complex.

Upvotes: 0

Views: 408

Answers (1)

ALollz
ALollz

Reputation: 59579

We need to create a counter for consecutive groups of True/False. Then find the maximum and minimum index for each column for the 0 group.

df1 = df.ne(df.shift(1)).cumsum().copy()

# Lowest
df1.eq(df1.loc[0]).idxmax()
P   -0.12
Q    0.00
n    0.00
dtype: float64

# Highest
df1.eq(df1.loc[0])[::-1].idxmax()
#P    0.00
#Q    0.00
#n    0.08
#dtype: float64

Upvotes: 1

Related Questions