[Numpy-discussion] Inconsistency with __index__() for rank-1 arrays?

Travis Oliphant oliphant at enthought.com
Fri Oct 29 00:34:52 EDT 2010


The __index__ method returns an integer from an array.  

The current behavior follows the idea of "return an integer if there is 1-element in the array"

Your suggestion is to only return an integer if it is a rank-0 array, otherwise raise an error.      

This could potentially be changed in NumPy 2.0.    I'm +0 on the suggestion.  

-Travis



On Oct 27, 2010, at 9:34 AM, Francesc Alted wrote:

> Hi,
> 
> I find this a bit misleading:
> 
>>>> a = np.arange(10)
> 
>>>> a[np.array(0)]
> 0
> 
>>>> a[np.array([0])]
> array([0])
> 
>>>> a[[0]]
> array([0])
> 
> But, for regular python lists we have:
> 
>>>> l = a.tolist()
> 
>>>> l[np.array(0)]
> 0
> 
>>>> l[np.array([0])]
> 0
> 
> i.e. indexing with a rank-0 array and a rank-1 array with one single 
> element return the same result, which I find inconsistent with the 
> expected behaviour for this case, i.e.:
> 
>>>> l[[0]]
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call 
> last)
> 
> /tmp/tables-2.2/<ipython console> in <module>()
> 
> TypeError: list indices must be integers, not list
> 
> The ultimate reason for this behaviour is this:
> 
>>>> np.array(0).__index__()
> 0
> 
>>>> np.array([0]).__index__()
> 0
> 
> But I wonder why NumPy needs the latter behaviour, instead of the more 
> logical:
> 
>>>> np.array([0]).__index__()
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call 
> last)
> 
> /tmp/tables-2.2/<ipython console> in <module>()
> 
> TypeError: only rank-0 integer arrays can be converted to an index
> 
> This inconsistency has indeed introduced a bug in my application and for 
> solving this I'd need something like:
> 
> """
> def is_idx(index):
>    """Check if an object can work as an index or not."""
> 
>    if hasattr(index, "__index__"):  # Only works on Python 2.5 on
>        if (hasattr(index, "shape") and index.shape == (1,)):
>            return False
>        try:                           # (as per PEP 357)
>            idx = index.__index__()
>            return True
>        except TypeError:
>            return False
> 
>    return False
> """
> 
> i.e. for determining if an object can be an index or not, I need to 
> explicitly check for a shape different from (1,), which is unnecessarily 
> complicated.
> 
> So I find the current behaviour prone to introduce errors in apps and 
> I'm wondering why exactly np.array([1]) should work as an index at all.  
> It would not be better if that would raise a ``TypeError``?
> 
> Thanks,
> 
> -- 
> Francesc Alted
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

---
Travis Oliphant
Enthought, Inc.
oliphant at enthought.com
1-512-536-1057
http://www.enthought.com






More information about the NumPy-Discussion mailing list