Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
lebigot opened this Issue · 1 comment

2 participants

@lebigot

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
Owner

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

@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
d253433
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
8dfbe5a
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
c510365
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
0a9c08d
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
853dfc9
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
d05a504
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
f5c6ae1
@ahaldane ahaldane referenced this issue from a commit in ahaldane/numpy
@ahaldane ahaldane 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.
3c1a13d
@charris charris closed this in #5636
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.