Python: ufunc ‘add’ did not contain a loop with signature matching types dtype(‘S21’) dtype(‘S21’) dtype(‘S21’)

The problem is that you can’t add an object array (containing strings) to a number array, that’s just ambiguous:

>>> import pandas as pd

>>> pd.Series(['abc', 'def']) + pd.Series([1, 2])
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')

You need to explicitly convert your Dates to str.

I don’t know how to do that efficiently in pandas but you can use:

df1['key'] = df1['Order_ID'] + '_' + df1['Date'].apply(str)  # .apply(str) is new

Leave a Comment