Skip to content

Commit

Permalink
Properly handle missing attributes in query/eval strings (#32408)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki authored Apr 10, 2020
1 parent 12ffb23 commit f334fcc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ Other
- Bug in :meth:`DataFrame.to_records` incorrectly losing timezone information in timezone-aware ``datetime64`` columns (:issue:`32535`)
- Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`).
- :meth:`IntegerArray.astype` now supports ``datetime64`` dtype (:issue:32538`)
- Getting a missing attribute in a query/eval string raises the correct ``AttributeError`` (:issue:`32408`)
- 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`)
- Bug in :meth:`Series.map` not raising on invalid ``na_action`` (:issue:`32815`)
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,9 @@ def visit_Attribute(self, node, **kwargs):
# something like datetime.datetime where scope is overridden
if isinstance(value, ast.Name) and value.id == attr:
return resolved
raise

raise ValueError(f"Invalid Attribute context {ctx.__name__}")
raise ValueError(f"Invalid Attribute context {type(ctx).__name__}")

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

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,11 @@ def test_lots_of_operators_string(self, df):
expect = df[df[" &^ :!€$?(} > <++*'' "] > 4]
tm.assert_frame_equal(res, expect)

def test_missing_attribute(self, df):
message = "module 'pandas' has no attribute 'thing'"
with pytest.raises(AttributeError, match=message):
df.eval("@pd.thing")

def test_failing_quote(self, df):
with pytest.raises(SyntaxError):
df.query("`it's` > `that's`")
Expand Down

0 comments on commit f334fcc

Please sign in to comment.