Skip to content

Commit 81b18ca

Browse files
committed
Fixed IntegerArray division by 0
When dividing by 0, the result should be `inf`, not `NaN`. Closes #27398
1 parent ad0539b commit 81b18ca

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

doc/source/whatsnew/v1.0.0.rst

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

861862

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

+11
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,13 @@ 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+
def test_divide_by_zero(self):
343+
# https://github.com/pandas-dev/pandas/issues/27398
344+
a = pd.array([0, 1, -1, None], dtype="Int64")
345+
result = a / 0
346+
expected = np.array([np.nan, np.inf, -np.inf, np.nan])
347+
tm.assert_numpy_array_equal(result, expected)
348+
342349
def test_pow(self):
343350
# https://github.com/pandas-dev/pandas/issues/22022
344351
a = integer_array([1, np.nan, np.nan, 1])
@@ -389,6 +396,10 @@ def test_compare_array(self, data, all_compare_operators):
389396
other = pd.Series([0] * len(data))
390397
self._compare_other(data, op_name, other)
391398

399+
def test_no_shared_mask(self, data):
400+
result = data + 1
401+
assert np.shares_memory(result._mask, data._mask) is False
402+
392403

393404
class TestCasting:
394405
@pytest.mark.parametrize("dropna", [True, False])

0 commit comments

Comments
 (0)