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

Maria Liukis liukis@usc....
Mon Aug 10 14:23:41 CDT 2009


A correction for the typo below.

Thanks,
Masha
--------------------
liukis@usc.edu



On Aug 10, 2009, at 12:19 PM, Maria Liukis 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))

Sorry, copied wrong line for an example which creates an array:
 >>> a  = np.ones((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@usc.edu
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090810/b4d324a7/attachment.html 


More information about the NumPy-Discussion mailing list