[Numpy-discussion] Indexing empty array with empty boolean array causes "IndexError: invalid index exception"
Robert Kern
robert.kern at gmail.com
Wed Aug 12 12:25:20 EDT 2009
On Mon, Aug 10, 2009 at 14:19, Maria Liukis<liukis at usc.edu> wrote:
> Hello everybody,
> I'm using following versions of Scipy and Numpy packages:
>>>> scipy.__version__
> '0.7.1'
>>>> np.__version__
> '1.3.0'
> My code uses boolean array to filter 2-dimensional array which sometimes
> happens to be an empty array. It seems like I have to take special care when
> dimension I'm filtering is zero, otherwise I'm getting an "IndexError:
> invalid index" exception:
>>>> import numpy as np
>>>> a = np.zeros((2,10))
>>>> a
> array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
> [ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
If that were actually your output from zeros(), that would definitely
be a bug. :-)
>>>> filter_array = np.zeros(2,)
>>>> filter_array
> array([False, False], dtype=bool)
>>>> a[filter_array,:]
> array([], shape=(0, 10), dtype=float64)
>>>>>>>
>
> Now if filtered dimension is zero:
>>>> a = np.ones((0,10))
>>>> a
> array([], shape=(0, 10), dtype=float64)
>>>> filter_array = np.zeros((0,), dtype=bool)
>>>> filter_array
> array([], dtype=bool)
>>>> filter_array.shape
> (0,)
>>>> a.shape
> (0, 10)
>>>> a[filter_array,:]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> IndexError: invalid index
>>>>
> Would somebody know if it's an expected behavior, a package bug or am I
> doing something wrong?
I would call it a bug. It's a corner case that we should probably
handle gracefully rather than raising an exception.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list