我有一个整数的numpyarray.在我的代码中,我需要从列表中附加一些其他整数,这样可以很好地工作,并按预期返回一个dtypeint64
array.但可能会出现要附加的整数列表为空的情况.在这种情况下,numpy返回float64
个值的array.示例代码如下:
import numpy as np
a = np.arange(10, dtype='int64')
np.append(a, [10]) # dtype is int64
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
np.append(a, []) # dtype is float64
# array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
这是预期的行为吗?如果是,这背后的理由是什么?这可能是个bug吗?
np.append
的文档说明返回值为
arr
的副本,axis
后附有values
.
既然没有要附加的值,它不应该只返回数组的副本吗?
(NUMPY版本:1.22.4,Python版本:3.8.0)