[Numpy-discussion] how to add columns

wheres pythonmonks wherespythonmonks at gmail.com
Wed Jul 28 12:51:17 EDT 2010


Thank you -- just what I was looking for.



On Wed, Jul 28, 2010 at 11:39 AM, Benjamin Root <ben.root at ou.edu> wrote:
> On Wed, Jul 28, 2010 at 7:43 AM, wheres pythonmonks
> <wherespythonmonks at gmail.com> wrote:
>>
>> I have a rec array and I want to add an additional column.
>>
>> I've seen at least two solutions to this problem:
>>
>> mlab.rec_append_fields (matplotlib)
>>
>> And append_field from
>>
>> http://mail.scipy.org/pipermail/numpy-discussion/2007-September/029357.html
>>
>> In [19]: def append_field(rec, name, arr, dtype=None):
>>    arr = np.asarray(arr)
>>    if dtype is None:
>>        dtype = arr.dtype
>>    newdtype = np.dtype(rec.dtype.descr + [(name, dtype)])
>>    newrec = np.empty(rec.shape, dtype=newdtype)
>>    for field in rec.dtype.fields:
>>        newrec[field] = rec[field]
>>    newrec[name] = arr
>>    return newrec
>>
>> Is there a best solution?  I don't like the matplotlib solution b/c of
>> the "dll-hell" anti-pattern.  But the pure numpy solution looks like
>> it has too many copies. (or are recarrays pointers-of-pointers as
>> opposed to contiguous memory?)
>>
>> Help!
>>
>> W
>
> W,
>
> You can import numpy.lib.recfunctions module and use the .append_fields()
> function.  I am pretty sure it is still making a copy, but it is a
> numpy-native function rather than rolling your own and/or using mlab.  Note,
> that it will try to turn it into a masked record array by default.  You can
> turn that off by using usemask=False.
>
> I hope that helps,
>
> Ben Root
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>



More information about the NumPy-Discussion mailing list