Skip to content

Simplify algebra in Week and SemiMonth offsets #18278

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 1 commit into from
Nov 15, 2017
Merged
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
80 changes: 21 additions & 59 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,10 +1093,7 @@ def _apply(self, n, other):
n -= 1
elif other.day > self.day_of_month:
other = other.replace(day=self.day_of_month)
if n == 0:
n = 1
else:
n += 1
n += 1

months = n // 2
day = 31 if n % 2 else self.day_of_month
Expand Down Expand Up @@ -1146,15 +1143,10 @@ def _apply(self, n, other):
# if other.day is not day_of_month move to day_of_month and update n
if other.day < self.day_of_month:
other = other.replace(day=self.day_of_month)
if n == 0:
n = -1
else:
n -= 1
n -= 1
elif other.day > self.day_of_month:
other = other.replace(day=self.day_of_month)
if n == 0:
n = 1
elif n < 0:
if n <= 0:
n += 1

months = n // 2 + n % 2
Expand Down Expand Up @@ -1414,29 +1406,17 @@ def isAnchored(self):

@apply_wraps
def apply(self, other):
base = other
if self.weekday is None:
return other + self.n * self._inc

if self.n > 0:
k = self.n
otherDay = other.weekday()
if otherDay != self.weekday:
other = other + timedelta((self.weekday - otherDay) % 7)
k = k - 1
for i in range(k):
other = other + self._inc
else:
k = self.n
otherDay = other.weekday()
if otherDay != self.weekday:
other = other + timedelta((self.weekday - otherDay) % 7)
for i in range(-k):
other = other - self._inc
k = self.n
otherDay = other.weekday()
if otherDay != self.weekday:
other = other + timedelta((self.weekday - otherDay) % 7)
if k > 0:
k -= 1

other = datetime(other.year, other.month, other.day,
base.hour, base.minute, base.second, base.microsecond)
return other
return other + timedelta(weeks=k)

@apply_index_wraps
def apply_index(self, i):
Expand Down Expand Up @@ -1511,18 +1491,11 @@ def apply(self, other):
base = other
offsetOfMonth = self.getOffsetOfMonth(other)

if offsetOfMonth > other:
if self.n > 0:
months = self.n - 1
else:
months = self.n
elif offsetOfMonth == other:
months = self.n
else:
if self.n > 0:
months = self.n
else:
months = self.n + 1
months = self.n
if months > 0 and offsetOfMonth > other:
months -= 1
elif months <= 0 and offsetOfMonth < other:
months += 1

other = self.getOffsetOfMonth(shift_month(other, months, 'start'))
other = datetime(other.year, other.month, other.day, base.hour,
Expand All @@ -1533,11 +1506,7 @@ def getOffsetOfMonth(self, dt):
w = Week(weekday=self.weekday)
d = datetime(dt.year, dt.month, 1, tzinfo=dt.tzinfo)
d = w.rollforward(d)

for i in range(self.week):
d = w.apply(d)

return d
return d + timedelta(weeks=self.week)

def onOffset(self, dt):
if self.normalize and not _is_normalized(dt):
Expand Down Expand Up @@ -1602,18 +1571,11 @@ def __init__(self, n=1, normalize=False, weekday=None):
def apply(self, other):
offsetOfMonth = self.getOffsetOfMonth(other)

if offsetOfMonth > other:
if self.n > 0:
months = self.n - 1
else:
months = self.n
elif offsetOfMonth == other:
months = self.n
else:
if self.n > 0:
months = self.n
else:
months = self.n + 1
months = self.n
if months > 0 and offsetOfMonth > other:
months -= 1
elif months <= 0 and offsetOfMonth < other:
months += 1

return self.getOffsetOfMonth(shift_month(other, months, 'start'))

Expand Down