Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ Solution

By | November 20, 2021
Python AttributeError 'numpy.ndarray' object has no attribute 'append' Solution

Numpy is a third-party open-source Python scientific computational library. It is widely used for its array module, and the arrays defined using numpy are very fast and powerful as compared to the Python lists.

Using the numpy array we can define one as well as multi dimensional arrays. But if we try to use the append() method to add new elements to a numpy array object we will encounter the AttributeError: 'numpy.ndarray' object has no attribute 'append' Error.

Vamware

In this Python guide, we will discuss this error in detail and see why this error is raised in a Python program and how to debug it. We will also walk through an example to see how to resolve this error.

Error Problem: AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’

The Python error AttributeError: 'numpy.ndarray' object has no attribute 'append' statement has two parts.

  1. AttributeError (Exception Type)
  2. ‘numpy.ndarray’ object has no attribute ‘append’ (Error Message)

1. AttributeError

AttributeError is one of the Python standard exceptions. It is raised when we try to call a method or property that is not defined for an object.

2. ‘numpy.ndarray’ object has no attribute ‘append’

” 'numpy.ndarray' object has no attribute 'append' ” is the error message. You will only encounter this error when you try to call the append property or append() method on a numpy array object.

This error message is simply telling us that the numpy array does not support any method or property append.

Error Example

import numpy as np

# initialize a numpy array
my_array = np.array([1,2,3,4,5,6,7,8])

# add a new element to the numpy array
my_array.append(9)

print(my_array)

Output

Traceback (most recent call last):
    File "main.py", line 7, in <module>
         my_array.append(9)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

Break the code

In the above example we are getting this error because the numpy arrays do not support append() method. And when we tried to call the append() method on our array object my_array to add the new value 9  we received the error.

Solution

However, numpy arrays do not support append() method, but the numpy module provide a methods append() that can be used to append new elements to the numpy array.

Solve the error using append() method.

We use the numpy append() mehtod to add new elements to a numpy array.

syntax 

array_name = numpy.append(array_name, new_element)

Solution 1 Example

import numpy as np

# initialize a numpy array
my_array = np.array([1,2,3,4,5,6,7,8])

# add a new to the numpy array
my_array = np.append(my_array, 9)
print(my_array)

Output

[1 2 3 4 5 6 7 8 9]

Conclusion

The append() is the numpy’s method, not the numpy array’s. So if we try to perform the append method on a numpy array we will receive the  AttributeError: 'numpy.ndarray' object has no attribute 'append' Error. If you are getting this error in your Python program this means you are trying to call the append() method on your array object. Instead of calling the append on the array object you need to call it on the numpy object and pass the array and value to be appended to the method as the arguments.

If you are still getting this error in your program, you can share your code in the comment section. We will try to help you in debugging.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *