user2015487
user2015487

Reputation: 2185

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.array(data)
data_dtype=[('date',int),('type','|S1')]
data_rec=numpy.core.records.array(list(tuple(data_array.transpose())), dtype=data_dtype)
data_rec.date
data_rec.type

#Here, i will just try to make another field called copy_date that is a copy of the date    , just as an example

y=numpy.lib.recfunctions.append_fields(data_rec,'copy_date',data_rec.date,dtypes=data_rec.date.dtype,usemask=False)

Now look at the output

>>> type(y)
<type 'numpy.ndarray'>
>>> y.date
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'date'
>>> y.copy_date
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'copy_date'

y is no longer a type of record like

>>> type(data_rec)
<class 'numpy.core.records.recarray'>

I seem to have lost the record abilities, which is to call the fields by the attribute. How can I correctly add a column to a record and be able to call the fields?

Also, I'd be happy if someone can tell me what the usemask option does in the above code.

Thanks

Upvotes: 3

Views: 3029

Answers (1)

mgilson
mgilson

Reputation: 309929

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

Upvotes: 1

Related Questions