[Numpy-discussion] Inconsistency with __index__() for rank-1 arrays?

Francesc Alted faltet@pytables....
Fri Oct 29 02:54:23 CDT 2010


Hi Travis, thanks for answering,

A Friday 29 October 2010 06:34:52 Travis Oliphant escrigué:
> The __index__ method returns an integer from an array.
> 
> The current behavior follows the idea of "return an integer if there
> is 1-element in the array"
> 
> Your suggestion is to only return an integer if it is a rank-0 array,
> otherwise raise an error.

Yes.  I think this makes a lot of sense because a rank-0 array can be 
seen as a scalar (and hence, and index), but it is difficult to see a 
rank-1 (or, in general, rank-N) array as a scalar (even if only has 1 
single element).  In particular, this would avoid this inconsistency:

>>> a[np.array(1)]
1
>>> a[np.array([1])]
array([1])
>>> a[np.array([[[1]]])]
array([[[1]]])

but:

>>> np.array(1).__index__()
1
>>> np.array([1]).__index__()
1
>>> np.array([[[1]]]).__index__()
1

> This could potentially be changed in NumPy 2.0.    I'm +0 on the
> suggestion.

My vote is +1 for deprecating ``array([scalar])`` as a scalar index for 
NumPy 2.0.

-- 
Francesc Alted


More information about the NumPy-Discussion mailing list