how to add columns

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

Wed, 28 Jul 2010 08:43:25 -0400, wheres pythonmonks wrote: [clip]
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.
You cannot avoid making copies, since adding a new field changes the size of the array item.
(or are recarrays pointers-of-pointers as opposed to contiguous memory?)
No.

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

Thank you -- just what I was looking for.
On Wed, Jul 28, 2010 at 11:39 AM, Benjamin Root ben.root@ou.edu wrote:
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
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Benjamin Root
-
Pauli Virtanen
-
wheres pythonmonks