Skip to content
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

'BinOp' object has no attribute 'value' Issue while calling global functions in eval and importing numpy #24670

Open
anjsudh opened this issue Jan 8, 2019 · 7 comments
Assignees
Labels
Bug expressions pd.eval, query

Comments

@anjsudh
Copy link
Contributor

anjsudh commented Jan 8, 2019

import pandas as pd
from  numpy import *
pd.eval("floor(1)")
Output: 1.0
pd.eval("floor(1+2)")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/eval.py", line 291, in eval
    truediv=truediv)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 739, in __init__
    self.terms = self.parse()
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 756, in parse
    return self._visitor.visit(self.expr)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 321, in visit
    return visitor(node, **kwargs)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 327, in visit_Module
    return self.visit(expr, **kwargs)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 321, in visit
    return visitor(node, **kwargs)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 330, in visit_Expr
    return self.visit(node.value, **kwargs)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 321, in visit
    return visitor(node, **kwargs)
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 579, in visit_Call_35
    new_args = [self.visit(arg).value for arg in node.args]
  File "/Users/sanjana/simulation-service/venv/lib/python3.7/site-packages/pandas/core/computation/expr.py", line 579, in <listcomp>
    new_args = [self.visit(arg).value for arg in node.args]
AttributeError: 'BinOp' object has no attribute 'value'
@WillAyd
Copy link
Member

WillAyd commented Jan 8, 2019

Thanks for the report. Investigation and PRs are always welcome

@WillAyd WillAyd added this to the Contributions Welcome milestone Jan 8, 2019
@WillAyd WillAyd added Bug Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff labels Jan 8, 2019
@jbrockmendel jbrockmendel added the expressions pd.eval, query label Oct 22, 2019
@SurajH1
Copy link
Contributor

SurajH1 commented Apr 2, 2020

take

@SurajH1
Copy link
Contributor

SurajH1 commented Apr 3, 2020

After looking at the documentation, I noticed that evaluating global functions is not an explicit property of pd.eval(). However, it does seem like a useful addition and something that should be a property of the function. @WillAyd can we also add a request to update documentation?

@mroeschke
Copy link
Member

This looks to work on master, I suppose it could use a test.

In [19]: pd.__version__
Out[19]: '1.1.0.dev0+1390.gf3fdab389'

In [20]: pd.eval("floor(1)")
    ...:
Out[20]: 1.0

In [21]: pd.eval("floor(1+2)")
Out[21]: 3.0

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug expressions pd.eval, query labels Apr 26, 2020
@vmuriart
Copy link

@mroeschke I downloaded and setup the latest master branch locally to test this, and didn't get it to work.

Using:

import pandas as pd
from  numpy import *

print("Version:", pd.__version__)
print()

r = pd.eval("floor(1)")
print("Test1:", r)

r = pd.eval("floor(1+5)")
print("Test2:", r)

I still get the original error:

Version: 1.1.0.dev0+1575.gee1efb6d9

Test1: 1.0
Traceback (most recent call last):
  File "test_pd_parse.py", line 10, in <module>
    r = pd.eval("floor(1+5)")
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\eval.py", line 341, in eval
    parsed_expr = Expr(expr, engine=engine, parser=parser, env=env)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 787, in __init__
    self.terms = self.parse()
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 806, in parse
    return self._visitor.visit(self.expr)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 398, in visit
    return visitor(node, **kwargs)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 404, in visit_Module
    return self.visit(expr, **kwargs)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 398, in visit
    return visitor(node, **kwargs)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 407, in visit_Expr
    return self.visit(node.value, **kwargs)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 398, in visit
    return visitor(node, **kwargs)
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 677, in visit_Call
    new_args = [self.visit(arg).value for arg in node.args]
  File "c:\users\vmuriart\desktop\pandas\pandas\pandas\core\computation\expr.py", line 677, in <listcomp>
    new_args = [self.visit(arg).value for arg in node.args]
AttributeError: 'BinOp' object has no attribute 'value'

@SurajH1
Copy link
Contributor

SurajH1 commented May 14, 2020

@mroeschke The issue is raised if you include the line:

from numpy import *
but there is no error if you do not import the library.

@mroeschke mroeschke added Bug expressions pd.eval, query and removed Needs Tests Unit test(s) needed to prevent regressions good first issue labels May 14, 2020
@mroeschke mroeschke changed the title 'BinOp' object has no attribute 'value' Issue while calling global functions in eval 'BinOp' object has no attribute 'value' Issue while calling global functions in eval and importing numpy May 14, 2020
@mirekphd
Copy link

mirekphd commented Mar 11, 2021

I've just found out that this issue can be caused by trying to parse formulas referring to an empty pandas df column (i.e. column entirely composed of NaN's):

pd.eval('df.INNOCENT1 * df.INNOCENT2 * df.GUILTY1 * df.GUILTY2', parser="python", target=df, inplace=False)

raises

*** AttributeError: 'BinOp' object has no attribute 'type'

if:

ipdb> df.GUILTY1.value_counts(dropna=False)
NaN    10000

# or:

ipdb> df.GUILTY2.value_counts(dropna=False)
NaN    10000

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug expressions pd.eval, query
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants