Skip to content

eval should handle ternary operator #12945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
benjello opened this issue Apr 21, 2016 · 4 comments
Open

eval should handle ternary operator #12945

benjello opened this issue Apr 21, 2016 · 4 comments
Labels

Comments

@benjello
Copy link
Contributor

It would be nice to be able to write ternary operator into expressions submitted to eval.

Code Sample

df = pd.DataFrame({'a': range(10), 'b': range(10,20)})
df.eval('x = a if a < 5 else b', inplace = True)
df

Traceback (most recent call last):

  File "<ipython-input-27-89e215206825>", line 1, in <module>
    df.eval('x = a if a < 5 else b')

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 2209, in eval
    return _eval(expr, inplace=inplace, **kwargs)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/eval.py", line 245, in eval
    truediv=truediv)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 709, in __init__
    self.terms = self.parse()

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 726, in parse
    return self._visitor.visit(self.expr)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 310, in visit
    return visitor(node, **kwargs)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 316, in visit_Module
    return self.visit(expr, **kwargs)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 310, in visit
    return visitor(node, **kwargs)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 494, in visit_Assign
    return self.visit(node.value, **kwargs)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 310, in visit
    return visitor(node, **kwargs)

  File "/home/benjello/.local/lib/python2.7/site-packages/pandas/computation/expr.py", line 201, in f
    "implemented".format(node_name))

NotImplementedError: 'IfExp' nodes are not implemented

Expected Output

  a   b   x
0  0  10   0
1  1  11   1
2  2  12   2
3  3  13   3
4  4  14   4
5  5  15  15
6  6  16  16
7  7  17  17
8  8  18  18
9  9  19  19

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-1-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.0
nose: 1.3.7
pip: 1.5.6
setuptools: 18.8
Cython: 0.23.4
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.0.3
sphinx: 1.3.6
patsy: 0.4.1
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: 2.3.0
xlrd: 0.9.4
xlwt: 0.7.5
xlsxwriter: 0.7.3
lxml: 3.6.0
bs4: 4.4.1
html5lib: 0.999
httplib2: 0.9.1
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
@jreback
Copy link
Contributor

jreback commented Apr 21, 2016

this directly translates to a .where() so would kick it back out back to python land (which actually does execute it using numexpr internally. So from a syntactical point of view I guess its ok. Though remember these are vectorized expressions, not sure if that is clear to the user. I am only +0 as a result.

@jonas-eschle
Copy link

While a ternary operator itself is pure Python and can be replaced by the where, it is unfortunately currently not possible to use a where inside the eval string as mentioned in #34834. However, resolving #34834 would also resolve the issue here I assume?

@RinnetenseiQ
Copy link

Now I developing service for some data calculation relates to Machine Learning.
It requres to have a possibility to calculate ternary equition that could be send by user like this:

"a = 0 if b < 0 else a"

image
Because I have a string as a start point, I must to implement own parser to convert this string to "where-or-aplly-based" function, I'm still sure that it should be implemented as part of eval() method

@unnir
Copy link

unnir commented Mar 28, 2024

It would be really great to have!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants