From e69d9d8fc11f7bf04cae26e106599fb5b2a2b4be Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 3 Sep 2025 10:54:12 -0700 Subject: [PATCH] BUG: divmod with bool dtype failing to raise --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/ops/array_ops.py | 2 ++ pandas/tests/arithmetic/test_bool.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 pandas/tests/arithmetic/test_bool.py diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index ffa65032e6aae..84b6091a36c83 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1119,6 +1119,7 @@ Other - Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`) - Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`) - Bug in ``Series.replace`` when the Series was created from an :class:`Index` and Copy-On-Write is enabled (:issue:`61622`) +- Bug in ``divmod`` and ``rdivmod`` with :class:`DataFrame`, :class:`Series`, and :class:`Index` with ``bool`` dtypes failing to raise, which was inconsistent with ``__floordiv__`` behavior (:issue:`46043`) - Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`) - Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`) - Fixed bug where the :class:`DataFrame` constructor misclassified array-like objects with a ``.name`` attribute as :class:`Series` or :class:`Index` (:issue:`61443`) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 3a466b6fc7fc8..ecd2e2e4963d3 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -585,6 +585,8 @@ def maybe_prepare_scalar_for_op(obj, shape: Shape): roperator.rfloordiv, operator.pow, roperator.rpow, + divmod, + roperator.rdivmod, } diff --git a/pandas/tests/arithmetic/test_bool.py b/pandas/tests/arithmetic/test_bool.py new file mode 100644 index 0000000000000..3723b7042a3ce --- /dev/null +++ b/pandas/tests/arithmetic/test_bool.py @@ -0,0 +1,28 @@ +import pytest + +from pandas import ( + DataFrame, + Series, +) +import pandas._testing as tm + + +def test_divmod_bool_raises(box_with_array): + # GH#46043 // raises, so divmod should too + ser = Series([True, False]) + obj = tm.box_expected(ser, box_with_array) + + msg = "operator 'floordiv' not implemented for bool dtypes" + with pytest.raises(NotImplementedError, match=msg): + obj // obj + + if box_with_array is DataFrame: + msg = "operator 'floordiv' not implemented for bool dtypes" + else: + msg = "operator 'divmod' not implemented for bool dtypes" + with pytest.raises(NotImplementedError, match=msg): + divmod(obj, obj) + + # go through __rdivmod__ + with pytest.raises(NotImplementedError, match=msg): + divmod(True, obj)