[Numpy-discussion] structured array indexing oddity

Matthew Brett matthew.brett@gmail....
Tue Mar 15 19:24:37 CDT 2011


Hi,

I just wrote a short test for indexing into structured arrays with
strings and found this:

In [4]: a = np.zeros((1,), dtype=[('f1', 'i4')])
In [5]: a['f1']
Out[5]: array([0])
In [6]: a['f2']  # not present -> error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/mb312/<ipython console> in <module>()

ValueError: field named f2 not found.
In [7]: a[0]['f1'] # OK
Out[7]: 0
In [8]: a[0]['f2']
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

/Users/mb312/<ipython console> in <module>()

IndexError: invalid index

It seems odd to raise an 'IndexError' without a message about the
field name in the second case, and a ValueError with a good message in
the first.

Do y'all agree?

Matthew


More information about the NumPy-Discussion mailing list