Saturday, January 22, 2022

ItsMyCode: TypeError: ‘numpy.float64’ object cannot be interpreted as an integer

The TypeError: ‘numpy.float64’ object cannot be interpreted as an integer occurs if you pass a float value to a function like range() which accepts only integer.

In this tutorial, let us look at what is TypeError: ‘numpy.float64’ object cannot be interpreted as an integer and how to resolve this error with examples.

What is TypeError: ‘numpy.float64’ object cannot be interpreted as an integer?

The TypeErrors are very common in Python, and usually, we get if we pass the wrong data type to a function.  

The range() function expects an integer. However, while working with NumPy arrays, it is common that sometimes we pass a float value into the range() function and get a TypeError.

Let us take an example to reproduce this error in Python.

# import numpy library
import numpy as np

# create array of values in pandas
my_array = np.array([2.5, 6.4, 2.1, 7.4, 8.9, 1.1])

#  print the range of values using for loop
for i in range(len(my_array)):
    print(range(my_array[i]))

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\program.py", line 10, in <module>
    print(range(my_array[i]))
TypeError: 'numpy.float64' object cannot be interpreted as an integer

How to Fix TypeError: ‘numpy.float64’ object cannot be interpreted as an integer?

There are two ways to fix the TypeError.

  • Using astype() method
  • Using int() method

Let us take a look at both methods with examples.

Method 1: Using the astype() function 

The astype() method comes in handy when we have to convert one data type into another data type.

We can fix our code by converting the values of the NumPy array to an integer using the astype() method, as shown below.

# import numpy library
import numpy as np

# create array of values in pandas
my_array = np.array([2.5, 6.4, 2.1, 7.4, 8.9, 1.1])

# covert values of array to integer using astype()
my_array = my_array.astype(int)
print("Converted array is", my_array)

#  print the range of values using for loop
for i in range(len(my_array)):
    print(range(my_array[i]))

Output

Converted array is [2 6 2 7 8 1]
range(0, 2)
range(0, 6)
range(0, 2)
range(0, 7)
range(0, 8)
range(0, 1)

Method 2: Using the int() function 

Another way to fix the issue is to cast the array object to an integer using the int() method before getting into range.

The int() method will convert each float value to an integer in the NumPy array, thus avoiding the TypeError.

# import numpy library
import numpy as np

# create array of values in pandas
my_array = np.array([2.5, 6.4, 2.1, 7.4, 8.9, 1.1])

#  print the range of values using for loop
for i in range(len(my_array)):
    # cast to integer before applying the range
    print(range(int(my_array[i])))

Output

range(0, 2)
range(0, 6)
range(0, 2)
range(0, 7)
range(0, 8)
range(0, 1)

Conclusion

 If you pass a float value to functions like range() which can only accept integer Python will raise TypeError: ‘numpy.float64’ object cannot be interpreted as an integer

There are two ways to fix this TypeError. 

  1. We can use astype() method to convert the values of the NumPy array to an integer 
  2. We can cast the array object to an integer using the int() method before getting into range.


from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...