Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FutureWarning: elementwise comparison failed; returning scalar instead

Tags:

python

pandas

I am receiving a warning and I want to check if this will break. I am using np.where like this in a lot of cases (it is similar, for me, to an if statement in excel). Is there a better or more pythonic or pandas way to do this? I'm trying to turn one dimension into something I can easily do mathematical operations on.

df['closed_item'] = np.where(df['result']=='Action Taken', 1, 0)

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  result = getattr(x, name)(y)


INSTALLED VERSIONS
------------------
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 10

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.0
setuptools: 20.2.2
Cython: 0.23.4
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.0.0
sphinx: 1.3.1
patsy: 0.4.0
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.1
matplotlib: 1.5.1
openpyxl: 2.2.6
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.7.7
lxml: 3.4.4
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.38.0
like image 804
trench Avatar asked Apr 14 '16 10:04

trench


1 Answers

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

df['closed_item'] = np.where(df['result'].astype(str)=='Action Taken', 1, 0)
like image 100
Allen Sun Avatar answered Sep 30 '22 09:09

Allen Sun