Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

pandas - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...