[Numpy-discussion] Indexing empty array with empty boolean array causes "IndexError: invalid index exception"

Maria Liukis liukis at usc.edu
Mon Aug 10 15:19:24 EDT 2009


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.]])
 >>> 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?


Thanks in advance,
Masha
--------------------
liukis at usc.edu



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090810/2c7015b7/attachment.html>


More information about the NumPy-Discussion mailing list