Last updated: Apr 11, 2024
Reading timeยท4 min
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.
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)
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.
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
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.
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.
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).
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
dtype
when creating the arrayYou can also set the dtype (data type) when creating the array to solve the error.
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
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.
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.
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
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.
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
You can learn more about the related topics by checking out the following tutorials: