Skip to content

Commit de5d732

Browse files
BUG: eval fails for ExtensionArray (#58793)
1 parent cce2f66 commit de5d732

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ Styler
596596
Other
597597
^^^^^
598598
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
599+
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
599600
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
600601
- Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`)
601602
- Bug in :meth:`DataFrame.apply` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`)

pandas/core/computation/ops.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from pandas.core.dtypes.common import (
2121
is_list_like,
22+
is_numeric_dtype,
2223
is_scalar,
2324
)
2425

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

510511

511-
def isnumeric(dtype) -> bool:
512-
return issubclass(np.dtype(dtype).type, np.number)
513-
514-
515512
class Div(BinOp):
516513
"""
517514
Div operator to special case casting.
@@ -525,7 +522,9 @@ class Div(BinOp):
525522
def __init__(self, lhs, rhs) -> None:
526523
super().__init__("/", lhs, rhs)
527524

528-
if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type):
525+
if not is_numeric_dtype(lhs.return_type) or not is_numeric_dtype(
526+
rhs.return_type
527+
):
529528
raise TypeError(
530529
f"unsupported operand type(s) for {self.op}: "
531530
f"'{lhs.return_type}' and '{rhs.return_type}'"

pandas/tests/frame/test_query_eval.py

+7
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ def test_eval_simple(self, engine, parser):
202202
expected = df["a"]
203203
tm.assert_series_equal(expected, res)
204204

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

206213
class TestDataFrameQueryWithMultiIndex:
207214
def test_query_with_named_multiindex(self, parser, engine):

0 commit comments

Comments
 (0)