Skip to content

Commit 004a1c9

Browse files
authored
BUG: Solves errors when calling series methods in DataFrame.query with numexpr (#43301)
1 parent efbf499 commit 004a1c9

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ Indexing
426426
- Bug in :meth:`Index.get_indexer_non_unique` when index contains multiple ``np.nan`` (:issue:`35392`)
427427
- Bug in :meth:`DataFrame.query` did not handle the degree sign in a backticked column name, such as \`Temp(°C)\`, used in an expression to query a dataframe (:issue:`42826`)
428428
- Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`)
429+
- Bug in :meth:`DataFrame.query` where method calls in query strings led to errors when the ``numexpr`` package was installed. (:issue:`22435`)
429430
- Bug in :meth:`DataFrame.nlargest` and :meth:`Series.nlargest` where sorted result did not count indexes containing ``np.nan`` (:issue:`28984`)
430431

431432

pandas/core/computation/expr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,8 @@ def visit_Call(self, node, side=None, **kwargs):
702702
if key.arg:
703703
kwargs[key.arg] = self.visit(key.value).value
704704

705-
return self.const_type(res(*new_args, **kwargs), self.env)
705+
name = self.env.add_tmp(res(*new_args, **kwargs))
706+
return self.term_type(name=name, env=self.env)
706707

707708
def translate_In(self, op):
708709
return op

pandas/tests/frame/test_query_eval.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,26 @@ def test_check_tz_aware_index_query(self, tz_aware_fixture):
731731
result = df.reset_index().query('"2018-01-03 00:00:00+00" < time')
732732
tm.assert_frame_equal(result, expected)
733733

734+
def test_method_calls_in_query(self):
735+
# https://github.com/pandas-dev/pandas/issues/22435
736+
n = 10
737+
df = DataFrame({"a": 2 * np.random.rand(n), "b": np.random.rand(n)})
738+
expected = df[df["a"].astype("int") == 0]
739+
result = df.query(
740+
"a.astype('int') == 0", engine=self.engine, parser=self.parser
741+
)
742+
tm.assert_frame_equal(result, expected)
743+
744+
df = DataFrame(
745+
{
746+
"a": np.where(np.random.rand(n) < 0.5, np.nan, np.random.randn(n)),
747+
"b": np.random.randn(n),
748+
}
749+
)
750+
expected = df[df["a"].notnull()]
751+
result = df.query("a.notnull()", engine=self.engine, parser=self.parser)
752+
tm.assert_frame_equal(result, expected)
753+
734754

735755
@td.skip_if_no_ne
736756
class TestDataFrameQueryNumExprPython(TestDataFrameQueryNumExprPandas):

0 commit comments

Comments
 (0)