[Numpy-discussion] Matching 0-d arrays and NumPy scalars

Stefan van der Walt stefan@sun.ac...
Thu Feb 21 17:33:15 CST 2008

Hi Travis,

On Wed, Feb 20, 2008 at 10:14:07PM -0600, Travis E. Oliphant wrote:
> In writing some generic code, I've encountered situations where it would 
> reduce code complexity to allow NumPy scalars to be "indexed" in the 
> same number of limited ways, that 0-d arrays support.
> For example, 0-d arrays can be indexed with
>     * Boolean masks

I've tried to use this before, but an IndexError (0-d arrays can't be
indexed) is raised.

>     * Ellipses x[...]  and x[..., newaxis]

This, especially, seems like it could be very useful.

> This is an easy change to implement, and I don't think it would cause 
> any backward compatibility issues.
> Any opinions from the list?

This is maybe a fairly esoteric use case, but one I can imagine coming
across.  I'm in favour of implementing the change.

Could I ask that we also consider implementing len() for 0-d arrays?
numpy.asarray returns those as-is, and I would like to be able to
handle them just as I do any other 1-dimensional array.  I don't know
if a length of 1 would be valid, given a shape of (), but there must
be some consistent way of handling them.


More information about the Numpy-discussion mailing list