Skip to content

Commit 08a090e

Browse files
Backport PR #35794: BUG: issubclass check with dtype instead of type, closes GH#24883 (#35919)
Co-authored-by: jbrockmendel <[email protected]>
1 parent d7a387b commit 08a090e

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

doc/source/whatsnew/v1.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Fixed regressions
2424

2525
Bug fixes
2626
~~~~~~~~~
27-
27+
- Bug in :meth:`DataFrame.eval` with ``object`` dtype column binary operations (:issue:`35794`)
2828
- Bug in :class:`Series` constructor raising a ``TypeError`` when constructing sparse datetime64 dtypes (:issue:`35762`)
2929
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
3030
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should bw ``""`` (:issue:`35712`)

pandas/core/computation/ops.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,21 @@ def stringify(value):
481481
self.lhs.update(v)
482482

483483
def _disallow_scalar_only_bool_ops(self):
484+
rhs = self.rhs
485+
lhs = self.lhs
486+
487+
# GH#24883 unwrap dtype if necessary to ensure we have a type object
488+
rhs_rt = rhs.return_type
489+
rhs_rt = getattr(rhs_rt, "type", rhs_rt)
490+
lhs_rt = lhs.return_type
491+
lhs_rt = getattr(lhs_rt, "type", lhs_rt)
484492
if (
485-
(self.lhs.is_scalar or self.rhs.is_scalar)
493+
(lhs.is_scalar or rhs.is_scalar)
486494
and self.op in _bool_ops_dict
487495
and (
488496
not (
489-
issubclass(self.rhs.return_type, (bool, np.bool_))
490-
and issubclass(self.lhs.return_type, (bool, np.bool_))
497+
issubclass(rhs_rt, (bool, np.bool_))
498+
and issubclass(lhs_rt, (bool, np.bool_))
491499
)
492500
)
493501
):

pandas/tests/frame/test_query_eval.py

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ def test_eval_resolvers_as_list(self):
160160
assert df.eval("a + b", resolvers=[dict1, dict2]) == dict1["a"] + dict2["b"]
161161
assert pd.eval("a + b", resolvers=[dict1, dict2]) == dict1["a"] + dict2["b"]
162162

163+
def test_eval_object_dtype_binop(self):
164+
# GH#24883
165+
df = pd.DataFrame({"a1": ["Y", "N"]})
166+
res = df.eval("c = ((a1 == 'Y') & True)")
167+
expected = pd.DataFrame({"a1": ["Y", "N"], "c": [True, False]})
168+
tm.assert_frame_equal(res, expected)
169+
163170

164171
class TestDataFrameQueryWithMultiIndex:
165172
def test_query_with_named_multiindex(self, parser, engine):

0 commit comments

Comments
 (0)