15

I know there was already a question about this topic (cleanest way to add a field to a structured numpy array), see

Adding a field to a structured numpy array

but I have a question about the answer given there ...

If you're using numpy 1.3, there's also numpy.lib.recfunctions.append_fields()

I still have numpy 1.3, but it doesn't recognise this function, and I also didn't find anything about it in the documentation of numpy. What happened with the function? Is there another function that can do the same?

Community
  • 1
  • 1
joris
  • 133,120
  • 36
  • 247
  • 202

3 Answers3

15

http://projects.scipy.org/numpy/browser/branches/1.3.x/numpy/lib/recfunctions.py?rev=8229

did you import?

from numpy.lib import recfunctions

recfunctions.append_fields(*your_args)  # base, names, data, ...

Seems like everything is working:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
'1.3.0'
>>> from numpy.lib.recfunctions import append_fields
>>> append_fields
<function append_fields at 0x9e3e80c>
>>> 
Community
  • 1
  • 1
dting
  • 38,604
  • 10
  • 95
  • 114
  • indeed, I didn't import... See other answer. – joris Mar 13 '11 at 11:07
  • 1
    I hit this, too. `import numpy` and then using `numpy.lib.recfunctions.append_fields` didn't work. One needs to `import numpy.lib.recfunctions` or something like @DTing recommends. Not sure why recfunctions is not just recognized as a result of `import numpy`... – Roland Jan 29 '14 at 19:46
14

Here is a concrete example how to use append_fields(..) (admittedly based on the other answers here):

import numpy as np
x = np.array(np.arange(0,10), dtype = [('x', float)])
y = np.array(np.arange(10,20), dtype = [('y', float)])

from numpy.lib.recfunctions import append_fields

z = append_fields(x, 'y', y)

where

z.dtype.names

will give

('x', 'y')

note that y in can also be 'plain' ndarray without column names:

y = np.arange(10,20)

or you can rename the column y to something else (even if y is a structured array with column names):

z = append_fields(x, 'p', y)

(tested in numpy 1.6.1)

Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
3

As far as documentation for the recfunctions, here it is: http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html

If you can't figure it out after reading that, then please provide some code samples (for instance, did you import numpy.lib.recfunctions?) and an error message or undesired result, so we can determine how to fix it.

jmlarson
  • 837
  • 8
  • 31
J. Taylor
  • 4,567
  • 3
  • 35
  • 55
  • 1
    Thank you! I thought that it was standard in numpy so just tried to type `np.lib.recfunctions.append_fields()` (after importing numpy as np off course), but didn't import `numpy.lib.recfunctions` seperately. So that's why it didn't know the function. But still, it is not mentioned in the documentation of numpy itself, and that doesn't make it easier to find it. Do you know why it is not included? – joris Mar 13 '11 at 10:59
  • No problem. Glad you got it worked out. I'm not sure why they didn't include this in the documentation (I'm not familiar with their documentation policies though, so you might want to ask one of the project devs if you're really interested, or think it's a documentation bug) – J. Taylor Mar 13 '11 at 11:11