TypeError: type numpy.ndarray doesn't define __round__ method

Borislav Hadzhiev

Last updated: Apr 13, 2024

Reading timeยท3 min

**The NumPy "TypeError: type numpy.ndarray doesn't define __round__ method"
occurs when you try to pass a NumPy array to the native Python round()
function.**

**To solve the error, pass the NumPy array to the numpy.round() method
instead.**

Here is an example of how the error occurs.

main.py

`import numpy as np arr = np.array([1.3, 2.6, 3.4, 4.7, 5.9]) rounded_arr = round(arr) # โ๏ธ TypeError: type numpy.ndarray doesn't define __round__ method print(rounded_arr)`

The issue in the code sample is that the native Python round() doesn't handle NumPy arrays.

`numpy.round()`

method to solve the errorYou can solve the error by passing the NumPy array to the numpy.round() method.

main.py

`import numpy as np arr = np.array([1.3, 2.6, 3.4, 4.7, 5.9]) rounded_arr = np.round(arr) # ๐๏ธ [1. 3. 3. 5. 6.] print(rounded_arr)`

The code for this article is available on GitHub

The `numpy.round()`

method takes a NumPy array as a parameter and evenly rounds
to the given number of decimals.

If the number of decimal places is not supplied, it is assumed to be `0`

.

Here is an example that rounds the elements of the NumPy array to 2 decimal places.

main.py

`import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.round(arr, 2) # ๐๏ธ [1.35 2.68 3.46 4.71 5.95] print(rounded_arr)`

The code for this article is available on GitHub

`numpy.round()`

You can also use the `numpy.round()`

method to round a two-dimensional NumPy
array.

main.py

`import numpy as np arr = np.array([ [1.345, 2.678], [3.456, 4.713], [5.945, 6.496] ]) rounded_arr = np.round(arr) # [[1. 3.] # [3. 5.] # [6. 6.]] print(rounded_arr)`

The code for this article is available on GitHub

If you get an error when using the code sample above, try using a list comprehension.

main.py

`import numpy as np arr = np.array([ [1.345, 2.678], [3.456, 4.713], [5.945, 6.496] ]) rounded_arr = [np.round(x) for x in arr] # [array([1., 3.]), array([3., 5.]), array([6., 6.])] print(rounded_arr)`

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we pass the current NumPy array to the `numpy.round()`

method
and return the result.

`numpy.around()`

methodThere is also a numpy.around() method that is used to round a NumPy array to the given number of decimals.

The `numpy.around`

method is an alias of `numpy.round`

so it does the same.

main.py

`import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.around(arr) # [1. 3. 3. 5. 6.] print(rounded_arr)`

The code for this article is available on GitHub

You can also pass your preferred number of decimal places as the second argument
to `around()`

(it defaults to `0`

).

main.py

`import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.around(arr, 2) # [1.35 2.68 3.46 4.71 5.95] print(rounded_arr)`

`numpy.ceil`

If only want to round the number in an array up, use the numpy.ceil() method.

main.py

`import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.ceil(arr) # ๐๏ธ [2. 3. 4. 5. 6.] print(rounded_arr)`

The code for this article is available on GitHub

The `numpy.ceil()`

method returns the ceiling of the supplied array,
element-wise.

The ceil of the scalar `x`

is the smallest integer `i`

, such that `i`

>= `x`

.

`numpy.floor`

If you only want to round the numbers in an array down, use the numpy.floor() method.

main.py

`import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.floor(arr) # ๐๏ธ [1. 2. 3. 4. 5.] print(rounded_arr)`

The `numpy.floor()`

method returns the floor of the supplied array,
element-wise.

The floor of the number `x`

is the largest integer `i`

, such that `i`

<= `x`

.

I've also written an article on how to round a float to N decimal places in Python.

You can learn more about the related topics by checking out the following tutorials: