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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression in :class:`IntegerArray` unary plus and minus operations raising a ``TypeError`` (:issue:`36063`)

.. ---------------------------------------------------------------------------

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

def __neg__(self):
return type(self)(0 - 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
8 changes: 2 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,13 +1408,9 @@ def __neg__(self):

def __pos__(self):
values = self._values
if is_bool_dtype(values):
if is_bool_dtype(values) or is_numeric_dtype(values):
arr = values
elif (
is_numeric_dtype(values)
or is_timedelta64_dtype(values)
or is_object_dtype(values)
):
elif is_timedelta64_dtype(values) or is_object_dtype(values):
arr = operator.pos(values)
else:
raise TypeError(f"Unary plus expects numeric dtype, not {values.dtype}")
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,27 @@ def test_datetime_understood(self):
result = series - offset
expected = pd.Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"]))
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16", "Int8"])
@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, dtype, source, target):
s = pd.Series(source, dtype=dtype)
result = -s
expected = pd.Series(target, dtype=dtype)
tm.assert_series_equal(result, expected)

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