Why does a ‘numpy.ndarray’ object have no attribute ‘append’?

Why does a ‘numpy.ndarray’ object have no attribute ‘append’?

A NumPy ndarray (n-dimensional array) is a data structure used for efficient storage and manipulation of arrays in Python. It is designed to handle large datasets and perform mathematical operations efficiently. However, unlike a regular Python list, an ndarray does not have an attribute called ‘append’. This is because ndarrays are optimized for fixed-size arrays, and appending elements to an array would require resizing the entire array, which is a computationally expensive operation. Therefore, the ‘append’ attribute is not included in the design of ndarrays to maintain their high performance and efficiency. Instead, NumPy provides other methods such as ‘concatenate’ or ‘resize’ for adding elements to an array.

Fix: ‘numpy.ndarray’ object has no attribute ‘append’


One error you may encounter when using NumPy is:

AttributeError: 'numpy.ndarray' object has no attribute 'append'

This error occurs when you attempt to append one or more values to the end of a NumPy array by using the append() function in regular Python.

Since NumPy doesn’t have an append attribute, an error is thrown. To fix this, you must use np.append() instead.

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we attempt to append a new value to the end of a NumPy array using the append() function from regular Python:

import numpy as np

#define NumPy array
x = np.array([1, 4, 4, 6, 7, 12, 13, 16, 19, 22, 23])

#attempt to append the value '25' to end of NumPy array
x.append(25)

AttributeError: 'numpy.ndarray' object has no attribute 'append'

We receive an error because NumPy doesn’t have an append attribute.

How to Fix the Error

To fix this error, we simply need to use np.append() instead:

import numpy as np

#define NumPy array
x = np.array([1, 4, 4, 6, 7, 12, 13, 16, 19, 22, 23])

#append the value '25' to end of NumPy array
x = np.append(x, 25)

#view updated array
x

array([ 1,  4,  4,  6,  7, 12, 13, 16, 19, 22, 23, 25])

By using np.append() we were able to successfully append the value ’25’ to the end of the array.

Note that if you’d like to append one NumPy array to the end of another NumPy array, it’s best to use the np.concatenate() function:

import numpy as np

#define two NumPy arrays
a = np.array([1, 4, 4, 6, 7, 12, 13, 16, 19, 22, 23])
b = np.array([25, 26, 26, 29])

#concatenate two arrays together
c = np.concatenate((a, b))

#view resulting array
c

array([ 1,  4,  4,  6,  7, 12, 13, 16, 19, 22, 23, 25, 26, 26, 29])

Refer to the online documentation for an in-depth explanation of both the array and concatenate functions:

The following tutorials explain how to fix other common errors in Python:

Cite this article

stats writer (2024). Why does a ‘numpy.ndarray’ object have no attribute ‘append’?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/why-does-a-numpy-ndarray-object-have-no-attribute-append/

stats writer. "Why does a ‘numpy.ndarray’ object have no attribute ‘append’?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/why-does-a-numpy-ndarray-object-have-no-attribute-append/.

stats writer. "Why does a ‘numpy.ndarray’ object have no attribute ‘append’?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/why-does-a-numpy-ndarray-object-have-no-attribute-append/.

stats writer (2024) 'Why does a ‘numpy.ndarray’ object have no attribute ‘append’?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/why-does-a-numpy-ndarray-object-have-no-attribute-append/.

[1] stats writer, "Why does a ‘numpy.ndarray’ object have no attribute ‘append’?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. Why does a ‘numpy.ndarray’ object have no attribute ‘append’?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top