If you attempt to call the index() method on a NumPy array, you will raise the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’. The NumPy array does not have index() as an attribute. The index() method belongs to the list object. To get the index of a value in a NumPy array, you can use the numpy.where() function.
This tutorial will go through how to solve this error with code examples.
Table of contents
AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’
What is an AttributeError?
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘numpy.ndarray’ object has no attribute ‘index'” tells us that the numpy array object we are handling does not have the index attribute.
We will raise the AttributeError if we try to call the index() method on a numpy array. index() is a list method that returns the position of the first occurrence of the specified value. We pass a separating character to the split() method when we call it. Let’s look at an example of calling the index() method on a list of strings:
lst = ["hydrogen", "oxygen", "nitrogen", "xenon"] print(lst.index("oxygen"))
1
Example
Let’s look at an example where we define a numpy array of integers and get the index of the largest value in the array. First, let’s create the NumPy array:
import numpy as np # Create NumPy array arr = np.array([2, 3, 1, 8, 9, 21, 2, 4, 18, 6])
Next, we will get the maximum value in the array using numpy.max():
# Get maximum value of array max_val = np.max(arr) print(max_val)
Then, we will attempt to get the index of the maximum value in the array using the index() method:
# Get index position of maximum value in array print(f'Index of max value is {arr.index(max_val)}')
Let’s run the code to see what happens:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) 1 # Get index position of maximum value in array 2 ----≻ 3 print(f'Index of max value is {arr.index(max_val)}') AttributeError: 'numpy.ndarray' object has no attribute 'index'
The code throws the error because the index() method does not belong to the NumPy array object. The index() method only works on a normal Python list.
Solution: Use the NumPy where() Function
To solve the error, we can use the numpy.where() function. The syntax for using the where() function to get the index position of an element in a NumPy array is
numpy.where(array_name==element_of_array)
The where() method returns the indices of a specified element in a NumPy array. Let’s look at the revised code with the where() method in our example:
# Find index position of minimum value import numpy as np # Create NumPy array arr = np.array([2, 3, 1, 8, 9, 21, 4, 18, 6]) # Get maximum value of array max_val = np.max(arr) # Find index position of maximum value using where() idx = np.where(arr == max_val) print(idx) print(f'Index of max value is {idx[0]}')
The variable idx is a tuple, where the first value is the array with the index positions. We can print the index to the console using an f-string. Let’s run the code to see the result:
(array([5]),) Index of max value is [5]
The maximum value is at the sixth position in the array. Remember that the indexing of an array starts at zero.
A key difference between index() and where() is index() returns the index of the first occurrence of a value, whereas the where() function returns the index of all occurrences of a value. Let’s look at an example where the NumPy array has several occurrences of the maximum value.
# Find index position of minimum value import numpy as np # Create NumPy array arr = np.array([2, 3, 21, 1, 8, 9, 21, 4, 18, 6, 21]) # Get maximum value of array max_val = np.max(arr) # Find indices of maximum value using where() idx = np.where(arr == max_val) print(f'Indices of max value is {idx[0]}')
When we run the code, we will get an array with all index positions where the maximum value of 21 occurs.
Indices of max value is [ 2 6 10]
For further reading on the NumPy where function, go to the article: How-to Guide for Python NumPy Where Function.
Summary
Congratulations on reading to the end of this tutorial! The error AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’ occurs when you try to call the index() method on a numpy array. You can call the index() method on a list. To get the index position of a value in a NumPy array, you can use the where() method. The where() method will return the index positions of all occurrences of the specified value in the array.
For further reading on AttributeErrors with NumPy arrays, go to the articles:
- How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’
- How to Solve Python AttributeError: ‘numpy.ndarray’ has no attribute ‘values’
For further reading on TypeErrors with NumPy, go to the article: How to Solve Python TypeError: ‘numpy.float64’ object cannot be interpreted as an integer
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.