[Numpy-discussion] Can I add rows and columns to recarray?

Skipper Seabold jsseabold@gmail....
Sun Dec 5 22:23:51 CST 2010


On Sun, Dec 5, 2010 at 10:56 PM, Wai Yip Tung <tungwaiyip@yahoo.com> wrote:
> I'm fairly new to numpy and I'm trying to figure out the right way to do
> things. Continuing on my question about using recarray as a relation. I
> have a recarray like this
>
>
> In [339]: arr = np.array([
>    .....:     (1, 2.2, 0.0),
>    .....:     (3, 4.5, 0.0)
>    .....:     ],
>    .....:     dtype=[
>    .....:         ('unit',int),
>    .....:         ('price',float),
>    .....:         ('amount',float),
>    .....:     ]
>    .....: )
>
> In [340]: data = arr.view(recarray)
>
>
> One of the most common thing I want to do is to append rows to data.  I
> think concatenate() might be the method. But I get a problem:
>
>
> In [342]: np.concatenate((data0,[1,9.0,9.0]))
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> c:\Python26\Lib\site-packages\numpy\<ipython console> in <module>()
>
> TypeError: expected a readable buffer object
>
>
>
> The other thing I want to do is to calculate the column value. Right now
> it can do great thing like
>
>
>
> In [343]: data.amount = data.unit * data.price
>
>
>
> But sometimes it may require me to add a new column not already exist,
> e.g.:
>
>
> In [344]: data.discount_price = data.price * 0.9
>
>
> How can I add a new column? I tried column_stack. But it give a similar
> TypeError. I figure I need to first specify the type of the column. But I
> don't know how.
>

Check out numpy.lib.recfunctions

I often have

import numpy.lib.recfunctions as nprf

Skipper


More information about the NumPy-Discussion mailing list