转换numpy数组中特定列的dtype

noo*_*per 4 python arrays types numpy type-conversion

我想更改 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') 
Run Code Online (Sandbox Code Playgroud)

事件通过我更改了列的 dtype,但为什么我想替换原来的列,它仍然是u32

cs9*_*s95 6

如果你对命名列没问题,你可以定义一个 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')
Run Code Online (Sandbox Code Playgroud)