Skip to content

Commit 0ee266c

Browse files
authored
BUG: divmod with pd.NA (#62246)
1 parent a4c63d6 commit 0ee266c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ Missing
951951
^^^^^^^
952952
- Bug in :meth:`DataFrame.fillna` and :meth:`Series.fillna` that would ignore the ``limit`` argument on :class:`.ExtensionArray` dtypes (:issue:`58001`)
953953
- Bug in :meth:`NA.__and__`, :meth:`NA.__or__` and :meth:`NA.__xor__` when operating with ``np.bool_`` objects (:issue:`58427`)
954+
- Bug in ``divmod`` between :class:`NA` and ``Int64`` dtype objects (:issue:`62196`)
954955
-
955956

956957
MultiIndex

pandas/core/arrays/masked.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ def _arith_method(self, other, op):
791791
# will be all-True, but since this is division, we want
792792
# to end up with floating dtype.
793793
result = result.astype(np.float64)
794+
elif op_name in {"divmod", "rdivmod"}:
795+
# GH#62196
796+
res = self._maybe_mask_result(result, mask)
797+
return res, res.copy()
794798
else:
795799
# Make sure we do this before the "pow" mask checks
796800
# to get an expected exception message on shape mismatch.

pandas/tests/arrays/masked/test_arithmetic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,26 @@ def test_unary_op_does_not_propagate_mask(data, op):
246246
expected = result.copy(deep=True)
247247
ser[0] = None
248248
tm.assert_series_equal(result, expected)
249+
250+
251+
@pytest.mark.parametrize("dtype", ["Int64", "Int32", "Float32", "Float64"])
252+
def test_divmod_pdna(dtype):
253+
# GH#62196
254+
ser = pd.Series([1, 2, 3], dtype=dtype)
255+
res = divmod(pd.NA, ser)
256+
assert isinstance(res, tuple) and len(res) == 2
257+
258+
exp = pd.Series([pd.NA, pd.NA, pd.NA], dtype=dtype)
259+
tm.assert_series_equal(res[0], exp)
260+
tm.assert_series_equal(res[1], exp)
261+
262+
tm.assert_series_equal(res[0], pd.NA // ser)
263+
tm.assert_series_equal(res[1], pd.NA % ser)
264+
265+
res = divmod(ser, pd.NA)
266+
assert isinstance(res, tuple) and len(res) == 2
267+
tm.assert_series_equal(res[0], exp)
268+
tm.assert_series_equal(res[1], exp)
269+
270+
tm.assert_series_equal(res[0], ser // pd.NA)
271+
tm.assert_series_equal(res[1], ser % pd.NA)

0 commit comments

Comments
 (0)