Skip to content

Implement roll_monthday, simplify SemiMonthOffset #18762

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 19 commits into from
Dec 30, 2017
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6100c3f
implement roll_monthday, roll_qtrday, notes on possible bugs
jbrockmendel Dec 13, 2017
aac0832
simplify SemiMonth.apply
jbrockmendel Dec 13, 2017
c5bc5b2
whitespace/comment fixup
jbrockmendel Dec 13, 2017
15b7916
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 13, 2017
cb07e88
flake8 indentation cleanup
jbrockmendel Dec 13, 2017
eb5e72f
deprivatize and type roll_monthday
jbrockmendel Dec 13, 2017
ec3b24d
docstrings, de-privatize, types
jbrockmendel Dec 13, 2017
c6f025b
avoid while loop[ in BusinessDay.apply
jbrockmendel Dec 13, 2017
f5694de
docstring edits
jbrockmendel Dec 14, 2017
2566bf5
add typing
jbrockmendel Dec 14, 2017
0838271
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 14, 2017
a649238
dummy commit to force CI
jbrockmendel Dec 14, 2017
425d3b4
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 15, 2017
71f138d
Separate int and datetime roll funcs
jbrockmendel Dec 16, 2017
091acc2
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 18, 2017
365cdb8
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 24, 2017
9993a91
add tests for liboffsets funcs
jbrockmendel Dec 29, 2017
5d773a5
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 29, 2017
a5d9dee
Merge branch 'master' of https://github.com/pandas-dev/pandas into qt…
jbrockmendel Dec 29, 2017
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
Prev Previous commit
Next Next commit
avoid while loop[ in BusinessDay.apply
  • Loading branch information
jbrockmendel committed Dec 13, 2017
commit c6f025b88f48da304d9475357d00192cdc41e9c4
41 changes: 22 additions & 19 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,28 +532,31 @@ def get_str(td):
def apply(self, other):
if isinstance(other, datetime):
n = self.n
wday = other.weekday()

if n == 0 and other.weekday() > 4:
n = 1

result = other

# avoid slowness below
if abs(n) > 5:
k = n // 5
result = result + timedelta(7 * k)
if n < 0 and result.weekday() > 4:
n += 1
n -= 5 * k
if n == 0 and result.weekday() > 4:
n -= 1
# avoid slowness below by operating on weeks first
weeks = n // 5
if n <= 0 and wday > 4:
# roll forward
n += 1

while n != 0:
k = n // abs(n)
result = result + timedelta(k)
if result.weekday() < 5:
n -= k
n -= 5 * weeks

# n is always >= 0 at this point
if n == 0 and wday > 4:
# roll back
days = 4 - wday
elif wday > 4:
# roll forward
days = (7 - wday) + (n - 1)
elif wday + n <= 4:
# shift by n days without leaving the current week
days = n
else:
# shift by n days plus 2 to get past the weekend
days = n + 2

result = other + timedelta(days=7 * weeks + days)
if self.offset:
result = result + self.offset
return result
Expand Down