[Numpy-discussion] Indexing empty array with empty boolean array causes "IndexError: invalid index exception"
Maria Liukis
liukis at usc.edu
Mon Aug 17 23:13:43 EDT 2009
On Aug 12, 2009, at 9:25 AM, Robert Kern wrote:
> 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. :-)
Sorry, I realized my copy-and-paste mistake a minute after I posted
the message. Obviously, it was too late :)
>
>>>>> 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.
Thanks, Robert!
>
> --
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list