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

Francesc Altet faltet@carabos....
Fri Feb 22 06:04:25 CST 2008

A Friday 22 February 2008, Stefan van der Walt escrigué:
> 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.

Yes, that's true, and what's more, you can't pass a slice to a 0-d 
array, which is certainly problematic.  I think this should be fixed.

> >     * Ellipses x[...]  and x[..., newaxis]
> This, especially, seems like it could be very useful.

Well, if you want to create a x[..., newaxis], you can always use 
array([x]), which also works with scalars (and python scalars too), 
although the later does create a copy :-/

> 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.

If 0-d arrays are going to be indexable, then +1 for len(0-d) returning 


>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data

More information about the Numpy-discussion mailing list