On Wed, Jul 28, 2010 at 7:43 AM, wheres pythonmonks <wherespythonmonks@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