Skip to content

Commit ae6dc80

Browse files
author
Marco Gorelli
committed
🐛 -1 to the power of pd.NA was returning -1
1 parent 46c2864 commit ae6dc80

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

doc/source/user_guide/missing_data.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ Operation Result
831831
================ ======
832832
``pd.NA ** 0`` 0
833833
``1 ** pd.NA`` 1
834-
``-1 ** pd.NA`` -1
834+
``-1 ** pd.NA`` pd.NA
835835
================ ======
836836

837837
In equality and comparison operations, ``pd.NA`` also propagates. This deviates

pandas/_libs/missing.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ class NAType(C_NAType):
417417
if other is C_NA:
418418
return NA
419419
elif isinstance(other, (numbers.Number, np.bool_)):
420-
if other == 1 or other == -1:
420+
if other == 1:
421421
return other
422422
else:
423423
return NA
424424
elif isinstance(other, np.ndarray):
425-
return np.where((other == 1) | (other == -1), other, NA)
425+
return np.where(other == 1, other, NA)
426426

427427
return NotImplemented
428428

pandas/tests/arrays/test_integer.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -363,24 +363,26 @@ def test_divide_by_zero(self, zero, negative):
363363
tm.assert_numpy_array_equal(result, expected)
364364

365365
def test_pow_scalar(self):
366-
a = pd.array([0, 1, None, 2], dtype="Int64")
366+
a = pd.array([-1, 0, 1, None, 2], dtype="Int64")
367367
result = a ** 0
368-
expected = pd.array([1, 1, 1, 1], dtype="Int64")
368+
expected = pd.array([1, 1, 1, 1, 1], dtype="Int64")
369369
tm.assert_extension_array_equal(result, expected)
370370

371371
result = a ** 1
372-
expected = pd.array([0, 1, None, 2], dtype="Int64")
372+
expected = pd.array([-1, 0, 1, None, 2], dtype="Int64")
373373
tm.assert_extension_array_equal(result, expected)
374374

375375
result = a ** pd.NA
376-
expected = pd.array([None, 1, None, None], dtype="Int64")
376+
expected = pd.array([None, None, 1, None, None], dtype="Int64")
377377
tm.assert_extension_array_equal(result, expected)
378378

379379
result = a ** np.nan
380-
expected = np.array([np.nan, 1, np.nan, np.nan], dtype="float64")
380+
expected = np.array([np.nan, np.nan, 1, np.nan, np.nan], dtype="float64")
381381
tm.assert_numpy_array_equal(result, expected)
382382

383383
# reversed
384+
a = a[1:] # Can't raise integers to negative powers.
385+
384386
result = 0 ** a
385387
expected = pd.array([1, 0, None, 0], dtype="Int64")
386388
tm.assert_extension_array_equal(result, expected)

pandas/tests/scalar/test_na_scalar.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,7 @@ def test_pow_special(value, asarray):
9696

9797

9898
@pytest.mark.parametrize(
99-
"value",
100-
[
101-
1,
102-
1.0,
103-
-1,
104-
-1.0,
105-
True,
106-
np.bool_(True),
107-
np.int_(1),
108-
np.float_(1),
109-
np.int_(-1),
110-
np.float_(-1),
111-
],
99+
"value", [1, 1.0, True, np.bool_(True), np.int_(1), np.float_(1)],
112100
)
113101
@pytest.mark.parametrize("asarray", [True, False])
114102
def test_rpow_special(value, asarray):
@@ -125,6 +113,21 @@ def test_rpow_special(value, asarray):
125113
assert result == value
126114

127115

116+
@pytest.mark.parametrize(
117+
"value", [-1, -1.0, np.int_(-1), np.float_(-1)],
118+
)
119+
@pytest.mark.parametrize("asarray", [True, False])
120+
def test_rpow_minus_one(value, asarray):
121+
if asarray:
122+
value = np.array([value])
123+
result = value ** pd.NA
124+
125+
if asarray:
126+
result = result[0]
127+
128+
assert pd.isna(result)
129+
130+
128131
def test_unary_ops():
129132
assert +NA is NA
130133
assert -NA is NA

0 commit comments

Comments
 (0)