Skip to content

Inconsistent advanced indexing behavior #4940

Closed

Description

@pbazant

The following comments are partially based on impressions triggered by the following part of the NumPy documentation:

For the discussion below, when the selection object is not a tuple, it will be referred to as if it had been promoted to a 1-tuple, which will be called the selection tuple.

To avoid confusion, by wrap into a tuple I mean a->(a,), whereas by converting to a tuple, I mean a->tuple(a).

Let's start with some array:

In [176]: a=np.array([10,11,12])

If an indexing object is not a tuple, it is wrapped in a tuple:

In [178]: a[0]
Out[178]: 10

If the indexing object is a list, we have advanced indexing:

In [179]: a[[0]]
Out[179]: array([10])

Providing the trivial tuple wrap ourselves, we get the same result:

In [180]: a[[0],]
Out[180]: array([10])

If the indexing object has several levels of nesting, so does the result:

In [181]: a[[[0]],]
Out[181]: array([[10]])

Relying on the automatic wrapping into a tuple, we might want to remove the explicit tuple construction:

In [182]: a[[[0]]]
Out[182]: array([10])

This reveals the inconsistency -- the result has changed as if the outer list were actually reinterpreted as a tuple. This theory is consistent with the following behavior:

In [185]: a[[[0,0],[0,0]]]
IndexError: unsupported iterator index

Explicitly wrapping into a tuple makes it work:

In [186]: a[[[0,0],[0,0]],]
Out[186]: 
array([[10, 10],
       [10, 10]])

Alternatively, explicitly converting to an array also works:

In [187]: a[np.array([[0,0],[0,0]])]
Out[187]: 
array([[10, 10],
       [10, 10]])

It would be logical to convert the object to a tuple only if the result obtained by wrapping the object was not suitable for indexing. Even better would be to actually not backtrack too much while trying to make sense of the indexing object, as it makes the behavior really hard to understand.
The following behavior seems to be the instance of the same problem:

In [177]: b=np.array([[1,2],[3,4]])

In [191]: b[[0,0]] # wraps into a tuple and pads as to get ([0,0],:)
Out[191]: 
array([[1, 2],
       [1, 2]])

In [193]: b[[[0,1],[0,1]]]# does not wrap into a tuple and instead converts to a tuple
Out[193]: array([1, 4])

In [194]: b[[[0,1],[0,1]],] # wrapping it ourselves solves the problem
Out[194]: 
array([[[1, 2],
        [3, 4]],

       [[1, 2],
        [3, 4]]])

In my opinion either the order in which various interpretations are tried should be strict or, even better (but problematic due to backwards compatibility) raise an error if there is "too much" ambiguity.

Activity

  1. seberg commented on Aug 1, 2014

    @seberg
    Member

    Yes, I agree, please avoid lists ;). See gh-4434... Should really figure out a way to fix that one thing. Won't be in 1.9., but for 1.10 we may be deprecating it.
    Basically these kind of things often go back as far as numeric and were always kept for backward compatibility.

  2. pbazant commented on Aug 1, 2014

    @pbazant
    Author

    The behavior seems to be in direct conflict with the afforementioned sentence from the documentation. Moreover, it is actually not the case that lists as index objects are simply converted to tuples. The example in gh-4434

    All it does changes is to start issuing a DeprecationWarning for a[[2, 1]], which right now acts like a[(2, 1)] instead of a[np.array([2, 1])]. We want it to eventually act like np.array([2, 1])

    does not correspond to what happens on my machine, as illustrated by my former example. To trigger the problem, I had to use a nested list, so the issue seems to be more subtle.

    I like the behavior implied by the documentation -- the first step would always wrap any non-tuple object in a tuple. Do we agree on the fact that lists are no problem conceptually, but using them as the indexing object is to be avoided for some time, as the behavior will be undergoing changes in the future? If so, I suppose it is not a problem to use lists inside a tuple, so just wrapping all "top level" lists in a tuple (essentially just adding a trailing comma) should behave the same now and in the future?

  3. seberg commented on Aug 2, 2014

    @seberg
    Member

    What that is saying is exactly that any non-tuple would be "wrapped" in the future.

  4. seberg commented on Aug 4, 2014

    @seberg
    Member

    So yeah, I miswrote it in that sentence :). By the way, the actual rule is "a sequence is interpreted like a tuple (converted) if it contains at least one slice, Ellipsis, None or another sequence (and is not an array itself)". And yes, the future goal would be to always "wrap" everything so that arr[indx] is equivalent to arr[indx,] unless type(indx) is tuple

  5. pbazant commented on Aug 4, 2014

    @pbazant
    Author

    Everything is clear now. Thanks. I will adapt my code to make it future proof.

  6. seberg commented on Jan 3, 2019

    @seberg
    Member

    Closing this, since master has a FutureWarning in place here, and we will change it eventually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions