Mike Clayton
Nov 15, 2022

--

Hi Robert. Yes, you can use np.where() for more complex functions, but it is limited to a single statement (if-else). If you need if-elif-else (as per my article), then you cannot use it as a single statement (as far as I can see). The documentation states "Where True, yield x, otherwise yield y.". If you start having to string muliple statements together then it gets unwieldy, and ideally needs to be broken out into a function.

As for speed, if your case is a simple if-else, then you will see a significant speedup over something like np.vectorize. i.e. it is using vectorization properly like a typical numpy native method.

The fact you have to consider limitations like this is another reason Juila is just easier to use. You are less likely to "get the rules wrong" and end up with a slow function.

Regards, Mike

--

--