Skip to content
Advertisement

Appending an empty list to a numpy array changes its dtype

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 arr with values appended to axis.

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)

Advertisement

Answer

The default dtype for a numpy array constructed from an empty list is float64:

>>> np.array([])
array([], dtype=float64)

A float64 dtype will “win” over the int64 dtype and promote everything to float64, because converting the other way around would cause loss of precision (i.e. truncate any float64). Of course this is an extreme case because there is no value to truncate in an empty array, but the check is only done on the dtype. More info on this is in the doc for numpy.result_type().

In fact:

>>> a = np.array(dtype='int64')
>>> a
array([], dtype=int64)

>>> b = np.array([])
>>> b
array([], dtype=float64)

>>> np.result_type(a, b)
dtype('float64')

The np.promote_types() function is used to determine the type to promote to:

>>> np.promote_types('int64', 'float64')
dtype('float64')

See also: How the dtype of numpy array is calculated internally?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement