[Numpy-discussion] Rename record array fields (with object arrays)

Travis E. Oliphant oliphant at enthought.com
Thu Feb 28 11:48:41 EST 2008


Sameer DCosta wrote:
> Hi,
>
> I'm having trouble renaming record array fields if they contain object
> arrays in them. I followed the solutions posted by Robert Kern and
> Stefan van der Walt  (Thanks again) but it doesn't look like this
> method works in all cases. For reference:
> http://projects.scipy.org/pipermail/numpy-discussion/2008-February/031509.html
>
> In [1]: from numpy import *
>
> In [2]: olddt = dtype([('foo', '|O4'), ('bar', float)])
>
> In [3]: a = zeros(10, olddt)
>
> In [4]: a
> Out[4]:
> array([(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0),
>        (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
>       dtype=[('foo', '|O4'), ('bar', '<f8')])
>
> In [5]: newdt = dtype([('notfoo', '|O4'), ('notbar', float)])
>
> In [6]: b = a.view(newdt)
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /home/titan/sameer/projects/<ipython console>
>
> TypeError: Cannot change data-type for object array.
>   

This looks like a bug.  We are being a bit over-zealous in protecting 
you from getting access to pointers and in the process making it 
impossible to rename Object fields.  

Perhaps an actual field-renaming API (which would be relatively easy) is 
useful.

-Travis O.





More information about the NumPy-Discussion mailing list