Skip to content

Commit 1500336

Browse files
sinhrksjreback
authored andcommitted
BUG: Addition raises TypeError if Period is on rhs
Author: sinhrks <[email protected]> Closes pandas-dev#13069 from sinhrks/period_radd and squashes the following commits: 3935104 [sinhrks] TST: Fix assertNotIsInstance msg
1 parent 881a690 commit 1500336

File tree

4 files changed

+182
-42
lines changed

4 files changed

+182
-42
lines changed

doc/source/whatsnew/v0.18.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ Bug Fixes
115115

116116

117117

118-
119118
- Bug in ``PeriodIndex`` and ``Period`` subtraction raises ``AttributeError`` (:issue:`13071`)
120119

121120

@@ -132,3 +131,4 @@ Bug Fixes
132131

133132

134133
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
134+
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)

pandas/src/period.pyx

+17-10
Original file line numberDiff line numberDiff line change
@@ -813,16 +813,23 @@ cdef class Period(object):
813813
return NotImplemented
814814

815815
def __add__(self, other):
816-
if isinstance(other, (timedelta, np.timedelta64,
817-
offsets.Tick, offsets.DateOffset, Timedelta)):
818-
return self._add_delta(other)
819-
elif lib.is_integer(other):
820-
if self.ordinal == tslib.iNaT:
821-
ordinal = self.ordinal
822-
else:
823-
ordinal = self.ordinal + other * self.freq.n
824-
return Period(ordinal=ordinal, freq=self.freq)
825-
else: # pragma: no cover
816+
if isinstance(self, Period):
817+
if isinstance(other, (timedelta, np.timedelta64,
818+
offsets.Tick, offsets.DateOffset, Timedelta)):
819+
return self._add_delta(other)
820+
elif other is tslib.NaT:
821+
return tslib.NaT
822+
elif lib.is_integer(other):
823+
if self.ordinal == tslib.iNaT:
824+
ordinal = self.ordinal
825+
else:
826+
ordinal = self.ordinal + other * self.freq.n
827+
return Period(ordinal=ordinal, freq=self.freq)
828+
else: # pragma: no cover
829+
return NotImplemented
830+
elif isinstance(other, Period):
831+
return other + self
832+
else:
826833
return NotImplemented
827834

828835
def __sub__(self, other):

pandas/tseries/offsets.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from pandas.tseries.tools import to_datetime, normalize_date
7-
from pandas.core.common import ABCSeries, ABCDatetimeIndex
7+
from pandas.core.common import ABCSeries, ABCDatetimeIndex, ABCPeriod
88

99
# import after tools, dateutil check
1010
from dateutil.relativedelta import relativedelta, weekday
@@ -381,6 +381,8 @@ def __call__(self, other):
381381
def __add__(self, other):
382382
if isinstance(other, (ABCDatetimeIndex, ABCSeries)):
383383
return other + self
384+
elif isinstance(other, ABCPeriod):
385+
return other + self
384386
try:
385387
return self.apply(other)
386388
except ApplyTypeError:
@@ -2489,6 +2491,8 @@ def __add__(self, other):
24892491
return type(self)(self.n + other.n)
24902492
else:
24912493
return _delta_to_tick(self.delta + other.delta)
2494+
elif isinstance(other, ABCPeriod):
2495+
return other + self
24922496
try:
24932497
return self.apply(other)
24942498
except ApplyTypeError:

0 commit comments

Comments
 (0)