Skip to content

Commit f334fcc

Browse files
authored
Properly handle missing attributes in query/eval strings (#32408)
1 parent 12ffb23 commit f334fcc

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ Other
585585
- Bug in :meth:`DataFrame.to_records` incorrectly losing timezone information in timezone-aware ``datetime64`` columns (:issue:`32535`)
586586
- Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`).
587587
- :meth:`IntegerArray.astype` now supports ``datetime64`` dtype (:issue:32538`)
588+
- Getting a missing attribute in a query/eval string raises the correct ``AttributeError`` (:issue:`32408`)
588589
- Fixed bug in :func:`pandas.testing.assert_series_equal` where dtypes were checked for ``Interval`` and ``ExtensionArray`` operands when ``check_dtype`` was ``False`` (:issue:`32747`)
589590
- Bug in :meth:`Series.map` not raising on invalid ``na_action`` (:issue:`32815`)
590591
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)

pandas/core/computation/expr.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,9 @@ def visit_Attribute(self, node, **kwargs):
635635
# something like datetime.datetime where scope is overridden
636636
if isinstance(value, ast.Name) and value.id == attr:
637637
return resolved
638+
raise
638639

639-
raise ValueError(f"Invalid Attribute context {ctx.__name__}")
640+
raise ValueError(f"Invalid Attribute context {type(ctx).__name__}")
640641

641642
def visit_Call(self, node, side=None, **kwargs):
642643

pandas/tests/frame/test_query_eval.py

+5
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,11 @@ def test_lots_of_operators_string(self, df):
11651165
expect = df[df[" &^ :!€$?(} > <++*'' "] > 4]
11661166
tm.assert_frame_equal(res, expect)
11671167

1168+
def test_missing_attribute(self, df):
1169+
message = "module 'pandas' has no attribute 'thing'"
1170+
with pytest.raises(AttributeError, match=message):
1171+
df.eval("@pd.thing")
1172+
11681173
def test_failing_quote(self, df):
11691174
with pytest.raises(SyntaxError):
11701175
df.query("`it's` > `that's`")

0 commit comments

Comments
 (0)