Change dtype of recarray column for object type

mike picture mike · May 17, 2013 · Viewed 7.1k times · Source

I have a csv file where two columns (v3 and v7) are blank for all observations:

v1,v2,v3,v4,v5,v6,v7
GNB,1980,,20,-1.168689,0.4619077,
GNB,1981,20,-1.185176,0.4619077,

I am reading this into python (epd-7.0-2) using the csv2rec function:

from pylab import rec2csv, csv2rec
all_data = csv2rec(infile)

When I try to augment values in the third or 7th row, I receive an error (doesn't happen for other columns):

all_data = csv2rec(infile)
all_data.v3 = 'test'

RuntimeError: cannot call setfield on an object array

So I tried changing the type, only to run into other errors:

all_data.v3.dtype = '|S30'
TypeError: Cannot change data-type for object array.

An additional problem is that I am unable to change other columns in the array as well:

all_data.v1 = 'test'
RuntimeError: cannot call setfield on an object array

Thoughts? Thanks,

Answer

root picture root · May 17, 2013

Use the .astype method:

all_data.astype(dtype=[('v1', 'S3'), ('v2', '<i4'), ('v3', '|S30'), ('v4', '<f8'), ('v5', '<f8'), ('v6', '<f8'), ('v7', 'O')])
#rec.array([('GNB', 1980, '-1', 20.0, -1.168689, 0.4619077, None),
#           ('GNB', 1981, '20', -1.185176, 0.4619077, nan, None)], 
#             dtype=[('v1', 'S3'), ('v2', '<i4'), ('v3', 'S30'), ('v4', '<f8'), ('v5', '<f8'), ('v6', '<f8'), ('v7', 'O')])