Skip to content

BUG: Exception text when slicing with an invalid object does not report tuple as an option #26115

Description

@robb-brown

Describe the issue:

When slicing an array with an invalid object, like a list, the exception text is:

"IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices"

which is generated on line 606 of _core/src/multiarray/mapping.c.

This was appropriate when lists of indices were accepted or detected as deprecated, but is misleading now. Would it be better to report something like the following:

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None), tuples of these objects, and integer or boolean arrays are valid indices

Reproduce the code example:

import numpy

myarray = numpy.zeros((5,5))

# generates misleading exception
slice = myarray[[slice(None),slice(None)]]

# correct
slice = myarray[(slice(None),slice(None))]

Error message:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Python and NumPy Versions:

Python version: all

Numpy version: all up to current main branch (commit 20185fd).

Runtime Environment:

No response

Context for the issue:

Seeing the exception, especially debugging legacy code that uses lists of slices, it's easy to waste time checking the individual indices, rather than the acutal indexing object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions