8000 Incorrect Exception when indexing array with field. · Issue #8519 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Incorrect Exception when indexing array with field. #8519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
godaygo opened this issue Jan 22, 2017 · 2 comments
Open

Incorrect Exception when indexing array with field. #8519

godaygo opened this issue Jan 22, 2017 · 2 comments

Comments

@godaygo
Copy link
Contributor
godaygo commented Jan 22, 2017

When record array is indexed with incorrect field name it raises ValueError as expected:

> arr = np.zeros(10, dtype=[('a', 'i8')])
> arr['b']

ValueError: no field of name b

When a simple array is indexed, it raises IndexError as unexpected:

> arr = np.arange(10)
> arr['a']

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

According to this question on SO previously it was ValueError in both cases. Because you can not typecheck, they are both ndarrays, it is odd to have different types of exceptions.

Was this an intended change?

Also according to all this, the message for IndexError is not full, it does not say about fields.

@charris
Copy link
Member
charris commented Jan 22, 2017

It looks like stray voltage from the indexing work. OTOH

In [1]: arange(5)[6]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-ede2619ddcc7> in <module>()
----> 1 arange(5)[6]

IndexError: index 6 is out of bounds for axis 0 with size 5

So one could argue that both errors should be IndexError, but with a better error message. The Python documentation is


exception IndexError

    Raised when a sequence subscript is out of range.
   (Slice indices are silently truncated to fall in the allowed range;
   if an index is not a plain integer, TypeError is raised.)

So strictly speaking, it seems that the first case should be IndexError, taking 'b' to be out of bounds, but the second error message would be more appropriate to a TypeError. This actually sounds like a good topic for discussion if you want to raise it on the mailing list.

@ahaldane
Copy link
Member
ahaldane commented Jan 22, 2017

The IndexError is pretty old behavior that goes back to v.1.8.2 (edit: The change happened between 1.8.2 and 1.9.3), so I don't think this was caused by recent changes. (The SO question is from 2012 which must be even older than 1.8.2?).

I actually didn't realize the IndexError might be unintentional and have often intentionally maintained the IndexError/ValueError discrepancy because I wanted to stay backward compatible.

Just to link up some PRs, here are some PRs where I either changed or considered changing the exception types:

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

No branches or pull requests

4 participants
0