Skip to content

Commit 5203027

Browse files
committed
ENH: adds logic to handle unary ops on Period
1 parent 63faec8 commit 5203027

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pandas/core/generic.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
is_datetime64_dtype,
2323
is_timedelta64_dtype,
2424
is_datetime64tz_dtype,
25+
is_period_arraylike,
2526
is_list_like,
2627
is_dict_like,
2728
is_re_compilable,
@@ -1031,14 +1032,30 @@ def __neg__(self):
10311032
arr = operator.inv(values)
10321033
elif (is_numeric_dtype(values) or is_timedelta64_dtype(values)):
10331034
arr = operator.neg(values)
1035+
elif is_period_arraylike(values):
1036+
# An incredibly terrible workaround that should be replaced
1037+
# with real logic for handling time period arithmetic
1038+
if isinstance(self, ABCSeries):
1039+
freq = self.dt.freq
1040+
year = min(self.dt.year)
1041+
elif isinstance(self, ABCDataFrame):
1042+
freqs = set([self[col].dt.freq for col in self])
1043+
if len(freqs) > 1:
1044+
raise("Unary negative requires Period objects have "
1045+
"same frequency, not {}".format(freqs))
1046+
else:
1047+
freq = freqs.pop()
1048+
year = min(min([self[col].dt.year for col in self]))
1049+
unit = Period(value=year, freq=freq)
1050+
arr = unit - self - 1
10341051
else:
10351052
raise TypeError("Unary negative expects numeric dtype, not {}"
10361053
.format(values.dtype))
10371054
return self.__array_wrap__(arr)
10381055

10391056
def __pos__(self):
10401057
values = com._values_from_object(self)
1041-
if is_bool_dtype(values):
1058+
if (is_bool_dtype(values) or is_period_arraylike(values)):
10421059
arr = values
10431060
elif (is_numeric_dtype(values) or is_timedelta64_dtype(values)):
10441061
arr = operator.pos(values)

0 commit comments

Comments
 (0)