Skip to content

Commit

Permalink
BUG: eval fails for ExtensionArray (pandas-dev#58793)
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddheshBangar committed Jun 12, 2024
1 parent cce2f66 commit de5d732
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ Styler
Other
^^^^^
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
- Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`)
- Bug in :meth:`DataFrame.apply` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`)
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from pandas.core.dtypes.common import (
is_list_like,
is_numeric_dtype,
is_scalar,
)

Expand Down Expand Up @@ -508,10 +509,6 @@ def _disallow_scalar_only_bool_ops(self) -> None:
raise NotImplementedError("cannot evaluate scalar only bool ops")


def isnumeric(dtype) -> bool:
return issubclass(np.dtype(dtype).type, np.number)


class Div(BinOp):
"""
Div operator to special case casting.
Expand All @@ -525,7 +522,9 @@ class Div(BinOp):
def __init__(self, lhs, rhs) -> None:
super().__init__("/", lhs, rhs)

if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type):
if not is_numeric_dtype(lhs.return_type) or not is_numeric_dtype(
rhs.return_type
):
raise TypeError(
f"unsupported operand type(s) for {self.op}: "
f"'{lhs.return_type}' and '{rhs.return_type}'"
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ def test_eval_simple(self, engine, parser):
expected = df["a"]
tm.assert_series_equal(expected, res)

def test_extension_array_eval(self, engine, parser):
# GH#58748
df = DataFrame({"a": pd.array([1, 2, 3]), "b": pd.array([4, 5, 6])})
result = df.eval("a / b", engine=engine, parser=parser)
expected = Series([0.25, 0.40, 0.50])
tm.assert_series_equal(result, expected)


class TestDataFrameQueryWithMultiIndex:
def test_query_with_named_multiindex(self, parser, engine):
Expand Down

0 comments on commit de5d732

Please sign in to comment.