Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 800
mathtick Avatar asked Mar 30 '12 19:03

mathtick


People also ask

How do you change the datatype of a column in NumPy?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.

How do I specify a NumPy datatype?

The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

How do I select a specific column in a NumPy array?

Use NumPy array indexing to extract specific columsUse the syntax array[:, [i, j]] to extract the i and j indexed columns from array . Like lists, NumPy arrays use zero-based indexes. Use array[:, i:j+1] to extract the i through j indexed columns from array .

Can NumPy array hold different data types?

Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.


1 Answers

Here is an example using astype to perform the conversion:

import numpy as np
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)]

The age is of dtype <i2:

print(r.dtype)
# [('name', '|S30'), ('age', '<i2'), ('weight', '<f4')]

We can change that to <f4 using astype:

r = r.astype([('name', '|S30'), ('age', '<f4'), ('weight', '<f4')])
print(r)
# [('Bill', 31.0, 260.0) ('Fred', 15.0, 145.0)]
like image 93
unutbu Avatar answered Sep 21 '22 20:09

unutbu