Skip to content

BUG: Addition raises TypeError if Period is on rhs #13069

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 1 commit 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Bug Fixes




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


Expand All @@ -103,3 +102,4 @@ Bug Fixes


- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)
27 changes: 17 additions & 10 deletions pandas/src/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -813,16 +813,23 @@ cdef class Period(object):
return NotImplemented

def __add__(self, other):
if isinstance(other, (timedelta, np.timedelta64,
offsets.Tick, offsets.DateOffset, Timedelta)):
return self._add_delta(other)
elif lib.is_integer(other):
if self.ordinal == tslib.iNaT:
ordinal = self.ordinal
else:
ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
else: # pragma: no cover
if isinstance(self, Period):
if isinstance(other, (timedelta, np.timedelta64,
offsets.Tick, offsets.DateOffset, Timedelta)):
return self._add_delta(other)
elif other is tslib.NaT:
return tslib.NaT
elif lib.is_integer(other):
if self.ordinal == tslib.iNaT:
ordinal = self.ordinal
else:
ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
else: # pragma: no cover
return NotImplemented
elif isinstance(other, Period):
return other + self
else:
return NotImplemented

def __sub__(self, other):
Expand Down
6 changes: 5 additions & 1 deletion pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

from pandas.tseries.tools import to_datetime, normalize_date
from pandas.core.common import ABCSeries, ABCDatetimeIndex
from pandas.core.common import ABCSeries, ABCDatetimeIndex, ABCPeriod

# import after tools, dateutil check
from dateutil.relativedelta import relativedelta, weekday
Expand Down Expand Up @@ -381,6 +381,8 @@ def __call__(self, other):
def __add__(self, other):
if isinstance(other, (ABCDatetimeIndex, ABCSeries)):
return other + self
elif isinstance(other, ABCPeriod):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we handle pd.NaT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, what about Period(...) + pd.NaT? (not a Period('NaT'))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are intended ATM... Should cover (part of) #12759 in this PR?

pd.Period('2011-01', freq='M') + pd.NaT
# NaT

pd.NaT + pd.Period('2011-01', freq='M')
# TypeError: unsupported operand type(s) for +: 'NaTType' and 'pandas._period.Period'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added explicit tests for add/radd with pd.NaT.

return other + self
try:
return self.apply(other)
except ApplyTypeError:
Expand Down Expand Up @@ -2489,6 +2491,8 @@ def __add__(self, other):
return type(self)(self.n + other.n)
else:
return _delta_to_tick(self.delta + other.delta)
elif isinstance(other, ABCPeriod):
return other + self
try:
return self.apply(other)
except ApplyTypeError:
Expand Down
Loading