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

Wai Yip Tung tungwaiyip@yahoo....
Mon Dec 6 15:00:29 CST 2010


Thank you for the quick response and Christopher's explanation on the  
design background.

All my tables fit in-memory. I want to explore the data interactively and  
relational database is does not provide me a lot of value.

I was rolling my own library before I come to numpy. Then I find numpy's  
universal function awesome and really fit what I want to do. Now I just  
need to find out what to add row which is easy in Python. It is OK if it  
rebuild an array when I add a column, which should happen infrequently.  
But if adding row build a new array, this will lead to O(n^2) complexity.  
In anycase, I will explore the recfunctions.

Thank you

Wai Yip


> 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