Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
lebigot opened this Issue · 1 comment

2 participants

Eric O. LEBIGOT (EOL) seberg
Eric O. LEBIGOT (EOL)

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

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.