Skip to content

BUG: NaT+Period doesnt match Period+NaT #34242

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

Merged
merged 2 commits into from
May 19, 2020
Merged
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
96 changes: 46 additions & 50 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ from pandas._libs.tslibs.ccalendar cimport (
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
from pandas._libs.tslibs.frequencies cimport (
attrname_to_abbrevs,
get_base_alias,
get_freq_code,
get_freq_str,
get_rule_month,
Expand Down Expand Up @@ -1600,9 +1599,7 @@ cdef class _Period:
raise IncompatibleFrequency("Input cannot be converted to "
f"Period(freq={self.freqstr})")
elif util.is_offset_object(other):
freqstr = other.rule_code
base = get_base_alias(freqstr)
if base == self.freq.rule_code:
if other.base == self.freq.base:
ordinal = self.ordinal + other.n
return Period(ordinal=ordinal, freq=self.freq)
msg = DIFFERENT_FREQ.format(cls=type(self).__name__,
Expand All @@ -1613,58 +1610,57 @@ cdef class _Period:
return NotImplemented

def __add__(self, other):
if is_period_object(self):
if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
util.is_offset_object(other)):
return self._add_delta(other)
elif other is NaT:
if not is_period_object(self):
# cython semantics; this is analogous to a call to __radd__
if self is NaT:
return NaT
elif util.is_integer_object(other):
ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif (PyDateTime_Check(other) or
is_period_object(other) or util.is_datetime64_object(other)):
# can't add datetime-like
# GH#17983
sname = type(self).__name__
oname = type(other).__name__
raise TypeError(f"unsupported operand type(s) for +: '{sname}' "
f"and '{oname}'")
else: # pragma: no cover
return NotImplemented
elif is_period_object(other):
# this can be reached via __radd__ because of cython rules
return other + self
else:
return NotImplemented
return other.__add__(self)

if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
util.is_offset_object(other)):
return self._add_delta(other)
elif other is NaT:
return NaT
elif util.is_integer_object(other):
ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif (PyDateTime_Check(other) or
is_period_object(other) or util.is_datetime64_object(other)):
# can't add datetime-like
# GH#17983
sname = type(self).__name__
oname = type(other).__name__
raise TypeError(f"unsupported operand type(s) for +: '{sname}' "
f"and '{oname}'")

return NotImplemented

def __sub__(self, other):
if is_period_object(self):
if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
util.is_offset_object(other)):
neg_other = -other
return self + neg_other
elif util.is_integer_object(other):
ordinal = self.ordinal - other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif is_period_object(other):
if other.freq != self.freq:
msg = DIFFERENT_FREQ.format(cls=type(self).__name__,
own_freq=self.freqstr,
other_freq=other.freqstr)
raise IncompatibleFrequency(msg)
# GH 23915 - mul by base freq since __add__ is agnostic of n
return (self.ordinal - other.ordinal) * self.freq.base
elif other is NaT:
return NaT
return NotImplemented
elif is_period_object(other):
# this can be reached via __rsub__ because of cython rules
if not is_period_object(self):
# cython semantics; this is like a call to __rsub__
if self is NaT:
return NaT
return NotImplemented
else:
return NotImplemented

elif (PyDelta_Check(other) or util.is_timedelta64_object(other) or
util.is_offset_object(other)):
neg_other = -other
return self + neg_other
elif util.is_integer_object(other):
ordinal = self.ordinal - other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif is_period_object(other):
if other.freq != self.freq:
msg = DIFFERENT_FREQ.format(cls=type(self).__name__,
own_freq=self.freqstr,
other_freq=other.freqstr)
raise IncompatibleFrequency(msg)
# GH 23915 - mul by base freq since __add__ is agnostic of n
return (self.ordinal - other.ordinal) * self.freq.base
elif other is NaT:
return NaT

return NotImplemented

def asfreq(self, freq, how='E') -> "Period":
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ def _addsub_int_array(

def _add_offset(self, other):
assert not isinstance(other, Tick)
base = libfrequencies.get_base_alias(other.rule_code)
if base != self.freq.rule_code:

if other.base != self.freq.base:
raise raise_on_incompatible(self, other)

# Note: when calling parent class's _add_timedeltalike_scalar,
Expand Down
19 changes: 16 additions & 3 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,13 @@ def test_add_invalid(self):
per1 = Period(freq="D", year=2008, month=1, day=1)
per2 = Period(freq="D", year=2008, month=1, day=2)

msg = r"unsupported operand type\(s\)"
msg = "|".join(
[
r"unsupported operand type\(s\)",
"can only concatenate str",
"must be str, not Period",
]
)
with pytest.raises(TypeError, match=msg):
per1 + "str"
with pytest.raises(TypeError, match=msg):
Expand Down Expand Up @@ -1402,8 +1408,15 @@ def test_sub_offset(self):

@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
def test_period_addsub_nat(self, freq):
assert NaT - Period("2011-01", freq=freq) is NaT
assert Period("2011-01", freq=freq) - NaT is NaT
per = Period("2011-01", freq=freq)

# For subtraction, NaT is treated as another Period object
assert NaT - per is NaT
assert per - NaT is NaT

# For addition, NaT is treated as offset-like
assert NaT + per is NaT
assert per + NaT is NaT

def test_period_ops_offset(self):
p = Period("2011-04-01", freq="D")
Expand Down