[Numpy-discussion] boolean indexing of rank-0 arrays and scalars - ticket #721

Anne Archibald peridot.faceted@gmail....
Thu Apr 10 13:26:02 CDT 2008


Hi,

Ticket #721 is titled "0-dimensional boolean arrays should work as
masks for array scalars". It is not quite clear to me what the right
behaviour is.

We are generally trying to make zero-dimensional arrays behave the
same as array scalars, but I'm not sure how that should work in this
case:

In [2]: scalar = np.array([1,2])[0]

In [3]: rank0 = np.array(1)

In [4]: scalar[np.array(False)]
---------------------------------------------------------------------------
<type 'exceptions.IndexError'>            Traceback (most recent call last)

/home/peridot/software/numpy/test/lib/python2.5/site-packages/<ipython
console> in <module>()

<type 'exceptions.IndexError'>: invalid index to scalar variable.

In [5]: rank0[np.array(False)]
Out[5]: array([], dtype=int32)

In [6]: rank0[np.array(False)].shape
Out[6]: (0,)

In [7]: rank0[np.array(True)].shape
Out[7]: ()

Here indexing a zero-dimensional array produces either a
zero-dimensional array if the argument is True or a one-dimensional
array of length zero if the argument is False. This is necessary
because there's not really a way to represent "no value" with a
zero-dimensional array - they always have exactly one value.

Should a boolean index into a scalar always produce an array as a
result? Only if that boolean is False?

I should also point out that we have another difference between array
scalars and zero-dimensional arrays:

In [7]: rank0[np.array(True)].shape
Out[7]: ()

In [8]: rank0[np.array([True,False])[0]].shape
---------------------------------------------------------------------------
<type 'exceptions.IndexError'>            Traceback (most recent call last)

/home/peridot/software/numpy/test/lib/python2.5/site-packages/<ipython
console> in <module>()

<type 'exceptions.IndexError'>: 0-d arrays can't be indexed

Zero-dimensional arrays can be used as indices, but
apparently-equivalent scalars cannot.

I am totally unclear on what the distinction between rank-zero arrays
and scalars should be and what indexing into them should look like. Is
there a document describing this (or are we just throwing an
undocumented mass of confusing corner cases at users)?

Thanks,
Anne


More information about the Numpy-discussion mailing list