[Numpy-discussion] Numpy where

Nathaniel Smith njs at pobox.com
Fri Mar 13 15:31:17 EDT 2015

On Thu, Mar 12, 2015 at 5:02 PM, Charles R Harris
<charlesr.harris at gmail.com> wrote:
> Hi All,
> This is apropos gh-5582 dealing with some corner cases of np.where. The
> following are the current behavior
>>>> import numpy
>>>> numpy.where(True)  # case 1
> ... (array([0]),)
>>>> numpy.where(True, None, None)  # case 2
> ... array(None, dtype=object)
>>>> numpy.ma.where(True)  # case 3
> ... (array([0]),)
>>>> numpy.ma.where(True, None, None)  # case 4
> ... (array([0]),)
> The question is, what exactly should be done in these cases? I'd be inclined
> to raise an error for cases 1 and 3.

Actually, I forgot about the annoying thing where np.where is
two-functions-for-the-price-of-one, and np.where(x) is equivalent to
np.nonzero(asarray(x)). That's documented, and is what's happening in
cases 1 and 3.

So I take back my previous email: I'm -1 on specifically making
np.where(scalar) into an error -- we should either consistently stick
with the current nonzero() behavior, or consistently remove it. I'd be
happy with deprecating np.where(x) entirely and eventually making it
an error, since it's just a weird ugly alias for nonzero().

(Case 2 still looks correct to me, and case 4 just looks like an
accidental side-effect of someone being lazy and using None to mean
"not specified".)


Nathaniel J. Smith -- http://vorpus.org

More information about the NumPy-Discussion mailing list