[Numpy-discussion] adding field to rec array

Barry Wark barrywark@gmail....
Fri Oct 5 13:05:56 CDT 2007


Try

r = r.view(numpy.recarray)

barry

On 10/5/07, John Hunter <jdh2358@gmail.com> wrote:
> On 9/26/07, Robert Kern <robert.kern@gmail.com> wrote:
>
> > Here is the straightforward way:
> >
> > In [15]: import numpy as np
> >
> > In [16]: dt = np.dtype([('foo', int), ('bar', float)])
> >
> > In [17]: r = np.zeros((3,3), dtype=dt)
>
> Here is a (hopefully) simple question.  If I create an array like
> this, how can I efficiently convert it to a record array which lets me
> do r.attr in addition to r['attr'].  I'm pretty addicted to the former
> syntax.
>
> In [114]: dt = np.dtype([('foo', int), ('bar', float)])
>
> In [115]: r = np.zeros((3,3), dtype=dt)
>
> In [116]: r.dtype
> Out[116]: dtype([('foo', '<i4'), ('bar', '<f8')])
>
> In [117]: r['foo']
> Out[117]:
> array([[0, 0, 0],
>        [0, 0, 0],
>        [0, 0, 0]])
>
> In [118]: r.foo
> ------------------------------------------------------------
> Traceback (most recent call last):
>   File "<ipython console>", line 1, in ?
> AttributeError: 'numpy.ndarray' object has no attribute 'foo'
>
>
> Thanks,
> JDH
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the Numpy-discussion mailing list