Skip to content
Prev Previous commit
Next Next commit
Report error when non boolean dtype df.query
  • Loading branch information
NumberPiOso committed Apr 25, 2022
commit 4e99d2923535c8745da050e5abf54b4fc2ec559b
4 changes: 3 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4134,7 +4134,9 @@ def query(self, expr: str, inplace: bool = False, **kwargs):
kwargs["level"] = kwargs.pop("level", 0) + 1
kwargs["target"] = None
res = self.eval(expr, **kwargs)

if not is_bool_dtype(res):
msg = f"expr must evaluate to boolean not {res.dtypes}"
raise ValueError(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, as not looked at this in detail, but if we are ensuring a bool indexer, is the following try/except still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is a common problem with the multidimensional res, so we can share the solution.

try:
result = self.loc[res]
except ValueError:
Expand Down