Skip to content

Commit 8841969

Browse files
TomAugspurgerjorisvandenbossche
authored andcommitted
Fixed IntegerArray division by 0 (#30183)
When dividing by 0, the result should be `inf`, not `NaN`. Closes #27398
1 parent 27836e9 commit 8841969

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ Other
860860
- Bug in :meth:`DataFrame.append` that raised ``IndexError`` when appending with empty list (:issue:`28769`)
861861
- Fix :class:`AbstractHolidayCalendar` to return correct results for
862862
years after 2030 (now goes up to 2200) (:issue:`27790`)
863+
- Fixed :class:`IntegerArray` returning ``NA`` rather than ``inf`` for operations dividing by 0 (:issue:`27398`)
863864
- Bug in :meth:`Series.count` raises if use_inf_as_na is enabled (:issue:`29478`)
864865

865866

pandas/core/arrays/integer.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,6 @@ def _maybe_mask_result(self, result, mask, other, op_name):
700700
op_name : str
701701
"""
702702

703-
# may need to fill infs
704-
# and mask wraparound
705-
if is_float_dtype(result):
706-
mask |= (result == np.inf) | (result == -np.inf)
707-
708703
# if we have a float operand we are by-definition
709704
# a float result
710705
# or our op is a divide
@@ -748,7 +743,7 @@ def integer_arithmetic_method(self, other):
748743

749744
# nans propagate
750745
if mask is None:
751-
mask = self._mask
746+
mask = self._mask.copy()
752747
else:
753748
mask = self._mask | mask
754749

pandas/tests/arrays/test_integer.py

+17
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,19 @@ def test_error(self, data, all_arithmetic_operators):
339339
with pytest.raises(NotImplementedError):
340340
opa(np.arange(len(s)).reshape(-1, len(s)))
341341

342+
@pytest.mark.parametrize("zero, negative", [(0, False), (0.0, False), (-0.0, True)])
343+
def test_divide_by_zero(self, zero, negative):
344+
# https://github.com/pandas-dev/pandas/issues/27398
345+
a = pd.array([0, 1, -1, None], dtype="Int64")
346+
result = a / zero
347+
expected = np.array([np.nan, np.inf, -np.inf, np.nan])
348+
if negative:
349+
values = [np.nan, -np.inf, np.inf, np.nan]
350+
else:
351+
values = [np.nan, np.inf, -np.inf, np.nan]
352+
expected = np.array(values)
353+
tm.assert_numpy_array_equal(result, expected)
354+
342355
def test_pow(self):
343356
# https://github.com/pandas-dev/pandas/issues/22022
344357
a = integer_array([1, np.nan, np.nan, 1])
@@ -389,6 +402,10 @@ def test_compare_array(self, data, all_compare_operators):
389402
other = pd.Series([0] * len(data))
390403
self._compare_other(data, op_name, other)
391404

405+
def test_no_shared_mask(self, data):
406+
result = data + 1
407+
assert np.shares_memory(result._mask, data._mask) is False
408+
392409
def test_compare_to_string(self, any_nullable_int_dtype):
393410
# GH 28930
394411
s = pd.Series([1, None], dtype=any_nullable_int_dtype)

0 commit comments

Comments
 (0)