I have a numpy array of integers. In my code I need to append some other integers from a list, which works fine and gives me back an array of dtype int64 as expected. But it may happen that the list of integers to append is empty. In that case, numpy returns an array of float64 values. Exemplary code below:
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.])
Is this expected behaviour? If so, what is the rationale behind this? Could this even be a bug?
The documentation for np.append states that the return value is
A copy of
arrwithvaluesappended toaxis.
Since there are no values to append, shouldn't it just return a copy of the array?
(Numpy version: 1.22.4, Python version: 3.8.0)
concatenateis not joining lists. It is joining arrays. If one or more of the arguments is a list, it is first converted into an array.