-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
simplify+unify offset.apply logic #18263
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
jreback
merged 8 commits into
pandas-dev:master
from
jbrockmendel:tslibs-offsets-paramd
Nov 16, 2017
+105
−74
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c546744
implement _get_lastbday
jbrockmendel f6aebe3
implement shift_month for business days
jbrockmendel 8546bde
edits per reviewer requests
jbrockmendel 0b1ecaf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel edb370d
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel c754fc4
formalize docstrings
jbrockmendel c6a232f
add unit tests
jbrockmendel 45d9ddd
fix datetime.datetime --> datetime
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,12 +139,23 @@ def apply_index_wraps(func): | |
# --------------------------------------------------------------------- | ||
# Business Helpers | ||
|
||
cpdef int _get_firstbday(int wkday): | ||
cpdef int get_lastbday(int wkday, int days_in_month): | ||
""" | ||
wkday is the result of monthrange(year, month) | ||
(wkday, days_in_month) is the result of monthrange(year, month) | ||
""" | ||
return days_in_month - max(((wkday + days_in_month - 1) % 7) - 4, 0) | ||
|
||
|
||
cpdef int get_firstbday(int wkday, int days_in_month=0): | ||
""" | ||
(wkday, days_in_month) is the result of monthrange(year, month) | ||
|
||
If it's a saturday or sunday, increment first business day to reflect this | ||
|
||
days_in_month arg is a dummy so that this has the same signature as | ||
get_lastbday. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can type these |
||
cdef int first | ||
first = 1 | ||
if wkday == 5: # on Saturday | ||
first = 3 | ||
|
@@ -380,7 +391,6 @@ class BaseOffset(_BaseOffset): | |
# ---------------------------------------------------------------------- | ||
# RelativeDelta Arithmetic | ||
|
||
|
||
cpdef datetime shift_month(datetime stamp, int months, object day_opt=None): | ||
""" | ||
Given a datetime (or Timestamp) `stamp`, an integer `months` and an | ||
|
@@ -406,7 +416,7 @@ cpdef datetime shift_month(datetime stamp, int months, object day_opt=None): | |
""" | ||
cdef: | ||
int year, month, day | ||
int dim, dy | ||
int wkday, days_in_month, dy | ||
|
||
dy = (stamp.month + months) // 12 | ||
month = (stamp.month + months) % 12 | ||
|
@@ -416,15 +426,21 @@ cpdef datetime shift_month(datetime stamp, int months, object day_opt=None): | |
dy -= 1 | ||
year = stamp.year + dy | ||
|
||
dim = monthrange(year, month)[1] | ||
wkday, days_in_month = monthrange(year, month) | ||
if day_opt is None: | ||
day = min(stamp.day, dim) | ||
day = min(stamp.day, days_in_month) | ||
elif day_opt == 'start': | ||
day = 1 | ||
elif day_opt == 'end': | ||
day = dim | ||
day = days_in_month | ||
elif day_opt == 'business_start': | ||
# first business day of month | ||
day = get_firstbday(wkday, days_in_month) | ||
elif day_opt == 'business_end': | ||
# last business day of month | ||
day = get_lastbday(wkday, days_in_month) | ||
elif is_integer_object(day_opt): | ||
day = min(day_opt, dim) | ||
day = min(day_opt, days_in_month) | ||
else: | ||
raise ValueError(day_opt) | ||
return stamp.replace(year=year, month=month, day=day) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you update doc-strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update to reflect what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean update to a more proper doc-string