[Numpy-discussion] how to add columns

wheres pythonmonks wherespythonmonks at gmail.com
Wed Jul 28 08:43:25 EDT 2010


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



More information about the NumPy-Discussion mailing list