Skip to content

Commit 49cc0c2

Browse files
jseaboldadamklein
authored andcommitted
STY: Make some comments and cleanup some logic
1 parent 1517e1b commit 49cc0c2

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

pandas/core/datetools.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,8 @@ class MonthBegin(DateOffset, CacheableOffset):
392392
def apply(self, other):
393393
n = self.n
394394

395-
if other.day > 1: #then roll forward if n<=0
396-
if n <= 0:
397-
n = n + 1
395+
if other.day > 1 and n<=0: #then roll forward if n<=0
396+
n =+ 1
398397

399398
other = other + lib.Delta(months=n, day=1)
400399
return other
@@ -439,10 +438,9 @@ def apply(self, other):
439438
wkday, _ = calendar.monthrange(other.year, other.month)
440439
firstBDay = _get_firstbday(wkday)
441440

442-
if other.day > firstBDay:
443-
if n <= 0:
444-
# as if rolled forward already
445-
n = n + 1
441+
if other.day > firstBDay and n<=0:
442+
# as if rolled forward already
443+
n += 1
446444

447445
other = other + lib.Delta(months=n)
448446
wkday, _ = calendar.monthrange(other.year, other.month)
@@ -646,12 +644,15 @@ def apply(self, other):
646644

647645
if n <= 0 and monthsSince != 0: # make sure to roll forward so negate
648646
monthsSince = monthsSince - 3
647+
648+
# roll forward if on same month later than first bday
649649
if n <= 0 and (monthsSince == 0 and other.day > firstBDay):
650650
n = n + 1
651+
# pretend to roll back if on same month but before firstbday
651652
elif n > 0 and (monthsSince == 0 and other.day < firstBDay):
652653
n = n - 1
653654

654-
655+
# get the first bday for result
655656
other = other + lib.Delta(months=3*n - monthsSince)
656657
wkday, _ = calendar.monthrange(other.year, other.month)
657658
firstBDay = _get_firstbday(wkday)
@@ -718,12 +719,14 @@ def apply(self, other):
718719
wkday, days_in_month = calendar.monthrange(other.year, other.month)
719720

720721
monthsSince = (other.month - self.startingMonth) % 3
722+
721723
if monthsSince == 3: # on an offset
722724
monthsSince = 0
723725

724726
if n <= 0 and monthsSince != 0:
725727
# make sure you roll forward, so negate
726728
monthsSince = monthsSince - 3
729+
727730
if n < 0 and (monthsSince == 0 and other.day > 1):
728731
# after start, so come back an extra period as if rolled forward
729732
n = n + 1
@@ -799,21 +802,21 @@ def apply(self, other):
799802
firstBDay = _get_firstbday(wkday)
800803

801804
years = n
802-
if n > 0:
805+
806+
807+
if n > 0: # roll back first for positive n
803808
if (other.month < self.month or
804809
(other.month == self.month and other.day < firstBDay)):
805810
years -= 1
806-
elif n <= 0:
811+
elif n <= 0: # roll forward
807812
if (other.month > self.month or
808813
(other.month == self.month and other.day > firstBDay)):
809814
years += 1
810815

816+
# set first bday for result
811817
other = other + lib.Delta(years = years)
812-
813818
wkday, days_in_month = calendar.monthrange(other.year, self.month)
814-
815819
firstBDay = _get_firstbday(wkday)
816-
817820
result = datetime(other.year, self.month, firstBDay)
818821
return result
819822

0 commit comments

Comments
 (0)