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

Closed
lebigot opened this Issue Jun 13, 2014 · 1 comment

Comments

Projects
None yet
2 participants
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.

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 Mar 14, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #4806,
and also removes a potential segfaults, eg if the array is indexed using
an sequence-like object that raises an exception in getitem.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 12, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #5631, so array creation does not fail for structured dtypes
with no fields (an unusual and probably useless edge case).

Fixes #4806.
Fixes #5631.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 12, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #5631, so array creation does not fail for structured dtypes
with no fields (an unusual and probably useless edge case).

Fixes #4806.
Fixes #5631.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 12, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #5631, so array creation does not fail for structured dtypes
with no fields (an unusual and probably useless edge case).

Fixes #4806.
Fixes #5631.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 12, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #5631, so array creation does not fail for structured dtypes
with no fields (an unusual and probably useless edge case).

Fixes #4806.
Fixes #5631.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 12, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #5631, so array creation does not fail for structured dtypes
with no fields (an unusual and probably useless edge case).

Fixes #4806.
Fixes #5631.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 17, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #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 #4806.
Fixes #5631.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 17, 2015

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #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 #4806.
Fixes #5631.

@charris charris closed this in #5636 Jun 17, 2015

daniel-perry added a commit to daniel-perry/numpy that referenced this issue Jun 2, 2016

ENH: simplify field indexing of structured arrays
This commit simplifies the code in array_subscript and
array_assign_subscript related to field access. This fixes #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 #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 #4806.
Fixes #5631.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment