Skip to content Skip to sidebar Skip to footer

Python: How To Add Column To Record Array In Numpy

I am trying to add a column to a numpy record. This is my code: import numpy import numpy.lib.recfunctions data=[[20140101,'a'],[20140102,'b'],[20140103,'c']] data_array=numpy.arr

Solution 1:

You can pass asrecarray=True to get a recarray back out of numpy.lib.recfunctions.append_fields.

e.g.:

>>> y = numpy.lib.recfunctions.append_fields(data_rec, 'copy_date', data_rec.date, dtypes=data_rec.date.dtype, usemask=False, asrecarray=True)
>>> y.date
array([2, 2, 2])
>>> y
rec.array([(2, 'a', 2), (2, 'b', 2), (2, 'c', 2)], 
      dtype=[('date', '<i8'), ('type', '|S1'), ('copy_date', '<i8')])
>>> y.copy_date
array([2, 2, 2])

Tested on numpy 1.6.1

Post a Comment for "Python: How To Add Column To Record Array In Numpy"