@@ -1093,10 +1093,7 @@ def _apply(self, n, other):
10931093 n -= 1
10941094 elif other .day > self .day_of_month :
10951095 other = other .replace (day = self .day_of_month )
1096- if n == 0 :
1097- n = 1
1098- else :
1099- n += 1
1096+ n += 1
11001097
11011098 months = n // 2
11021099 day = 31 if n % 2 else self .day_of_month
@@ -1146,15 +1143,10 @@ def _apply(self, n, other):
11461143 # if other.day is not day_of_month move to day_of_month and update n
11471144 if other .day < self .day_of_month :
11481145 other = other .replace (day = self .day_of_month )
1149- if n == 0 :
1150- n = - 1
1151- else :
1152- n -= 1
1146+ n -= 1
11531147 elif other .day > self .day_of_month :
11541148 other = other .replace (day = self .day_of_month )
1155- if n == 0 :
1156- n = 1
1157- elif n < 0 :
1149+ if n <= 0 :
11581150 n += 1
11591151
11601152 months = n // 2 + n % 2
@@ -1414,29 +1406,17 @@ def isAnchored(self):
14141406
14151407 @apply_wraps
14161408 def apply (self , other ):
1417- base = other
14181409 if self .weekday is None :
14191410 return other + self .n * self ._inc
14201411
1421- if self .n > 0 :
1422- k = self .n
1423- otherDay = other .weekday ()
1424- if otherDay != self .weekday :
1425- other = other + timedelta ((self .weekday - otherDay ) % 7 )
1426- k = k - 1
1427- for i in range (k ):
1428- other = other + self ._inc
1429- else :
1430- k = self .n
1431- otherDay = other .weekday ()
1432- if otherDay != self .weekday :
1433- other = other + timedelta ((self .weekday - otherDay ) % 7 )
1434- for i in range (- k ):
1435- other = other - self ._inc
1412+ k = self .n
1413+ otherDay = other .weekday ()
1414+ if otherDay != self .weekday :
1415+ other = other + timedelta ((self .weekday - otherDay ) % 7 )
1416+ if k > 0 :
1417+ k -= 1
14361418
1437- other = datetime (other .year , other .month , other .day ,
1438- base .hour , base .minute , base .second , base .microsecond )
1439- return other
1419+ return other + timedelta (weeks = k )
14401420
14411421 @apply_index_wraps
14421422 def apply_index (self , i ):
@@ -1511,18 +1491,11 @@ def apply(self, other):
15111491 base = other
15121492 offsetOfMonth = self .getOffsetOfMonth (other )
15131493
1514- if offsetOfMonth > other :
1515- if self .n > 0 :
1516- months = self .n - 1
1517- else :
1518- months = self .n
1519- elif offsetOfMonth == other :
1520- months = self .n
1521- else :
1522- if self .n > 0 :
1523- months = self .n
1524- else :
1525- months = self .n + 1
1494+ months = self .n
1495+ if months > 0 and offsetOfMonth > other :
1496+ months -= 1
1497+ elif months <= 0 and offsetOfMonth < other :
1498+ months += 1
15261499
15271500 other = self .getOffsetOfMonth (shift_month (other , months , 'start' ))
15281501 other = datetime (other .year , other .month , other .day , base .hour ,
@@ -1533,11 +1506,7 @@ def getOffsetOfMonth(self, dt):
15331506 w = Week (weekday = self .weekday )
15341507 d = datetime (dt .year , dt .month , 1 , tzinfo = dt .tzinfo )
15351508 d = w .rollforward (d )
1536-
1537- for i in range (self .week ):
1538- d = w .apply (d )
1539-
1540- return d
1509+ return d + timedelta (weeks = self .week )
15411510
15421511 def onOffset (self , dt ):
15431512 if self .normalize and not _is_normalized (dt ):
@@ -1602,18 +1571,11 @@ def __init__(self, n=1, normalize=False, weekday=None):
16021571 def apply (self , other ):
16031572 offsetOfMonth = self .getOffsetOfMonth (other )
16041573
1605- if offsetOfMonth > other :
1606- if self .n > 0 :
1607- months = self .n - 1
1608- else :
1609- months = self .n
1610- elif offsetOfMonth == other :
1611- months = self .n
1612- else :
1613- if self .n > 0 :
1614- months = self .n
1615- else :
1616- months = self .n + 1
1574+ months = self .n
1575+ if months > 0 and offsetOfMonth > other :
1576+ months -= 1
1577+ elif months <= 0 and offsetOfMonth < other :
1578+ months += 1
16171579
16181580 return self .getOffsetOfMonth (shift_month (other , months , 'start' ))
16191581
0 commit comments