Applying an iterable mask, checking it against a value - if value doesn't satisfy the mask condition, move to the next value which does

Viewed 109

I currently have some code where I've created a mask which checks to see if a variable matches the first position in a sequence, called index_pos_overload. If it matches, the variable is chosen, and the check ends. However, I want to be able to use this mask to not only check if the number satisfies the condition of the mask, but if it doesn't move along to the next value in the sequence which does. It's essentially to pick out a row in my pandas data column, hyst. My code currently looks like this:

import pandas as pd
from itertools import chain

hyst = pd.DataFrame({"test":[12, 4, 5, 4, 1, 3, 2, 5, 10, 9, 7, 5, 3, 6, 3, 2 ,1, 5, 2]})

possible_overload_cycle = 1

index_pos_overload = chain.from_iterable((hyst.index[i]) 
                                                    for i in range(0, len(hyst)-1, 5))

if (possible_overload_cycle == index_pos_overload):
    hyst_overload_cycle = possible_overload_cycle
else:
    hyst_overload_cycle = 5 #next value in iterable where index_pos_overload is true

The expected output of hyst_overload_cycle should be this:

print(hyst_overload_cycle)

5

I've included my logic as to how I think this should work - possible_overload_cycle = 1 does not point to the first position in the dataframe, so hyst_overload_cycle should return as 5, the first position in the mask. I hope I've made sense, as I can't quite seem to work out how I would go about this programatically.

1 Answers

If I understood you correctly, it may be simpler than you think:

  • index_pos_overload can be an array / list, there is no need to use complex constructs to store a sequence of values
  • to find the first non-zero value from index_pos_overload, one can simply use np.nonzero()[0][0] (the first [0] is to select the dimension, the second is to select the index within that axis) and use array indexing of that on the original index_pos_overload array

The code would look like:

import numpy as np
import pandas as pd


hyst = pd.DataFrame({"test":[12, 4, 5, 4, 1, 3, 2, 5, 10, 9, 7, 5, 3, 6, 3, 2 ,1, 5, 2]})

possible_overload_cycle = 1
index_pos_overload = np.array([hyst.index[i] for i in range(0, len(hyst)-1, 5)])

if possible_overload_cycle in index_pos_overload:
    hyst_overload_cycle = possible_overload_cycle
else:
    hyst_overload_cycle = index_pos_overload[np.nonzero(index_pos_overload)[0][0]]
print(hyst_overload_cycle)
# 5
Related