ufunc 'add' did not contain loop with signature matching types

avatar
Borislav Hadzhiev

Last updated: Jul 1, 2023
3 min

banner

# ufunc 'add' did not contain loop with signature matching types

The error "ufunc 'add' did not contain a loop with signature matching types" occurs when you try to compute a value but your array contains multiple types, e.g. integers and strings.

To solve the error, use the astype() method to convert the values in the array to integers or floats.

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array([1, 3, 5, 7, 9, 11, '13', '15']) # โ›”๏ธ numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> None mean = np.mean(arr)

ufunc add did not contain loop with signature matching types

We tried to calculate the average of the array elements, however, the array contains integers and strings.

To solve the error, use the numpy.astype method to create a copy of the array cast to a specific type.

main.py
import numpy as np arr = np.array([1, 3, 5, 7, 9, 11, '13', '15']) arr = arr.astype(float) mean = np.mean(arr) print(mean) # ๐Ÿ‘‰๏ธ 8.0

make all array elements consistent type

We passed the float class to the astype() method to convert all array elements to floating-point numbers before calling numpy.mean().

If you need to convert the elements to integers, use the int() class instead.

main.py
import numpy as np arr = np.array([1, 3, 5, 7, 9, 11, '13', '15']) # ๐Ÿ‘‡๏ธ using `int` class. arr = arr.astype(int) print(arr) # ๐Ÿ‘‰๏ธ [ 1 3 5 7 9 11 13 15] mean = np.mean(arr) print(mean) # ๐Ÿ‘‰๏ธ 8.0

Similarly, if you need to convert all elements to strings, use the str class.

All array elements must be the same type to solve the error.

Here is another example of how the error occurs.

main.py
import numpy as np arr = np.array([1, 3, 5, 7, 9, 11, '13', '15']) # โ›”๏ธ numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> None total = arr.sum()

The array contains integers and strings, so calling sum() directly is not allowed.

To solve the error, use the astype() method to convert all elements in the array to integers (or floats).

main.py
import numpy as np arr = np.array([1, 3, 5, 7, 9, 11, '13', '15']) arr = arr.astype(int) total = arr.sum() print(total) # ๐Ÿ‘‰๏ธ 64

convert all elements in the array to integers before computation

# Setting the dtype when creating the array

You can also set the dtype (data type) when creating the array to solve the error.

main.py
import numpy as np arr = np.array([1, 3, 5, 7, 9, 11, '13', '15'], dtype=float) mean = np.mean(arr) print(mean) # ๐Ÿ‘‰๏ธ 8.0

set data type of array explicitly

We explicitly set the data type of the array to float, so all non-float values will get converted to floating-point numbers when the array is created.

# Solving the error when working with DataFrames or Series

You might also get the error when working with DataFrame or Series objects.

The solution is the same - you have to convert the values in the row to a consistent type.

main.py
import pandas df = pandas.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'age': [30, 40, '50'], 'salary': [75, 50, '100'] }) mean = df['age'].astype(int).mean() print(mean) # ๐Ÿ‘‰๏ธ 40.0

using apply to solve the error

We used the astype() method to convert the values in the age column to integers before calling mean().

You can also use the apply() method to achieve the same result.

main.py
import pandas df = pandas.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'age': [30, 40, '50'], 'salary': [75, 50, '100'] }) mean = df['age'].apply(int).mean() print(mean) # ๐Ÿ‘‰๏ธ 40.0

using apply to solve the error

# Additional Resources

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev