![](https://secure.gravatar.com/avatar/4f71fba1231b38e31020ed481bebccf9.jpg?s=120&d=mm&r=g)
I have a record array r and I want to add a new field to it. I have been looking at setfield but I am not sure how to use it for this purpose. Eg # r is some npy record array N = len(r) x = npy.zeros(N) # add array of floats x to r with dtype name 'jdh' and type '<f8' Any suggestions? JDH
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
John Hunter wrote:
Here is the straightforward way: In [15]: import numpy as np In [16]: dt = np.dtype([('foo', int), ('bar', float)]) In [17]: r = np.zeros((3,3), dtype=dt) In [18]: r Out[18]: array([[(0, 0.0), (0, 0.0), (0, 0.0)], [(0, 0.0), (0, 0.0), (0, 0.0)], [(0, 0.0), (0, 0.0), (0, 0.0)]], dtype=[('foo', '<i4'), ('bar', '<f8')]) 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 ....: In [29]: append_field(r, 'jdh', ones((3,3))) Out[29]: array([[(0, 0.0, 1.0), (0, 0.0, 1.0), (0, 0.0, 1.0)], [(0, 0.0, 1.0), (0, 0.0, 1.0), (0, 0.0, 1.0)], [(0, 0.0, 1.0), (0, 0.0, 1.0), (0, 0.0, 1.0)]], dtype=[('foo', '<i4'), ('bar', '<f8'), ('jdh', '<f8')]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/4f71fba1231b38e31020ed481bebccf9.jpg?s=120&d=mm&r=g)
On 9/26/07, Robert Kern <robert.kern@gmail.com> wrote:
Here is a (hopefully) simple question. If I create an array like this, how can I efficiently convert it to a record array which lets me do r.attr in addition to r['attr']. I'm pretty addicted to the former syntax. In [114]: dt = np.dtype([('foo', int), ('bar', float)]) In [115]: r = np.zeros((3,3), dtype=dt) In [116]: r.dtype Out[116]: dtype([('foo', '<i4'), ('bar', '<f8')]) In [117]: r['foo'] Out[117]: array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) In [118]: r.foo ------------------------------------------------------------ Traceback (most recent call last): File "<ipython console>", line 1, in ? AttributeError: 'numpy.ndarray' object has no attribute 'foo' Thanks, JDH
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
John Hunter wrote:
In [32]: r2 = r.view(np.recarray) In [33]: r2.foo Out[33]: array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
John Hunter wrote:
Here is the straightforward way: In [15]: import numpy as np In [16]: dt = np.dtype([('foo', int), ('bar', float)]) In [17]: r = np.zeros((3,3), dtype=dt) In [18]: r Out[18]: array([[(0, 0.0), (0, 0.0), (0, 0.0)], [(0, 0.0), (0, 0.0), (0, 0.0)], [(0, 0.0), (0, 0.0), (0, 0.0)]], dtype=[('foo', '<i4'), ('bar', '<f8')]) 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 ....: In [29]: append_field(r, 'jdh', ones((3,3))) Out[29]: array([[(0, 0.0, 1.0), (0, 0.0, 1.0), (0, 0.0, 1.0)], [(0, 0.0, 1.0), (0, 0.0, 1.0), (0, 0.0, 1.0)], [(0, 0.0, 1.0), (0, 0.0, 1.0), (0, 0.0, 1.0)]], dtype=[('foo', '<i4'), ('bar', '<f8'), ('jdh', '<f8')]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/4f71fba1231b38e31020ed481bebccf9.jpg?s=120&d=mm&r=g)
On 9/26/07, Robert Kern <robert.kern@gmail.com> wrote:
Here is a (hopefully) simple question. If I create an array like this, how can I efficiently convert it to a record array which lets me do r.attr in addition to r['attr']. I'm pretty addicted to the former syntax. In [114]: dt = np.dtype([('foo', int), ('bar', float)]) In [115]: r = np.zeros((3,3), dtype=dt) In [116]: r.dtype Out[116]: dtype([('foo', '<i4'), ('bar', '<f8')]) In [117]: r['foo'] Out[117]: array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) In [118]: r.foo ------------------------------------------------------------ Traceback (most recent call last): File "<ipython console>", line 1, in ? AttributeError: 'numpy.ndarray' object has no attribute 'foo' Thanks, JDH
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
John Hunter wrote:
In [32]: r2 = r.view(np.recarray) In [33]: r2.foo Out[33]: array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (3)
-
Barry Wark
-
John Hunter
-
Robert Kern