Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two dataframes, which both have an Order ID and a date.

I wanted to add a flag into the first dataframe df1: if a record with the same order id and date is in dataframe df2, then add a Y:

[ df1['R'] = np.where(orders['key'].isin(df2['key']), 'Y', 0)] 

To accomplish that, I was going to create a key, which would be the concatenation of the order_id and date, but when I try the following code:

df1['key']=df1['Order_ID']+'_'+df1['Date'] 

I get this error

ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21') 

df1 looks like this:

Date | Order_ID | other data points ...  201751 4395674  ... 201762 3487535  ... 

These are the datatypes:

df1.info() RangeIndex: 157443 entries, 0 to 157442 Data columns (total 6 columns): Order_ID                                 157429 non-null object Date                                     157443 non-null int64 ... dtypes: float64(2), int64(2), object(2) memory usage: 7.2+ MB  df1['Order_ID'].values array(['782833030', '782834969', '782836416', ..., '783678018',        '783679806', '783679874'], dtype=object) 
like image 687
jeangelj Avatar asked Jun 13 '17 17:06

jeangelj


1 Answers

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 
like image 128
MSeifert Avatar answered Oct 01 '22 01:10

MSeifert