Fix NumPy ndarray __round__ TypeError in Python

Quick Answer

Use np.round(array, decimals) or array.round(decimals) when the input is an ndarray. If you need one value, select an element and call .item() before Python’s built-in round().

NumPy ndarray rounding visual showing array-wide and scalar rounding paths
Choose vectorized rounding for an array, or select one scalar before using Python’s built-in round().

TypeError: type numpy.ndarray doesn't define __round__ method happens when Python’s built-in round() receives a NumPy array instead of one scalar number. The built-in function calls a scalar-style rounding protocol, while a NumPy array needs vectorized rounding across its elements.

The fix is to use NumPy’s array-aware tools: np.round() or array.round(). If you truly want to round one value from the array, select one element first and convert it to a Python scalar. Do not pass the whole array to round() unless you have intentionally reduced it to one number.

This error is common when code starts with normal Python floats and later changes to NumPy arrays for performance or batch processing. The old scalar function call remains in place, but the data shape has changed from one number to many numbers.

Why the Error Happens

The built-in round() is designed for scalar values such as float, int, and objects that implement the scalar __round__ method. A NumPy array represents many values, so the scalar protocol does not apply.

import numpy as np

values = np.array([1.234, 4.567, 8.901])

rounded = round(values, 2)
print(rounded)

The array has three values. Python cannot decide whether to return one rounded number or a rounded array, so the operation fails. NumPy provides the array behavior explicitly. Treat the error as a signal to decide whether your code wants one scalar result or an array result.

Python Pool infographic showing a NumPy ndarray, scalar elements, shape, dtype, and rounding intent
Array value: A NumPy ndarray, scalar elements, shape, dtype, and rounding intent.

Use np.round() for Arrays

np.round() applies rounding element by element and returns a NumPy array. This is the most direct replacement for round(array, decimals).

import numpy as np

values = np.array([1.234, 4.567, 8.901])

rounded = np.round(values, 2)
print(rounded)

This keeps the same shape as the input and rounds each element to two decimal places. PythonPool’s NumPy round() guide covers more examples for arrays and decimal places. Use this when every value in the array should be rounded. The decimals argument controls precision without changing the array’s dimensions.

Use the ndarray round() Method

NumPy arrays also provide a method form: array.round(). It is equivalent for many common cases and can be convenient when you already have the array object.

import numpy as np

values = np.array([1.234, 4.567, 8.901])

rounded = values.round(1)
print(rounded)

Use whichever style is clearer in the surrounding code. In data-processing pipelines, the method style can read naturally because the operation stays attached to the array variable. In library code, the function style can be easier when input may be list-like.

Python Pool infographic comparing NumPy array rounding with extracting a scalar before Python round
Scalar extraction: NumPy array rounding with extracting a scalar before Python round.

Round One Scalar From an Array

If you only need one value, select one element first. Use .item() when you want a plain Python scalar before calling round().

import numpy as np

values = np.array([1.234, 4.567, 8.901])

first_value = values[0].item()
print(round(first_value, 2))

This works because first_value is one number. Use this only when selecting one element is actually the intended behavior. If all elements matter, use NumPy’s vectorized rounding instead. Selecting the first element just to silence the error usually creates a data bug.

Python Pool infographic mapping an ndarray through NumPy rounding, decimals, dtype, and output
Vectorized rounding: An ndarray through NumPy rounding, decimals, dtype, and output.

Round After Reducing the Array

Another valid scalar case is when you reduce the array to one value first, such as a mean, sum, minimum, or maximum.

import numpy as np

values = np.array([1.234, 4.567, 8.901])

average = values.mean()
print(round(average.item(), 2))

Here the array is reduced to one value before rounding. This is different from rounding every element. Make that distinction clear in code reviews and tests. A rounded mean and a mean of rounded values can produce different results.

Convert Input to an Array Before Rounding

If a function may receive a list, tuple, or array, convert the input with np.asarray() and then apply NumPy rounding. This gives the function one predictable path.

import numpy as np

def round_values(values, decimals=2):
    array = np.asarray(values, dtype=float)
    return np.round(array, decimals)

print(round_values([1.234, 4.567]))

This pattern is useful for small utility functions that should accept list-like input. It also avoids repeated type checks in the rest of the function. If callers should pass exactly one value, reject array input early instead of converting it silently.

Python Pool infographic testing zero-dimensional, one-element, and multi-element arrays before rounding
Shape checks: Zero-dimensional, one-element, and multi-element arrays before rounding.

Checklist

  • Use np.round(array, decimals) for array-wide rounding.
  • Use array.round(decimals) when the method style is clearer.
  • Select one element before using Python’s built-in round().
  • Reduce the array first when the desired result is one rounded scalar.

This error is related to other scalar-versus-array mistakes. PythonPool’s only size-1 arrays can be converted to Python scalars guide explains the same underlying idea for scalar conversion. If you are truncating instead of rounding, see Python truncate().

Before changing code, decide whether the expected output should be one value or an array with the same shape. That decision points to the correct fix immediately.

References

Check the Output Shape Before Choosing a Fix

The error is a useful boundary between scalar and array operations. Decide whether the next function expects one number or an array with the original shape. That decision prevents a silent data loss bug caused by selecting the first element just to make the exception disappear.

import numpy as np

values = np.array([1.234, 4.567, 8.901])

array_result = np.round(values, 2)
scalar_result = round(values[0].item(), 2)
print(array_result, scalar_result)

The two results are both valid, but they answer different questions. Keep the array path for vectorized data and use the scalar path only when the surrounding logic explicitly calls for one value.

Frequently Asked Questions

Why does round() fail on a NumPy array?

Python’s built-in round() follows a scalar rounding protocol, while a NumPy ndarray contains multiple values. Use np.round() or array.round() for element-wise rounding.

What is the correct way to round every ndarray value?

Call np.round(array, decimals) or array.round(decimals). Both preserve the array shape and apply rounding element by element.

How do I round one value from an ndarray?

Select the intended element first, then convert it to a Python scalar with .item() before calling round().

Does rounding change the NumPy array dtype?

Rounding usually preserves a compatible numeric dtype, but the exact result depends on the input dtype and the operation. Check values and dtype when precision matters.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted