Python

How to Fix numpy.ndarray has no attribute index

In Python, an AttributeError is an exception that occurs when an attribute or its reference fails. You may encounter the AttributeError ‘numpy.ndarray’ object has no attribute index if you use the index() method with a numpy array. In this guide, we will discuss how to reproduce this error and fix it using numpy.where() and argwhere() functions.

Error: ‘numpy.ndarray’ object has no attribute index

In numpy, the index() method doesn’t exist and should not be used. If you specify this method to return the index of any element, it will result in an error. There are other methods available in numpy that will return the index of specific elements at a time. First, we will reproduce this error and then will resolve it.

Create a numpy array named Industry with ten strings and try to return the index of “Government.”

import numpy

# Create numpy array - Industry with 10 strings
Industry = numpy.array(['Agriculture','Banking','Government','Chemical','Electronics',
'Engineering','Entertainment','Environmental','Finance','Manufacturing'])

print(Industry,"\n")

# Try to return the index of Government
Industry.index('Government')

Output

You can see that an AttributeError is encountered.

Solution 1: Using numpy.where()

This function is used to return the indices of the elements in the given array based on specified conditions. In this case, we will use this function and return the index of specific elements by providing some conditions.

Syntax

Let’s examine the syntax of this function. It will return the array of indices (indexing starts from 0).

numpy.where(condition/s)

Example 1

Use the above numpy array and return the index of “Government,” “Engineering,” and “Other” separately using the numpy.where() function.

import numpy

# Create numpy array - Industry with 10 strings

Industry = numpy.array(['Agriculture','Banking','Government','Chemical','Electronics',
'Engineering','Entertainment','Environmental','Finance','Manufacturing'])

print(Industry,"\n")

# Return the index of Government

print(numpy.where(Industry == 'Government'))

# Return the index of Engineering

print(numpy.where(Industry == 'Engineering'))

# Return the index of Other

print(numpy.where(Industry == 'Other'))

Output

  1. Index of ‘Government’ is 2
  2. Index of ‘Engineering’ is 5 and
  3. Index of ‘Other’ is empty since it doesn’t exist in the array.

Example 2

Create an Industry array with five industries and return the index of “Government.”

import numpy

# Create numpy array - Industry with 5 strings
Industry = numpy.array(['Agriculture','Banking','Government','Government','Government'])
print(Industry,"\n")

# Return the index of Government
print(numpy.where(Industry == 'Government'))

Output

‘Government’ appears three times in the array, so three indices were returned.

Solution 2: Using numpy.argwhere()

This next approach is using the numpy.argwhere() function, which returns the indices of all elements that are not equal to 0. It returns the array of nested indices in the format – array([[],[],…])

Syntax

Let’s see the syntax of this function.

numpy.argwhere(array_name)

Example

Create a numpy array named Budgets with five integers and use the above function to return the index of non-zero elements.

import numpy

# Create numpy array - Budgets with 5 integers
Budgets = numpy.array([0,2000,3000,0,0])
print(Budgets,"\n")

# Return the index of non-zero elements
numpy.argwhere(Budgets)

Output

There are two non-zero elements and their index positions are 1 and 2.

Conclusion

The index() method is not applied on the numpy array as it will return the AttributeError – ‘numpy.ndarray’ object has no attribute index. Instead, use the numpy.where() function by specifying the conditions. Based on the condition, the indices will be returned. If you want the index positions of non-zero elements, use the numpy.argwhere() function.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain