nums.numpy.where

nums.numpy.where(condition, x=None, y=None)[source]

Return elements chosen from x or y depending on condition.

This docstring was copied from numpy.where.

Some inconsistencies with the NumS version may exist.

Parameters
  • condition (BlockArray, bool) – Where True, yield x, otherwise yield y.

  • x (BlockArray) – Values from which to choose. x, y and condition need to be broadcastable to some shape.

  • y (BlockArray) – Values from which to choose. x, y and condition need to be broadcastable to some shape.

Returns

out – An array with elements from x where condition is True, and elements from y elsewhere.

Return type

BlockArray

Notes

If all the arrays are 1-D, where is equivalent to:

[xv if c else yv
 for c, xv, yv in zip(condition, x, y)]

Examples

The doctests shown below are copied from NumPy. They won’t show the correct result until you operate get().

>>> a = nps.arange(10)  
>>> a.get()  
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> nps.where(a < 5, a, 10*a).get()  
array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])