Skip to content

Python NumPy: How to Fix "TypeError: 'numpy.float64' object is not iterable"

Learn how to resolve the TypeError: 'numpy.float64' object is not iterable by converting scalar values to iterables like NumPy arrays or lists for loops and functions like min(), max(), or sum().

When working with NumPy, you might encounter the TypeError: 'numpy.float64' object is not iterable. This error occurs when you attempt to use a single NumPy floating-point number (a scalar value) in a context that requires a sequence of items (an iterable), such as a for loop or a function like min(), max(), or sum(). A scalar is a single, 0-dimensional value and cannot be looped over.

This guide will explain the difference between scalar values and iterables, demonstrate the common scenarios that trigger this TypeError, and provide the straightforward solution of ensuring you are working with an iterable data structure like a NumPy array or a Python list.

Understanding the Error: Iterables vs. Scalar Values

To understand this error, it's crucial to distinguish between two fundamental concepts:

  • Scalar: A single value, such as a Python int or float, or a NumPy-specific type like numpy.int64 or numpy.float64. A scalar represents one quantity and is not a collection. For example, 5.0 or np.float64(10.5).
  • Iterable: A collection of items that can be looped over, one element at a time. Examples include Python lists ([1, 2, 3]), tuples ((1, 2, 3)), and NumPy arrays (np.array([1, 2, 3])).

The TypeError: 'numpy.float64' object is not iterable occurs when you provide a scalar where Python expects an iterable.

Scenario 1: Using a NumPy Float in a for Loop

A for loop is designed to iterate over the elements of a sequence. Attempting to use a single numpy.float64 value in a for loop is the most common way to trigger this error.

Example of the code causing the error:

import numpy as np

score = np.float64(95.5)

try:
    for i in score:
        print(i)
except TypeError as e:
    print(f"Error: {e}")

Output:

Error: 'numpy.float64' object is not iterable

Scenario 2: Passing a NumPy Float to a Function Expecting an Iterable

Many built-in Python functions, such as min(), max(), sum(), and list(), are designed to operate on a sequence of values. Passing a single scalar numpy.float64 to them will also result in this TypeError.

Example of the code causing the error:

import numpy as np

score = np.float64(88.0)

try:
    result = min(score)
    print(result)
except TypeError as e:
    print(f"Error: {e}")

Output:

Error: 'numpy.float64' object is not iterable

Solution: Ensure You Provide an Iterable

The solution in all cases is to make sure the object you are trying to iterate over is, in fact, an iterable. If you have a single scalar value that you need to process in a loop or with a function like min(), you must wrap it in an iterable container like a NumPy array or a Python list.

Correct Approach:

import numpy as np

score_scalar = np.float64(95.5)

scores_array = np.array([score_scalar])

print("Iterating over a single-element NumPy array:")
for i in scores_array:
    print(i)

print()
print("Using min() on a single-element NumPy array:")
result = min(scores_array)
print(result)

Output:

Iterating over a single-element NumPy array:
95.5

Using min() on a single-element NumPy array:
95.5

Example with a Multi-Dimensional Array:

import numpy as np

scores_2d = np.array([[5.0, 6.0], [1.2, 9.1]])

print("Iterating over a 2D array (iterates over rows):")
for row in scores_2d:
    print(f"Row: {row}, Minimum value in row: {min(row)}")

Output:

Iterating over a 2D array (iterates over rows):
Row: [5. 6.], Minimum value in row: 5.0
Row: [1.2 9.1], Minimum value in row: 1.2

Conclusion

The TypeError: 'numpy.float64' object is not iterable is a fundamental error that arises from confusing a single scalar value with a collection of items. To resolve it:

  • Always ensure that any object you pass to a for loop or to functions like min(), max(), and sum() is an iterable (e.g., a NumPy ndarray, a Python list, or a tuple).
  • If you have a single scalar value, wrap it in a NumPy array or a list (e.g., np.array([my_scalar])) before attempting to iterate over it.

By maintaining this distinction, you can easily avoid this common TypeError in your NumPy code.

Navigation