I am using np.select() to construct an ndarray with values of either 1, -1, or 0, depending on some conditions. It is possible that none of these will be met, so I need a default value. I would like this value to be the value that the array holds in the previous index, if that makes sense. My naive code, which runs on some columns of a DataFrame named "total" and which raises an error, is below:
condlist = [total.ratios > total.s_entry, total.ratios < total.b_entry, (total.ratios > total.b_entry) & (total.ratios < total.s_entry)]
choicelist = (-1, 1, 0)
pos1 = pd.Series(np.select(condlist, choicelist, pos1))
Is there a way to do what I am asking? For example, having the array start
1
1
0
-1
-1
and then the sixth element doesn't satisfy any of the conditions, so its value defaults to -1 due to that being the most recent value of the array?