Python’s flexibility is one of its greatest features. It’s flexible enough that we can easily improve some of its most basic components. However, there are instances where this flexibility can make things a little confusing. And that’s the case for instances where we see a ‘numpy.ndarray’ object has no attribute ‘append’ error.
What is this error?
The NumPy library is one of the best examples of how we can expand python’s basic functionality. It improves python’s ability to work with advanced math in some important ways. However, this particular append error shows how expanding a language can sometimes lead to confusing situations in a new array or new dataset.
A python type object or string is a basic blueprint of functionality within the programming language. When you import NumPy module it provides a different way of handling objects and indexing than we find in the standard python implementation. NumPy’s ndarray, an N-dimensional array, is particularly noteworthy for its class attributes, whether it is a 1d array pr 2d array or has strange variables names.
What is causing it?
The main issue is that a NumPy array can be very different from a a normal python list because of the different dtype or ‘list’ object it uses. Python uses an append method when adding items to a standard list or original array. However, that’s not the case for NumPy’s ndarray. An ndarray and a standard 1d array or 2d array have very different instance attributes and class attributes.
Using a NumPy append function on an ndarray is somewhat akin to trying to use a digital keycard on an analog lock that uses standard keys. The functionality of the two locks is similar, but the methods used to interact with them are fundamentally different. Likewise a numpy append works differently than a standard append function because the attribute at the heart of each is fundamentally alien to the other. Trying to use a standard python append function with a NumPy ndarray results in an invalid attribute reference because of that fundamental difference between the two types of array.
How do I fix it?
Thankfully, fixing this no attribute error is fairly simple. NumPy’s ndarray doesn’t have an appended method. However, it does have its own special version of the append function. So instead of using append() you’d want to use numpy.append(). However, numpy.append isn’t a drop in replacement for the standard append method.
There are a few differences in how we use a standard python append method and NumPy’s append function. First and foremost, append only passes one element to the original array. NumPy’s append has a few extra parameters. For example, we might use “numpy.append(origional_list,new_values,axis)”. The original_list variable is the NumPy array we want to modify. The new_values variable consists of what we want to add onto the NumPy array. The axis refers to the axis where the append function is performed. The axis variable is optional. If it isn’t provided then both parameters will be flattened. Finally, after we’ve called numpy.append it will return the new retrieved value.
Fixing the error essentially just requires you to go through your code and update any uses of the append method that work with NumPy ndarrays. It’s not quite a simple matter of changing append to numpy.append, due to the variables being passed differently between the two. So a typical search and replace won’t take care of everything. However, after you’ve changed one of the instance attributes it’s generally a fairly quick and easy progression to fixing it throughout a script.