8000 cannot field-slice structured array (or recarray) containing objects · Issue #3286 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
cannot field-slice structured array (or recarray) containing objects #3286
@jnothman

Description

@jnothman

Usually one can select a subset of fields from a structured array using rarray[list_of_fields]. If the structured array contains objects (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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0