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

BUG: DataFrame.query doesn't support equals or not equals for string columns with python parser #40436

Open
2 tasks done
alimanfoo opened this issue Mar 14, 2021 · 0 comments
Open
2 tasks done
Labels
Bug expressions pd.eval, query

Comments

@alimanfoo
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.


Code Sample, a copy-pastable example

In [3]: import pandas as pd

In [4]: pd.__version__
Out[4]: '1.2.3'

In [5]: import numpy as np

In [7]: x = np.random.choice(["foo", "bar", "baz"], size=10, replace=True)

In [8]: df = pd.DataFrame({"x": x})

In [9]: df
Out[9]: 
     x
0  bar
1  baz
2  foo
3  baz
4  bar
5  baz
6  baz
7  bar
8  foo
9  foo

In [10]: df.query("x == 'bar'")
Out[10]: 
     x
0  bar
4  bar
7  bar

In [11]: df.query("x == 'bar'", parser="python")
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)

...

~/malariagen/binder/conda/envs/xarray-tests/lib/python3.8/site-packages/pandas/core/computation/expr.py in f(self, *args, **kwargs)
    251 
    252     def f(self, *args, **kwargs):
--> 253         raise NotImplementedError(f"'{node_name}' nodes are not implemented")
    254 
    255     return f

NotImplementedError: 'In' nodes are not implemented

In [12]: df.query("x != 'foo'")
Out[12]: 
     x
0  bar
1  baz
3  baz
4  bar
5  baz
6  baz
7  bar

In [13]: df.query("x != 'foo'", parser="python")
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)

...

~/malariagen/binder/conda/envs/xarray-tests/lib/python3.8/site-packages/pandas/core/computation/expr.py in f(self, *args, **kwargs)
    251 
    252     def f(self, *args, **kwargs):
--> 253         raise NotImplementedError(f"'{node_name}' nodes are not implemented")
    254 
    255     return f

NotImplementedError: 'NotIn' nodes are not implemented

Problem description

If the query expression has an equals or not equals comparison with a string, and the "python" parser is selected, then a NotImplemented error is raised. The error messages however reports that "In" nodes are not implemented, or "NotIn" nodes are not implemented, which doesn't fit with the expression, suggesting a bug in how the query expression is being parsed.

Expected Output

Expected output should match what you get with the "pandas" parser as semantics for this type of query expression should be the same for either parser.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : f2c8480 python : 3.8.8.final.0 python-bits : 64 OS : Linux OS-release : 5.4.0-66-generic Version : #74~18.04.2-Ubuntu SMP Fri Feb 5 11:17:31 UTC 2021 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8

pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 49.6.0.post20210108
Cython : None
pytest : 6.2.2
hypothesis : 6.8.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.7
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.17.1.dev3+g48378c4b
xlrd : None
xlwt : None
numba : 0.52.0

@alimanfoo alimanfoo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 14, 2021
@lithomas1 lithomas1 added the expressions pd.eval, query label Mar 16, 2021
@lithomas1 lithomas1 removed the Needs Triage Issue that has not been reviewed by a pandas team member label Mar 30, 2021
@lithomas1 lithomas1 modified the milestones: 2.0, Contributions Welcome Mar 30, 2021
@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.

3 participants