pandasboolean-logicbooleanquery

pandas - index data that comes after conditional


i have the following time series

[0,1,2,3,2,1,0,1,2,3,2,1,0]

i would like to boolean index all values that:

  1. include & come after 2
  2. are greater than 0
  3. terminates on 0

if the conditions are met, the following vector should be produced

[False,False,True,True,True,True,False,False,True,True,True,True,False]

i have attempted to solve it with a combination of logical queries, but to no avail

frame['boolean'] = False
frame['boolean'].loc[(frame['sequence'].gt(2)) & (frame['boolean'].shift(1).eq(False)] = True

Solution

  • Id use numpy for this (it works well with pandas Series)

    import numpy as np
    a = np.array([0,1,2,3,2,1,0,1,2,3,2,1,0])  
    
    result = a > 0
    where_zero = np.where(a==0)[0]
    where_two = list(np.where(a==2)[0])
    # note if where_two is an empty list, then the result should simply be all False, right ? 
    for x1 in where_zero:
        while 1: 
            try:
                x2 = where_two.pop(0)
            except IndexError:
                break
            if x2 > x1:
                break
    
        result[x1:x2] = False
    
    # result
    #array([False, False,  True,  True,  True,  True, False, False,  True,
    #        True,  True,  True, False])