AttributeError numpy.ndarray object has no attribute append

You may are facing AttributeError numpy.ndarray object has no attribute ‘append’ error when dealing with NumPy arrays. This error occurs when you are trying to append one more value to the array. In this short article, we will learn how we can solve the AttributeError numpy.ndarray object has no attribute append error and can solve it. Moreover, we will also discuss various reasons for getting these errors and will try to find some alternative methods to append elements in the array. At the end of this tutorial, we will also discuss how to understand errors in Python so that in the future we can easily solve the errors:

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

We get AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ error when we try to append some elements in the NumPy array. The reason for getting this error is that the NumPy array has no method named append as shown below:

# importing the numpy array
import numpy as np

#define NumPy array
x = np.array([1, 4, 4, 6, 7, 12])

# using appending method
x.append(30)
numpy.ndarray-object-has-no-attribute-append-error

As you can see, we get the error because the NumPy array does not have append() method. Now let us try to solve the error using various ways.

Solution-1: Using np.append() method

As we know that NumPy array does not have append() method. In order to solve the issue, we can use np.append() method. This method is used to append elements to the existing array. The method takes two mandatory parameters. The first parameter is the name of the array and the second parameter is used for the value. The simple syntax of the np.append() method is shown below:

# syntax of np.append() method
numpy.append(array, value)

Now let us use an example to see how we can use the np.append() method:

# importing the numpy array
import numpy as np

#define NumPy array
x = np.array([1, 4, 4, 6, 7, 12])

# using appending method
x = np.append(x, 23)

# printing x
print(x)
[ 1  4  4  6  7 12 23]

As you can see, this time we were able to append a new element to the existing array in numpy without getting the ‘numpy.ndarray’ object has no attribute ‘append’ error.

One thing to note is that the np.append() method does not change the original array. It will create a new array with the appended array. That is why if we will print the original array after appending, we will get the same array as shown below:

#define NumPy array
x = np.array([1, 4, 4, 6, 7, 12])

# using appending method
appended = np.append(x, 23)

# printing x
print(x)
print(appended)
[ 1  4  4  6  7 12]
[ 1  4  4  6  7 12 23]

As you can see, the original array is not modified.

Solution-2: Using concatenate method

Another method that we can use instead of np.append() is using np.concatenate method. This method is mostly used to append one NumPy array to another array. The following the simple syntax of numpy.concatenate method:

# concatenate method in numpy
np.concatenate(arr1, arr2)

Now we will take an example and see how the concatenate() method works. We will create two different arrays and then will use the np.concatenate method to append the two arrays together.

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

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

# printing the array
print(c)
[ 1  4  4  6  7 12 25 26 26 29]

As you can see, we get a new array that contains the elements from both arrays without getting AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ error.

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

So far we discussed why we are getting AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ error and how we can solve it. Now, let us understand what kind of information we can get from errors in Python and how we can interpret them.

In Python, the errors usually have two main parts. The first part of the error gives information about the category of the error which in our case is AttributeError which means we are trying to apply an attribute to the function that is not allowed.

The second part of the error gives more specific information about the error and helps us to find the function which is causing this error. For example in our case, it clearly says ‘numpy.ndarray’ object has no attribute ‘append’ which means we are trying to apply the append() method on ndarray which is not allowed.

What is an AttributeError in Python?

An attribute error is a type of exception in Python that is raised when we use an incorrect attributes with some functions. The simplest example can be, we know that a list can be appended by new values using the append() method. However, if we will use the append() method on some integer values, we will get an attribute error because we are referencing an attribute that is not allowed in Python. See the example below:

# variable defining
var = 29

# using append method
var.append(30)

It will raise an Attribute Error because the integer does not have an append() attribute.

What is numpy.ndarray()?

NumPy ndarray (n-dimensional array) is a multidimensional array that is used to store and manipulate arrays. The ndarray, allows to perform various operations on arrays using ndarray method.

Let us create a simple array using the NumPy module.

# Creating 1 dimensional array
a = np.array([1, 2, 3])
print(a)
print('\n')

#  creating 2-dimensional array
b = np.array([[1, 2], [3, 4], [5, 6]])
print(b)

# creating 3-dimensional array
c = np.array([[[1, 2, 3], [1, 2, 3]], [[2, 3, 5], [6, 7, 8]]])
print(c)
[1 2 3]


[[1 2]
 [3 4]
 [5 6]]
[[[1 2 3]
  [1 2 3]]

 [[2 3 5]
  [6 7 8]]]

As you can see, we have created various types of NumPy arrays.

Summary

In this article, we learned how we can solve AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ error using different methods. We learned that are getting this error because the NumPy object does not have append() method. Instead, we can use the np.append() method and concatenate method to append elements to the NumPy array. Moreover, we also discussed how we can interpret errors in Python taking example of AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ so that in the future we can easily solve such issues.

Related Issues

Leave a Comment

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

Scroll to Top