From d52ef15415a013d8f7ce386c45e3db988fc4ff8e Mon Sep 17 00:00:00 2001 From: lif <1835304752@qq.com> Date: Fri, 12 Dec 2025 11:13:28 +0800 Subject: [PATCH 1/2] BUG: Make Series flex methods (truediv, floordiv, etc.) raise NotImplementedError for bool dtypes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use arithmetic_op for arithmetic operations in Series._binop() to ensure flex methods like truediv(), floordiv(), pow() raise NotImplementedError for bool dtypes, consistent with their dunder counterparts. Comparison operations (gt, lt, etc.) continue to use direct function calls to preserve their existing behavior. Closes #63250 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- pandas/core/series.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 1ea8bbbaa0cfb..22eb33e0a95ed 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -6718,7 +6718,20 @@ def _binop(self, other: Series, func, level=None, fill_value=None) -> Series: this_vals, other_vals = ops.fill_binop(this._values, other._values, fill_value) with np.errstate(all="ignore"): - result = func(this_vals, other_vals) + # GH#63250: Use arithmetic_op for arithmetic operations to ensure + # consistent behavior with dunder methods (includes _bool_arith_check) + # But use direct func call for comparison operations (gt, lt, etc.) + if func in ( + operator.gt, + operator.ge, + operator.lt, + operator.le, + operator.eq, + operator.ne, + ): + result = func(this_vals, other_vals) + else: + result = ops.arithmetic_op(this_vals, other_vals, func) name = ops.get_op_result_name(self, other) From 2acf61362b340b15383a813449c8d2f1a73a4222 Mon Sep 17 00:00:00 2001 From: majiayu000 <1835304752@qq.com> Date: Sun, 14 Dec 2025 00:39:03 +0800 Subject: [PATCH 2/2] TST: Add test for bool dtype flex methods raising NotImplementedError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add test_flex_method_bool_dtype_raises to verify that Series flex methods (truediv, floordiv, pow, and their reverse variants) raise NotImplementedError for bool dtypes, consistent with dunder methods. Closes #63250 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- pandas/tests/series/test_arithmetic.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index a77e55612e23d..94d78df665aaf 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -198,6 +198,20 @@ def test_flex_disallows_dataframe(self): with pytest.raises(TypeError, match=msg): ser.add(df, axis=0) + @pytest.mark.parametrize( + "opname", ["truediv", "rtruediv", "floordiv", "rfloordiv", "pow", "rpow"] + ) + def test_flex_method_bool_dtype_raises(self, opname): + # GH#63250 - flex methods should raise NotImplementedError for bool dtypes + # consistent with their dunder counterparts + ser = Series([True, False, True]) + other = Series([False, True, True]) + + op = getattr(Series, opname) + msg = f"operator '{opname.removeprefix('r')}' not implemented for bool dtypes" + with pytest.raises(NotImplementedError, match=msg): + op(ser, other) + class TestSeriesArithmetic: # Some of these may end up in tests/arithmetic, but are not yet sorted