Skip to content

Record access on non-existing fields: no error issued #4806

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

Closed
lebigot opened this issue Jun 13, 2014 · 1 comment · Fixed by #5636
Closed

Record access on non-existing fields: no error issued #4806

lebigot opened this issue Jun 13, 2014 · 1 comment · Fixed by #5636

Comments

@lebigot
Copy link
Contributor

lebigot commented Jun 13, 2014

When accessing multiple records with arr[["field0", "field1",…]], using a non-existing field is silent:

>>> arr = numpy.array([(1, 2, 10), (3, 4, 10)], dtype=[('x', int), ('y', int), ("z", int)])
>>> arr[["lkjlkj"]]
array([(), ()], 
      dtype='{'names':[], 'formats':[], 'offsets':[], 'itemsize':24}')

This is inconsistent with accessing a single field:

>>> arr["lkjlkj"]
Traceback (most recent call last):
  File "<ipython-input-7-7a431ec24c6c>", line 1, in <module>
    arr["lkjlkj"]
ValueError: field named lkjlkj not found

Furthermore, it creates error messages that can be harder to make sense of, like in:

for (x, y_value) in arr[["x", "y_valuf"]]:  # Note the typo in "y_valuf"
    …

(ValueError: need more than 1 value to unpack).

I can't find any documentation on the current behavior either (it's not in http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#record-access).

I would suggest that non-existing fields raise an exception, for consistency/least-surprise and practicality reasons.

@seberg
Copy link
Member

seberg commented Jun 13, 2014

Sounds reasonable. The code is in function _index_fields in numpy/core/_internal.py

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 17, 2015
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes numpy#4806,
and also removes a potential segfaults, eg if the array is indexed using
an sequence-like object that raises an exception in getitem.

Also fixes numpy#5631, related to creation of structured dtypes
with no fields (an unusual and probably useless edge case).

Also moves all imports in _internal.py to the top.

Fixes numpy#4806.
Fixes numpy#5631.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants