Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python loc + isin returns FutureWarning (elementwise comparison failed)

df = df.loc[df['var'].isin(df2['this'].unique().tolist())]

The code above gives me a FutureWarning and I can't work out a way around it. Is this a bug or feature?

C:\Users\Username\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\arraysetops.py:569: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison mask |= (ar1 == a)

like image 806
Stphn Avatar asked Aug 11 '26 10:08

Stphn


1 Answers

This warning occurs when comparing "int" and "str" in your dataset. Add .astype(str) to your comparison dataset. Try this

df = df.loc[df['var'].astype(str).isin(df2['this'].astype(str).unique().tolist())]
like image 163
Allen Sun Avatar answered Aug 14 '26 00:08

Allen Sun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!