我想更改numpy列数据类型,但是当我替换原始numpy列时,dtype不会成功更改。
import numpy as np
arraylist =[(1526869384.273246, 0, 'a0'),
(1526869385.273246, 1, 'a1'),
(1526869386.273246, 2, 'a2'),
(1526869387.273246, 3, 'a3'),
(1526869388.273246, 4, 'a4'),
(1526869389.273246, 5, 'a5'),
(1526869390.273246, 6, 'a6'),
(1526869391.273246, 7, 'a7'),
(1526869392.273246, 8, 'a8'),
(1526869393.273246, 9, 'a9'),
(1526869384.273246, 0, 'a0'),
(1526869385.273246, 1, 'a1'),
(1526869386.273246, 2, 'a2'),
(1526869387.273246, 3, 'a3'),
(1526869388.273246, 4, 'a4'),
(1526869389.273246, 5, 'a5'),
(1526869390.273246, 6, 'a6'),
(1526869391.273246, 7, 'a7'),
(1526869392.273246, 8, 'a8'),
(1526869393.273246, 9, 'a9')]
array = np.array(arraylist)
array.dtype
dtype('<U32')
array[:,0]=array[:,0].astype("float64")
array[:,0].dtype
>>> dtype('<U32')
事件通过我更改了列的dtype,但为什么我要替换orignal列仍然u32
?
答案 0 :(得分:3)
如果你对命名列没有问题,你可以定义一个dtypes元组,并在创建过程中将它们分配给array
:
dtype = [('A', 'float'), ('B', 'int'), ('C', '<U32')]
array = np.array(arraylist, dtype=dtype)
array['A'].dtype # note, array[: 0] does not work here since these are named columns
dtype('float64')