2

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
thenatlog
  • 81
  • 1
  • 7
  • why is the item value False in your desired result. 1 > 0 so should be True ? – dermen Oct 05 '22 at 22:42
  • @dermen the conditional flag starts when the value is 2 or greater and then terminates on the 0. ill update it in the question – thenatlog Oct 05 '22 at 22:44

1 Answers1

1

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])
dermen
  • 5,252
  • 4
  • 23
  • 34
  • this is very close, the [1] at the 7th index is getting counted as True when it should be False – thenatlog Oct 05 '22 at 23:13
  • Oh I see, I was too hasty. New solution is less pretty.. and perhaps doesnt get all edge cases (if there are no 2s for example).. but it seems to match your desired result – dermen Oct 05 '22 at 23:27