Skip to content

Commit 9a744a7

Browse files
committed
BUG: correct behavior for unary plus and negative
Adds special method for unary plus to ndframe generics, and restricts the allowed dtypes for the unary negative. Closes pandas-dev#16073.
1 parent 9872d67 commit 9a744a7

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pandas/core/generic.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1025,8 +1025,22 @@ def __neg__(self):
10251025
values = com._values_from_object(self)
10261026
if values.dtype == np.bool_:
10271027
arr = operator.inv(values)
1028-
else:
1028+
elif (is_numeric_dtype(values) or is_timedelta64_dtype(values)):
10291029
arr = operator.neg(values)
1030+
else:
1031+
raise TypeError("Unary negative expects numeric dtype, not {}"
1032+
.format(values.dtype))
1033+
return self.__array_wrap__(arr)
1034+
1035+
def __pos__(self):
1036+
values = _values_from_object(self)
1037+
if values.dtype == np.bool_:
1038+
arr = values
1039+
elif (is_numeric_dtype(values) or is_timedelta64_dtype(values)):
1040+
arr = operator.pos(values)
1041+
else:
1042+
raise TypeError("Unary plus expects numeric dtype, not {}"
1043+
.format(values.dtype))
10301044
return self.__array_wrap__(arr)
10311045

10321046
def __invert__(self):

0 commit comments

Comments
 (0)