如何更改numpy recarray的dtype ?

时间:2022-09-30 21:21:54

There a number of posts that nearly answer this but either I don't understand them or they don't answer the question:

有很多帖子几乎回答了这个问题,但要么我不理解他们,要么他们不回答这个问题:

I have a recarray made using numpy.rec.fromrecords. 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?

我有一个使用numpi .rec.fromrecords制作的recarray。假设我要将某些列转换为浮点数。我该怎么做呢?我应该把它们换成ndarray还是重数组?

2 个解决方案

#1


14  

Here is an example using astype to perform the conversion:

这里有一个使用astype进行转换的例子:

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:

年龄为dtype

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

We can change that to <f4 using astype:

我们可以使用astype将其改为

r = r.astype([('name', '|S30'), ('age', '<f4'), ('weight', '<f4')])
print(r)
# [('Bill', 31.0, 260.0) ('Fred', 15.0, 145.0)]

#2


12  

There are basically two steps. My stumbling block was in finding how to modify an existing dtype. This is how I did it:

基本上有两个步骤。我的绊脚石是如何修改现有的dtype。我就是这样做的:

# change dtype by making a whole new array
dt = data.dtype
dt = dt.descr # this is now a modifiable list, can't modify numpy.dtype
# change the type of the first col:
dt[0] = (dt[0][0], 'float64')
dt = numpy.dtype(dt)
# data = numpy.array(data, dtype=dt) # option 1
data = data.astype(dt)

#1


14  

Here is an example using astype to perform the conversion:

这里有一个使用astype进行转换的例子:

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:

年龄为dtype

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

We can change that to <f4 using astype:

我们可以使用astype将其改为

r = r.astype([('name', '|S30'), ('age', '<f4'), ('weight', '<f4')])
print(r)
# [('Bill', 31.0, 260.0) ('Fred', 15.0, 145.0)]

#2


12  

There are basically two steps. My stumbling block was in finding how to modify an existing dtype. This is how I did it:

基本上有两个步骤。我的绊脚石是如何修改现有的dtype。我就是这样做的:

# change dtype by making a whole new array
dt = data.dtype
dt = dt.descr # this is now a modifiable list, can't modify numpy.dtype
# change the type of the first col:
dt[0] = (dt[0][0], 'float64')
dt = numpy.dtype(dt)
# data = numpy.array(data, dtype=dt) # option 1
data = data.astype(dt)