Skip to content

Commit b0693be

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 b0693be

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-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

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def _check_divmod_op(self, s, op, other, exc=None):
135135

136136
def _check_op(self, s, op_name, other, exc=None):
137137
op = self.get_op_from_name(op_name)
138+
# breakpoint()
138139
result = op(s, other)
139140

140141
# compute expected
@@ -339,6 +340,13 @@ def test_error(self, data, all_arithmetic_operators):
339340
with pytest.raises(NotImplementedError):
340341
opa(np.arange(len(s)).reshape(-1, len(s)))
341342

343+
def test_divide_by_zero(self):
344+
# https://github.com/pandas-dev/pandas/issues/27398
345+
a = pd.array([0, 1, -1, None], dtype="Int64")
346+
result = a / 0
347+
expected = np.array([np.nan, np.inf, -np.inf, np.nan])
348+
tm.assert_numpy_array_equal(result, expected)
349+
342350
def test_pow(self):
343351
# https://github.com/pandas-dev/pandas/issues/22022
344352
a = integer_array([1, np.nan, np.nan, 1])

0 commit comments

Comments
 (0)