How to Solve Python TypeError: ‘numpy.float64’ object is not iterable

by | Programming, Python, Tips

This error occurs when you try to iterate over a numpy.float64 object, for example, using a for loop.

You can solve this error by converting the numpy.float64 to an int and then passing it to the range() method to get an iterable to iterate over. For example,

import numpy as np

arr = np.array([3.2, 7.5, 8.6, 4.5, 9.0])

rounded_arr = np.round(arr)

min_val = min(rounded_arr)

for val in range(int(min_val)):

    print(val)

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: ‘numpy.float64’ object is not iterable

TypeError occurs in Python when you perform an illegal operation for a specific data type. A numpy.float64 is the double-precision floating-point number type, and we cannot iterate over it.

What is an Iterable Object in Python?

An iterable is an object that can be “iterated over“, for example in a for loop. In terms of dunder methods under the hood, an object can be iterated over with “for” if it implements __iter__() or __getitem__().

An iterator returns the next value in the iterable object. An iterable generates an iterator when it is passed to the iter() method.

In terms of dunder methods under the hood, an iterator is an object that implements the __next__() method.

A for loop automatically calls the iter() method to get an iterator and then calls next over and over until it reaches the end of the iterable object.

Example #1

Let’s look at an example of trying to iterate over a numpy.float64 object.

First, we will define an array of numpy.float64 values.

import numpy as np

arr = np.array([2.4, 3.2, 1.0, 0.4, 7.5, 8.6])

Next, we will iterate over the numpy array and pass each value in the array to the built-in method min().

for val in arr:

    print(min(val))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <cell line: 5>()
      3 arr = np.array([2.4, 3.2, 1.0, 0.4, 7.5, 8.6])
      5 for val in arr:
----> 7     print(min(val))

TypeError: 'numpy.float64' object is not iterable

The error occurs because the min() method requires an iterable object with one or more items to compare. We passed a numpy.float64 to the min() method, which is not iterable.

We can check if an object is iterable by passing it to the dir() method to get its list of attributes and look for __iter__. If __iter__ does not exist in the list of attributes then the object is not iterable.

import numpy as np

arr = np.array([2.4, 3.2, 1.0, 0.4, 7.5, 8.6])

val = arr[0]

attributes = dir(val)
print('__iter__' in attributes)
print('__getitem__' in attributes)

Solution

We can solve the error by using a two-dimensional array instead of a one-dimensional array. Each item in a two-dimensional array is an array. Therefore, we can iterate over the two-dimensional array and pass each item to the min() method call. Let’s look at the revised code:

import numpy as np

arr = np.array([[2.4, 3.2, 1.0], [0.4, 7.5, 8.6]])

for val in arr:

    print(min(val))

Let’s run the code to see the result:

1.0
0.4

We successfully calculated the minimum value in the two arrays.

Example #2

Let’s look at another example of trying to iterate over a numpy.float64 object.

import numpy as np

arr = np.array([3.2, 7.5, 8.6, 4.5, 9.0])

for val in min(arr):
    print(val)

In the above code, we defined an array containing numpy.float64 values and then tried to iterate over the minimum value of the array. Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 5>()
      1 import numpy as np
      3 arr = np.array([2.4, 3.2, 1.0, 7.5, 8.6])
----> 5 for val in min(arr):
      6     print(val)

TypeError: 'numpy.float64' object is not iterable

The error occurs because the min() method call returns a numpy.float64 object, which we cannot iterate over.

Solution

We can solve this error by converting the numpy.float64 object to an int using the int() method and then passing it to the range() method. The range() method returns a range object consisting of a sequence of integers, which is iterable.

Let’s look at the revised code:

import numpy as np

# Define numpy array

arr = np.array([3.2, 7.5, 8.6, 4.5, 9.0])

# Round values to nearest integer

rounded_arr = np.round(arr)

# Retrieve minimum value from array

min_val = min(rounded_arr)

# Iterate over range object

for val in range(int(min_val)):

    print(val)

Let’s run the code to see the result:

0
1
2

Summary

Congratulations on reading to the end of this tutorial!

For more reading on not iterable TypeErrors, go to the article:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

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.

Buy Me a Coffee