[Numpy-discussion] [SciPy-user] Regarding what "where" returns

Travis Oliphant oliphant at ee.byu.edu
Wed Apr 12 15:16:01 CDT 2006

Perry Greenfield wrote:

>We've noticed that in numpy that the where() function behaves  
>differently than for numarray. In numarray, where() (when used with a  
>mask or condition array only) always returns a tuple of index arrays,  
>even for the 1D case whereas numpy returns an index array for the 1D  
>case and a tuple for higher dimension cases. While the tuple is a  
>annoyance for users when they want to manipulate the 1D case, the  
>benefit is that one always knows that where is returning a tuple, and  
>thus can write code accordingly. The  problem with the current numpy  
>behavior is that it requires special case testing to see which kind  
>return one has before manipulating if you aren't certain of what the  
>dimensionality of the argument is going to be.
I think this is reasonable.  I don't think much thought went in to the 
current behavior as it simply defaults to the behavior of the nonzero 
method (where just defaults to nonzero in the circumstances you are 
describing).    The nonzero method has it's behavior because of the 
nonzero function in Numeric (which only worked with 1-d and returned an 
array not a tuple).

Ideally, I think we should fix the nonzero method and where to have the 
same behavior (both return tuples --- that's actually what the docstring 
of nonzero says right now).   The nonzero function can be special-cased 
to index the tuple for backward compatibility.


More information about the Numpy-discussion mailing list