Categories
numpy pandas python

How to change the dtype of certain columns of a numpy recarray?

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?