Skip to content
Advertisement

Get first value less than x using np.select / ValueError: -1 is not in range

I have come across an issue with the np.select section of my code and have reproduced a minimal-reproducible-example to seek some advice as to why ValueError: -1 is not in range is being returned rather than nan

JavaScript

Using df['number'][3] when number = 1 I would expect to return nan since the value located in df['number'][3] is 4 and although number = 1 is less than 4, there are is no row above the row index in df['number'] where the value is 1

Instead I get ValueError: -1 is not in range instead of nan

Advertisement

Answer

Code modification to avoid the error mentioned above – it is just a typical version of the original code but with minor modification and if statement:

JavaScript
Advertisement