[Numpy-discussion] how to add columns

wheres pythonmonks wherespythonmonks@gmail....
Wed Jul 28 07:43:25 CDT 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

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?)



More information about the NumPy-Discussion mailing list