Skip to content

REGR: Fix IntegerArray unary ops regression #36303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 13, 2020
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :class:`IntegerArray` unary plus and minus operations raising a ``TypeError`` (:issue:`36063`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
-
Expand Down
13 changes: 13 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,19 @@ def any_nullable_int_dtype(request):
return request.param


@pytest.fixture(params=tm.SIGNED_EA_INT_DTYPES)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!

def any_signed_nullable_int_dtype(request):
"""
Parameterized fixture for any signed nullable integer dtype.

* 'Int8'
* 'Int16'
* 'Int32'
* 'Int64'
"""
return request.param


@pytest.fixture(params=tm.ALL_REAL_DTYPES)
def any_real_dtype(request):
"""
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
)
super().__init__(values, mask, copy=copy)

def __neg__(self):
return type(self)(-self._data, self._mask)

def __pos__(self):
return self

def __abs__(self):
return type(self)(np.abs(self._data), self._mask)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "IntegerArray":
return integer_array(scalars, dtype=dtype, copy=copy)
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,10 @@ def __pos__(self):
):
arr = operator.pos(values)
else:
raise TypeError(f"Unary plus expects numeric dtype, not {values.dtype}")
raise TypeError(
"Unary plus expects bool, numeric, timedelta, "
f"or object dtype, not {values.dtype}"
)
return self.__array_wrap__(arr)

def __invert__(self):
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/arrays/integer/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,41 @@ def test_reduce_to_float(op):
index=pd.Index(["a", "b"], name="A"),
)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"source, target",
[
([1, 2, 3], [-1, -2, -3]),
([1, 2, None], [-1, -2, None]),
([-1, 0, 1], [1, 0, -1]),
],
)
def test_unary_minus_nullable_int(any_signed_nullable_int_dtype, source, target):
dtype = any_signed_nullable_int_dtype
arr = pd.array(source, dtype=dtype)
result = -arr
expected = pd.array(target, dtype=dtype)
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize(
"source", [[1, 2, 3], [1, 2, None], [-1, 0, 1]],
)
def test_unary_plus_nullable_int(any_signed_nullable_int_dtype, source):
dtype = any_signed_nullable_int_dtype
expected = pd.array(source, dtype=dtype)
result = +expected
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize(
"source, target",
[([1, 2, 3], [1, 2, 3]), ([1, -2, None], [1, 2, None]), ([-1, 0, 1], [1, 0, 1])],
)
def test_abs_nullable_int(any_signed_nullable_int_dtype, source, target):
dtype = any_signed_nullable_int_dtype
s = pd.array(source, dtype=dtype)
result = abs(s)
expected = pd.array(target, dtype=dtype)
tm.assert_extension_array_equal(result, expected)
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_pos_object(self, df):
"df", [pd.DataFrame({"a": pd.to_datetime(["2017-01-22", "1970-01-01"])})]
)
def test_pos_raises(self, df):
msg = re.escape("Unary plus expects numeric dtype, not datetime64[ns]")
msg = "Unary plus expects .* dtype, not datetime64\\[ns\\]"
with pytest.raises(TypeError, match=msg):
(+df)
with pytest.raises(TypeError, match=msg):
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,44 @@ def test_invert(self):
ser = tm.makeStringSeries()
ser.name = "series"
tm.assert_series_equal(-(ser < 0), ~(ser < 0))

@pytest.mark.parametrize(
"source, target",
[
([1, 2, 3], [-1, -2, -3]),
([1, 2, None], [-1, -2, None]),
([-1, 0, 1], [1, 0, -1]),
],
)
def test_unary_minus_nullable_int(
self, any_signed_nullable_int_dtype, source, target
):
dtype = any_signed_nullable_int_dtype
s = pd.Series(source, dtype=dtype)
result = -s
expected = pd.Series(target, dtype=dtype)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"source", [[1, 2, 3], [1, 2, None], [-1, 0, 1]],
)
def test_unary_plus_nullable_int(self, any_signed_nullable_int_dtype, source):
dtype = any_signed_nullable_int_dtype
expected = pd.Series(source, dtype=dtype)
result = +expected
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"source, target",
[
([1, 2, 3], [1, 2, 3]),
([1, -2, None], [1, 2, None]),
([-1, 0, 1], [1, 0, 1]),
],
)
def test_abs_nullable_int(self, any_signed_nullable_int_dtype, source, target):
dtype = any_signed_nullable_int_dtype
s = pd.Series(source, dtype=dtype)
result = abs(s)
expected = pd.Series(target, dtype=dtype)
tm.assert_series_equal(result, expected)