Skip to content

Commit 9e5a69c

Browse files
committed
BUG: Fixed IntegerArray pow special cases
x^0 == 1 1^x == 1
1 parent f0264f9 commit 9e5a69c

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ Other
859859
- Fix :class:`AbstractHolidayCalendar` to return correct results for
860860
years after 2030 (now goes up to 2200) (:issue:`27790`)
861861
- Fixed :class:`IntegerArray` returning ``NA`` rather than ``inf`` for operations dividing by 0 (:issue:`27398`)
862+
- Fixed ``pow`` operations for :class:`IntegerArray` when the other value is ``0`` or ``1`` (:issue:`29997`)
862863
- Bug in :meth:`Series.count` raises if use_inf_as_na is enabled (:issue:`29478`)
863864

864865

pandas/core/arrays/integer.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,13 @@ def _create_arithmetic_method(cls, op):
718718
@unpack_zerodim_and_defer(op.__name__)
719719
def integer_arithmetic_method(self, other):
720720

721-
mask = None
721+
omask = None
722722

723723
if getattr(other, "ndim", 0) > 1:
724724
raise NotImplementedError("can only perform ops with 1-d structures")
725725

726726
if isinstance(other, IntegerArray):
727-
other, mask = other._data, other._mask
727+
other, omask = other._data, other._mask
728728

729729
elif is_list_like(other):
730730
other = np.asarray(other)
@@ -742,17 +742,28 @@ def integer_arithmetic_method(self, other):
742742
raise TypeError("can only perform ops with numeric values")
743743

744744
# nans propagate
745-
if mask is None:
745+
if omask is None:
746746
mask = self._mask.copy()
747747
else:
748-
mask = self._mask | mask
748+
mask = self._mask | omask
749749

750-
# 1 ** np.nan is 1. So we have to unmask those.
751750
if op_name == "pow":
752-
mask = np.where(self == 1, False, mask)
751+
# 1 ** x is 1.
752+
mask = np.where((self._data == 1) & ~self._mask, False, mask)
753+
# x ** 0 is 1.
754+
if omask is not None:
755+
mask = np.where((other == 0) & ~omask, False, mask)
756+
else:
757+
mask = np.where(other == 0, False, mask)
753758

754759
elif op_name == "rpow":
755-
mask = np.where(other == 1, False, mask)
760+
# 1 ** x is 1.
761+
if omask is not None:
762+
mask = np.where((other == 1) & ~omask, False, mask)
763+
else:
764+
mask = np.where(other == 1, False, mask)
765+
# x ** 0 is 1.
766+
mask = np.where((self._data == 0) & ~self._mask, False, mask)
756767

757768
with np.errstate(all="ignore"):
758769
result = op(self._data, other)

pandas/tests/arrays/test_integer.py

+40-5
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,51 @@ def test_divide_by_zero(self, zero, negative):
352352
expected = np.array(values)
353353
tm.assert_numpy_array_equal(result, expected)
354354

355-
def test_pow(self):
356-
# https://github.com/pandas-dev/pandas/issues/22022
357-
a = integer_array([1, np.nan, np.nan, 1])
358-
b = integer_array([1, np.nan, 1, np.nan])
355+
def test_pow_scalar(self):
356+
a = pd.array([0, 1, None, 2], dtype="Int64")
357+
result = a ** 0
358+
expected = pd.array([1, 1, 1, 1], dtype="Int64")
359+
tm.assert_extension_array_equal(result, expected)
360+
361+
result = a ** 1
362+
expected = pd.array([0, 1, None, 2], dtype="Int64")
363+
tm.assert_extension_array_equal(result, expected)
364+
365+
# result = a ** pd.NA
366+
# expected = pd.array([None, 1, None, None], dtype="Int64")
367+
# tm.assert_extension_array_equal(result, expected)
368+
369+
result = a ** np.nan
370+
expected = np.array([np.nan, 1, np.nan, np.nan], dtype="float64")
371+
tm.assert_numpy_array_equal(result, expected)
372+
373+
# reversed
374+
result = 0 ** a
375+
expected = pd.array([1, 0, None, 0], dtype="Int64")
376+
tm.assert_extension_array_equal(result, expected)
377+
378+
result = 1 ** a
379+
expected = pd.array([1, 1, 1, 1], dtype="Int64")
380+
tm.assert_extension_array_equal(result, expected)
381+
382+
# result = pd.NA ** a
383+
# expected = pd.array([1, None, None, None], dtype="Int64")
384+
# tm.assert_extension_array_equal(result, expected)
385+
386+
result = np.nan ** a
387+
expected = np.array([1, np.nan, np.nan, np.nan], dtype="float64")
388+
tm.assert_numpy_array_equal(result, expected)
389+
390+
def test_pow_array(self):
391+
a = integer_array([0, 0, 0, 1, 1, 1, None, None, None])
392+
b = integer_array([0, 1, None, 0, 1, None, 0, 1, None])
359393
result = a ** b
360-
expected = pd.core.arrays.integer_array([1, np.nan, np.nan, 1])
394+
expected = integer_array([1, 0, None, 1, 1, 1, 1, None, None])
361395
tm.assert_extension_array_equal(result, expected)
362396

363397
def test_rpow_one_to_na(self):
364398
# https://github.com/pandas-dev/pandas/issues/22022
399+
# https://github.com/pandas-dev/pandas/issues/29997
365400
arr = integer_array([np.nan, np.nan])
366401
result = np.array([1.0, 2.0]) ** arr
367402
expected = np.array([1.0, np.nan])

0 commit comments

Comments
 (0)