Skip to content

recarray.__getitem__ with field list gives "TypeError: Cannot change data-type for object array." when dtype contains object #3256

@benmoran

Description

@benmoran

This code works for me on numpy 1.6.2 but on 1.7.1 I get an exception, because in the new version numpy.core._internal._index_fields calls ary.view() and this doesn't work on object arrays.

import numpy as np
ra = np.recarray((2,), dtype=[('x', object), ('y', float), ('z', int)]) 
ra[['x','y']]

Example on 1.7.1:

In [13]: ra[['x','y']]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

.../python-271-20110303-2/lib/python2.7/site-packages/numpy/core/records.pyc in __getitem__(self, indx)
    455 
    456     def __getitem__(self, indx):
--> 457         obj = ndarray.__getitem__(self, indx)
    458         if (isinstance(obj, ndarray) and obj.dtype.isbuiltin):
    459             return obj.view(ndarray)

.../lib/python2.7/site-packages/numpy/core/_internal.pyc in _index_fields(ary, fields)
    294 
    295     view_dtype = {'names':names, 'formats':formats, 'offsets':offsets, 'itemsize':dt.itemsize}
--> 296     view = ary.view(dtype=view_dtype)
    297 
    298     # Return a copy for now until behavior is fully deprecated


.../lib/python2.7/site-packages/numpy/core/records.pyc in view(self, dtype, type)
    495             if dtype.fields is None:
    496                 return self.__array__().view(dtype)
--> 497             return ndarray.view(self, dtype)
    498         else:
    499             return ndarray.view(self, dtype, type)

.../lib/python2.7/site-packages/numpy/core/records.pyc in __setattr__(self, attr, val)
    437             if attr not in fielddict:
    438                 exctype, value = sys.exc_info()[:2]
--> 439                 raise exctype, value
    440         else:
    441             fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}

TypeError: Cannot change data-type for object array.

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