简体   繁体   English

是否再次进行Numpy数组的原位类型转换?

[英]In-place type conversion of a Numpy array again?

I have got an array of floats and I would like to cast it into int . 我有一些floats ,我想将其转换为int

a1 = np.ones(10)
a2 = a1.astype(int, copy=False)
a2.dtype # int
a1.dtype # float

What's going on here? 这里发生了什么? I thought astype(int, copy=False) would cast types in-places. 我认为astype(int, copy=False)astype(int, copy=False)类型。 But it doesn't appear to do so? 但这似乎没有吗?

related question: In-place type conversion of a NumPy array 相关问题: NumPy数组的就地类型转换

From the copy argument documentation : copy参数文档中

If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy. 如果将其设置为false,并且满足dtype,order和subok要求,则返回输入数组,而不是副本。

It can't actually convert the type in place. 它实际上无法将类型转换为原位。 It only works if the input array is already of the dtype you want; 仅当输入数组已经是您想要的dtype时才有效; otherwise, it has to make a copy anyway. 否则,它仍然必须进行复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM