# AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’

The AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’ occurs when you attempt to use the `index()` method on a NumPy array that does not have any index attribute to use.

In this article, we will see what exactly ‘numpy.ndarray’ object has no attribute ‘index’ means and how to resolve this with examples.

## What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?

We get an object has no attribute index error when we try to index the NumPy array with the index function.

Let us take a simple example to reproduce this issue. In our below example, we have declared the NumPy array, and we are trying to get the index of the largest number in an array.

We can find the largest number in the NumPy array using the `max()` function, and to find an index position, we are using the index() method.

When we run the below program, we get AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’.

``````# import numpy library
import numpy as np

# create NumPy array
my_array = np.array([3, 5, 2, 1, 6, 9, 8, 4])

# find the largest number in numpy array
largest_num = np.max(my_array)
print("Largest number in array is", largest_num)

# find the index of largest number
my_array.index(largest_num)
``````

Output

``````Largest number in array is 9
Traceback (most recent call last):
File "C:\Personal\IJS\Code\program.py", line 13, in <module>
my_array.index(largest_num)
AttributeError: 'numpy.ndarray' object has no attribute 'index'``````

## How to fix AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?

We cannot apply an `index()` function to the NumPy array. The index function works only on the normal Python list.

If we want to find the index position of an element in a NumPy array, we can leverage the `where()` function.

Syntax:

`Numpy.where(array_name==element_of_array)`

The `where()` method returns the indexes of a specified element in a NumPy array as shown in below examples.

### Example 1: Get the index position of an element in the NumPy array

``````# import numpy library
import numpy as np

# create NumPy array
my_array = np.array([3, 5, 2, 1, 6, 9, 8, 4])

# find the largest number in numpy array
largest_num = np.max(my_array)
print("Largest number in array is", largest_num)

# print the index of largest number
print(np.where(my_array == largest_num))
``````

Output

``````Largest number in array is 9
(array([5], dtype=int64),)``````

The indexes in the array start from 0, in the above example, the largest element is 9, and it is at the index position 5 in our example.

### Example 2: Get all the index positions of an element in the NumPy array

In this example, the largest element is 9, but it appears more than once in an array. Hence the `where()` function will return all the index positions of the largest element.

In the below example the index positions are 2 and 6 for the largest element 9.

``````# import numpy library
import numpy as np

# create NumPy array
my_array = np.array([3, 5, 9, 2, 1, 6, 9, 8, 4])

# find the largest number in numpy array
largest_num = np.max(my_array)
print("Largest number in array is", largest_num)

# print the indexes of largest number
print(np.where(my_array == largest_num))
``````

Output

``````Largest number in array is 9
(array([2, 6], dtype=int64),)``````

### Example 3: Returns an empty array if the element is not found in the NumPy array

If we pass an element that is not present in an array, the `where()` function will return an empty array. Here the value 10 is not present, and hence it returns an empty array.

``````# import numpy library
import numpy as np

# create NumPy array
my_array = np.array([3, 5, 9, 2, 1, 6, 9, 8, 4])

# print the index of value 10
print(np.where(my_array == 10))
``````

Output

``(array([], dtype=int64),)``

## Conclusion

The index function works only on the normal Python list. If we try to apply the `index()` method on the NumPy array to find the element position, we get AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’ .

We can solve this error by using the `where()` function to get the indexes of the element in a NumPy array.

Get notified of the best deals on our WordPress themes.

## JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Table of Contents Hide JSONDecodeError: Expecting value: line 1 column 1 (char 0)Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)Solution Other Possible SolutionsEnsure HTTP 200 status code and valid…

## Python ValueError: could not convert string to float

Table of Contents Hide ValueError: could not convert string to floatException could not convert string to float Fix ValueError: could not convert string to floatSolution 1: Ensure the string has…

## ImportError: attempted relative import with no known parent package

Table of Contents Hide How does module import work in Python?Absolute vs. Relative importsHow to fix ImportError: attempted relative import with no known parent package?Option 1 – Use absolute importsOption…

## Python FileNotFoundError: [Errno 2] No such file or directory Solution

Table of Contents Hide Python FileNotFoundError: [Errno 2] No such file or directoryExample FileNotFoundErrorMisspelled file nameInvalid file path or directory pathUsing a relative pathSolution to FileNotFoundError: [Errno 2] No such…

## TypeError: list indices must be integers or slices, not tuple

If you are accessing the list elements in Python, you need to access it using its index position. If you specify a tuple or a list as an index, Python…

## List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…