pythonpandasdataframepandas-locfuture-warning

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)


Solution

  • 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())]