FutureWarning: Elementary comparison failed; returns a scalar instead

I get a warning and I want to check if it breaks. I use np.where, as it is in many cases (for me it is like an if statement in excel). Is there a better or more pythonic or pandas way to do this? I am trying to turn one dimension into something with which I can easily perform mathematical operations.

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
+6
source share
1 answer

I think you can try other alternatives - compared to numpy in1dor perhaps str.contains, but for me all 3 approaches work very well (but mine python: 2.7.11.final.0, pandas: 0.18.0and numpy: 1.10.4):

print df
                      result
0               Action Taken
1  Action Taken Action Taken
2                     Action
3             Something else

print df['result']=='Action Taken'
0     True
1    False
2    False
3    False
Name: result, dtype: bool

print np.in1d(df['result'],'Action Taken')
[ True False False False]

print df['result'].str.contains('Action Taken')
0     True
1     True
2    False
3    False
Name: result, dtype: bool

df['closed_item'] = np.where(df['result']=='Action Taken', 1, 0)
df['closed_item1'] = np.where(np.in1d(df['result'],'Action Taken'), 1, 0)
df['closed_item2'] = np.where(df['result'].str.contains('Action Taken'), 1, 0)
print df
                      result  closed_item  closed_item1  closed_item2
0               Action Taken            1             1             1
1  Action Taken Action Taken            0             0             1
2                     Action            0             0             0
3             Something else            0             0             0
0
source

All Articles