Suppose I have a recarray such as the following:
import numpy as np
# example data from @unutbu's answer
recs = [('Bill', '31', 260.0), ('Fred', 15, '145.0')]
r = np.rec.fromrecords(recs, formats = 'S30,i2,f4', names = 'name, age, weight')
print(r)
# [('Bill', 31, 260.0) ('Fred', 15, 145.0)]
Say I want to convert certain columns to floats. How do I do this? Should I change to an ndarray and them back to a recarray?
Here is an example using
astypeto perform the conversion:The
ageis of dtype<i2:We can change that to
<f4usingastype: