Skip to content

cannot field-slice structured array (or recarray) containing objects #3286

Closed

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.

Activity

  1. added 10 commits that reference this issue on Jun 4, 2015
    907c541
    b7fece9
    4e3841c
    8171464
    34bdab2
    00a31fe
    432ddcd
    f3903e3
    958bf63
    e2cd6fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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