Skip to content

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

Open
@alimanfoo

Description

@alimanfoo
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions