Skip to content

Wrong axis order when indexing by (int, slice, list) #6370

Closed

Description

@royf

Advanced indexing sometimes gives the result transposed w.r.t. the expected result.
This occurs when indexing by (int, slice, list), but not (int, slice, slice) or (slice, int, list).

Example:

>>> np.version.full_version
'1.9.3'
>>> a = np.arange(24).reshape((4, 3, 2))

Incorrect:

>>> a[1, :, [0, 1]].shape
(2, 3)

Correct:

>>> a[1, :, :].shape
(3, 2)
>>> a[:, 1, :].shape
(4, 2)
>>> a[:, 1, [0, 1]].shape
(4, 2)

The incorrect result is the transpose of the correct one:

>>> (a[1, :, [0, 1]] == a[1, :, :].T).all()
True

Activity

  1. jaimefrio commented on Sep 26, 2015

    @jaimefrio
    Member

    This is actually the documented behavior: the first and last index are considered to be fancy indices, and when more than one non-contiguous axis is fancy-indexed, the result of indexing those axes is grouped before any of the non-fancy indexed axes.

    It's confusing, and there is an ongoing discussion to provide a more intuitive indexing, see #6075 and #6256, but it is not a bug, but a feature!

    I'm going to close this, but feel free to continue the discussion if you need clarification.

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