Skip to content

Commit ccf0b53

Browse files
committed
BUG: Fix query doesn't support equals or not equals with Python parser (pandas-dev#40436)
1 parent 8837b36 commit ccf0b53

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ Other
494494
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
495495
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
496496
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
497+
- Bug in :method:`DataFrame.query`, where "python" ``parser`` would raise NotImplementedError for == and != (:issue:`40436`)
497498

498499

499500
.. ---------------------------------------------------------------------------

pandas/core/computation/expr.py

-2
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,6 @@ class BaseExprVisitor(ast.NodeVisitor):
384384
unary_op_nodes_map = {k: v for k, v in zip(unary_ops, unary_op_nodes)}
385385

386386
rewrite_map = {
387-
ast.Eq: ast.In,
388-
ast.NotEq: ast.NotIn,
389387
ast.In: ast.In,
390388
ast.NotIn: ast.NotIn,
391389
}

pandas/tests/frame/test_query_eval.py

+7
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,13 @@ def test_query(self):
548548
df[df.a + df.b > df.b * df.c],
549549
)
550550

551+
def test_query_equality(self):
552+
engine, parser = self.engine, self.parser
553+
df = DataFrame({"x": np.random.choice(["A", "B", "C"], size=20)})
554+
tm.assert_frame_equal(
555+
df.query("x == 'A'", engine=engine, parser=parser), df[df.x == "A"]
556+
)
557+
551558
def test_query_index_with_name(self):
552559
engine, parser = self.engine, self.parser
553560
df = DataFrame(

0 commit comments

Comments
 (0)