@@ -52,7 +52,6 @@ from pandas._libs.tslibs.ccalendar cimport (
52
52
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
53
53
from pandas._libs.tslibs.frequencies cimport (
54
54
attrname_to_abbrevs,
55
- get_base_alias,
56
55
get_freq_code,
57
56
get_freq_str,
58
57
get_rule_month,
@@ -1600,9 +1599,7 @@ cdef class _Period:
1600
1599
raise IncompatibleFrequency(" Input cannot be converted to "
1601
1600
f" Period(freq={self.freqstr})" )
1602
1601
elif util.is_offset_object(other):
1603
- freqstr = other.rule_code
1604
- base = get_base_alias(freqstr)
1605
- if base == self .freq.rule_code:
1602
+ if other.base == self .freq.base:
1606
1603
ordinal = self .ordinal + other.n
1607
1604
return Period(ordinal = ordinal, freq = self .freq)
1608
1605
msg = DIFFERENT_FREQ.format(cls = type (self ).__name__,
@@ -1613,58 +1610,57 @@ cdef class _Period:
1613
1610
return NotImplemented
1614
1611
1615
1612
def __add__ (self , other ):
1616
- if is_period_object(self ):
1617
- if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
1618
- util.is_offset_object(other)):
1619
- return self ._add_delta(other)
1620
- elif other is NaT:
1613
+ if not is_period_object(self ):
1614
+ # cython semantics; this is analogous to a call to __radd__
1615
+ if self is NaT:
1621
1616
return NaT
1622
- elif util.is_integer_object(other):
1623
- ordinal = self .ordinal + other * self .freq.n
1624
- return Period(ordinal = ordinal, freq = self .freq)
1625
- elif (PyDateTime_Check(other) or
1626
- is_period_object(other) or util.is_datetime64_object(other)):
1627
- # can't add datetime-like
1628
- # GH#17983
1629
- sname = type (self ).__name__
1630
- oname = type (other).__name__
1631
- raise TypeError (f" unsupported operand type(s) for +: '{sname}' "
1632
- f" and '{oname}'" )
1633
- else : # pragma: no cover
1634
- return NotImplemented
1635
- elif is_period_object(other):
1636
- # this can be reached via __radd__ because of cython rules
1637
- return other + self
1638
- else :
1639
- return NotImplemented
1617
+ return other.__add__ (self )
1618
+
1619
+ if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
1620
+ util.is_offset_object(other)):
1621
+ return self ._add_delta(other)
1622
+ elif other is NaT:
1623
+ return NaT
1624
+ elif util.is_integer_object(other):
1625
+ ordinal = self .ordinal + other * self .freq.n
1626
+ return Period(ordinal = ordinal, freq = self .freq)
1627
+ elif (PyDateTime_Check(other) or
1628
+ is_period_object(other) or util.is_datetime64_object(other)):
1629
+ # can't add datetime-like
1630
+ # GH#17983
1631
+ sname = type (self ).__name__
1632
+ oname = type (other).__name__
1633
+ raise TypeError (f" unsupported operand type(s) for +: '{sname}' "
1634
+ f" and '{oname}'" )
1635
+
1636
+ return NotImplemented
1640
1637
1641
1638
def __sub__ (self , other ):
1642
- if is_period_object(self ):
1643
- if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
1644
- util.is_offset_object(other)):
1645
- neg_other = - other
1646
- return self + neg_other
1647
- elif util.is_integer_object(other):
1648
- ordinal = self .ordinal - other * self .freq.n
1649
- return Period(ordinal = ordinal, freq = self .freq)
1650
- elif is_period_object(other):
1651
- if other.freq != self .freq:
1652
- msg = DIFFERENT_FREQ.format(cls = type (self ).__name__,
1653
- own_freq = self .freqstr,
1654
- other_freq = other.freqstr)
1655
- raise IncompatibleFrequency(msg)
1656
- # GH 23915 - mul by base freq since __add__ is agnostic of n
1657
- return (self .ordinal - other.ordinal) * self .freq.base
1658
- elif other is NaT:
1659
- return NaT
1660
- return NotImplemented
1661
- elif is_period_object(other):
1662
- # this can be reached via __rsub__ because of cython rules
1639
+ if not is_period_object(self ):
1640
+ # cython semantics; this is like a call to __rsub__
1663
1641
if self is NaT:
1664
1642
return NaT
1665
1643
return NotImplemented
1666
- else :
1667
- return NotImplemented
1644
+
1645
+ elif (PyDelta_Check(other) or util.is_timedelta64_object(other) or
1646
+ util.is_offset_object(other)):
1647
+ neg_other = - other
1648
+ return self + neg_other
1649
+ elif util.is_integer_object(other):
1650
+ ordinal = self .ordinal - other * self .freq.n
1651
+ return Period(ordinal = ordinal, freq = self .freq)
1652
+ elif is_period_object(other):
1653
+ if other.freq != self .freq:
1654
+ msg = DIFFERENT_FREQ.format(cls = type (self ).__name__,
1655
+ own_freq = self .freqstr,
1656
+ other_freq = other.freqstr)
1657
+ raise IncompatibleFrequency(msg)
1658
+ # GH 23915 - mul by base freq since __add__ is agnostic of n
1659
+ return (self .ordinal - other.ordinal) * self .freq.base
1660
+ elif other is NaT:
1661
+ return NaT
1662
+
1663
+ return NotImplemented
1668
1664
1669
1665
def asfreq (self , freq , how = ' E' ) -> "Period":
1670
1666
"""
0 commit comments