-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Description
Usually one can select a subset of fields from a structured array using rarray[list_of_fields]
. If the structured array contains object
s (dtype O
) -- even if an object field isn't being selected -- an error is thrown (in numpy 1.7.1).
>>> a = np.array([], dtype=[('a', 'f'), ('b', 'f'), ('c', 'O')])
>>> a[['a', 'b']] # extract two float columns
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../python2.7/site-packages/numpy/core/_internal.py", line 296, in _index_fields
view = ary.view(dtype=view_dtype)
TypeError: Cannot change data-type for object array.
This field-slice process functions by creating a view with a modified dtype; for this example, the new dtype is: {'names': ['a', 'b'], 'formats': [dtype('float32'), dtype('float32')], 'offsets': [0, 4], 'itemsize': 74}
.
It seems the checks in array_descr_set
are too conservative, discarding alternative views of any array including objects. While it is understandable that this check ensures against arbitrary modification of memory, it makes structured arrays containing objects very unwieldy.
Surely this validation can be made more precise, or else disabled for particular safe operations.
Related to TRAC#674 (closed) and #2346.