Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Why does recarray assignment fail: "cannot call setfield on an object array"? (Trac #2006) #2599

@numpy-gitbot

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/2006 on 2011-12-23 by trac user kevin000, assigned to unknown.

Using a recarray with member assignment fails if any column uses the object data type. I don't understand why this fails, especially since the dictionary style assignment works.

Brief example showing what recarray assignments succeed or fail

import numpy as np

recarray with integer datatypes

dt = np.dtype([('foo', 'i8'), ('bar', 'i8')])
r = np.zeros((1,3), dtype=dt).view(np.recarray)
r['foo'] = np.array([1, 2, 3]) # OK
r.foo = np.array([1, 2, 3]) # OK

recarray with an object datatype

dt = np.dtype([('foo', 'i8'), ('bar', 'O8')])
r = np.zeros((1,3), dtype=dt).view(np.recarray)
r['foo'] = np.array([1, 2, 3]) # OK
r.foo = np.array([1, 2, 3]) # RuntimeError

For the relevant C code search for "cannot call setfield on an object array" in this file:

https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/methods.c

Thanks!

Kevin

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions