[Numpy-discussion] Inconsistency with __index__() for rank-1 arrays?
Francesc Alted
faltet@pytables....
Wed Oct 27 08:34:31 CDT 2010
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
More information about the NumPy-Discussion
mailing list