Skip to content

Commit b018715

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 6b0c7e7 commit b018715

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
@@ -1029,8 +1029,22 @@ def __neg__(self):
10291029
values = com._values_from_object(self)
10301030
if values.dtype == np.bool_:
10311031
arr = operator.inv(values)
1032-
else:
1032+
elif (is_numeric_dtype(values) or is_timedelta64_dtype(values)):
10331033
arr = operator.neg(values)
1034+
else:
1035+
raise TypeError("Unary negative expects numeric dtype, not {}"
1036+
.format(values.dtype))
1037+
return self.__array_wrap__(arr)
1038+
1039+
def __pos__(self):
1040+
values = _values_from_object(self)
1041+
if values.dtype == np.bool_:
1042+
arr = values
1043+
elif (is_numeric_dtype(values) or is_timedelta64_dtype(values)):
1044+
arr = operator.pos(values)
1045+
else:
1046+
raise TypeError("Unary plus expects numeric dtype, not {}"
1047+
.format(values.dtype))
10341048
return self.__array_wrap__(arr)
10351049

10361050
def __invert__(self):

0 commit comments

Comments
 (0)