Skip to content

BUG: Unary pos/neg ops on IntegerArrays failing with TypeError #36081

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ def boolean_arithmetic_method(self, other):
name = f"__{op_name}__"
return set_function_name(boolean_arithmetic_method, name, cls)

def __neg__(self):
return self.__invert__()


BooleanArray._add_logical_ops()
BooleanArray._add_comparison_ops()
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ def __len__(self) -> int:
def __invert__(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
return type(self)(~self._data, self._mask)

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

def __pos__(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
return type(self)(+self._data, self._mask)

def to_numpy(
self, dtype=None, copy: bool = False, na_value: Scalar = lib.no_default
) -> np.ndarray:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/extension/base/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,15 @@ def test_invert(self, data):
result = ~s
expected = pd.Series(~data, name="name")
self.assert_series_equal(result, expected)

def test_neg(self, data):
s = pd.Series(data, name="name")
result = -s
expected = pd.Series(-data, name="name")
self.assert_series_equal(result, expected)

def test_pos(self, data):
s = pd.Series(data, name="name")
result = +s
expected = pd.Series(+data, name="name")
self.assert_series_equal(result, expected)
4 changes: 4 additions & 0 deletions pandas/tests/extension/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def _compare_other(self, s, data, op_name, other):
self.check_opname(s, op_name, other)


class TestUnaryOps(base.BaseUnaryOpsTests):
pass


class TestInterface(base.BaseInterfaceTests):
pass

Expand Down