[Numpy-discussion] Inconsistent/unexpected indexing semantics

Lluís Vilanova vilanova at ac.upc.edu
Mon Nov 30 12:42:06 EST 2015


Hi,

TL;DR: There's a pending pull request deprecating some behaviour I find
       unexpected. Does anyone object?

Some time ago I noticed that numpy yields unexpected results in some very
specific cases. An array can be used to index multiple elements of a single
dimension:

    >>> a = np.arange(8).reshape((2,2,2))
    >>> a[ np.array([[0], [0]]) ]
    array([[[[0, 1],
             [2, 3]]],
           [[[0, 1],
             [2, 3]]]])

Nonetheless, if a list is used instead, it is (unexpectedly) transformed into a
tuple, resulting in indexing across multiple dimensions:

    >>> a[ [[0], [0]] ]
    array([[0, 1]])

I.e., it is interpeted as:

    >>> a[ [0], [0] ]
    array([[0, 1]])

Or what is the same:

    >>> a[( [0], [0] )]
    array([[0, 1]])


I've been informed that there's a pending pull request that deprecates this
behaviour [1], which could in the future be reverted to what is expected (at
least what I expect) from the documents (except for an obscure note in [2]).

The discussion leading to this mail can be found here [3].

[1] https://github.com/numpy/numpy/pull/4434
[2] http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing
[3] https://github.com/numpy/numpy/issues/6564


Thanks,
  Lluis

-- 
"And it's much the same thing with knowledge, for whenever you learn
something new, the whole world becomes that much richer."
-- The Princess of Pure Reason, as told by Norton Juster in The Phantom
Tollbooth



More information about the NumPy-Discussion mailing list