numpy array concatenation error: 0-d arrays can’t be concatenated

You need to put the arrays you want to concatenate into a sequence (usually a tuple or list) in the argument.

tmp = np.concatenate((allValues, np.array([30], float)))
tmp = np.concatenate([allValues, np.array([30], float)])

Check the documentation for np.concatenate. Note that the first argument is a sequence (e.g. list, tuple) of arrays. It does not take them as separate arguments.

As far as I know, this API is shared by all of numpy’s concatenation functions: concatenate, hstack, vstack, dstack, and column_stack all take a single main argument that should be some sequence of arrays.


The reason you are getting that particular error is that arrays are sequences as well. But this means that concatenate is interpreting allValues as a sequence of arrays to concatenate. However, each element of allValues is a float rather than an array, and is therefore being interpreted as a zero-dimensional array. As the error says, these “arrays” cannot be concatenated.

The second argument is taken as the second (optional) argument of concatenate, which is the axis to concatenate on. This only works because there is a single element in the second argument, which can be cast as an integer and therefore is a valid value. If you had put an array with more elements in the second argument, you would have gotten a different error:

a = np.array([1, 2])
b = np.array([3, 4])
np.concatenate(a, b)

# TypeError: only length-1 arrays can be converted to Python scalars

Leave a Comment