-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
simplify CBMonth.apply to remove roll_monthday #19146
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
830789c
simplify CBMonth.apply to remove roll_monthday
jbrockmendel ad4a8c3
flake8 cleanup
jbrockmendel 2c79b38
requested refactor
jbrockmendel 8e7f0dc
Merge branch 'master' of https://github.com/pandas-dev/pandas into of…
jbrockmendel 4ee7c82
docstrings
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
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
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 |
---|---|---|
|
@@ -1039,55 +1039,61 @@ def __init__(self, n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', | |
_CustomMixin.__init__(self, weekmask, holidays, calendar) | ||
|
||
@cache_readonly | ||
def cbday(self): | ||
kwds = self.kwds | ||
return CustomBusinessDay(n=self.n, normalize=self.normalize, **kwds) | ||
def cbday_roll(self): | ||
cbday = CustomBusinessDay(n=self.n, normalize=False, **self.kwds) | ||
|
||
# Define default roll function to be called in apply method | ||
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. move the comment to a doc-string |
||
if self._prefix.endswith('S'): | ||
# MonthBegin | ||
roll_func = cbday.rollforward | ||
else: | ||
# MonthEnd | ||
roll_func = cbday.rollback | ||
return roll_func | ||
|
||
@cache_readonly | ||
def m_offset(self): | ||
if self._prefix.endswith('S'): | ||
# MonthBegin: | ||
return MonthBegin(n=1, normalize=self.normalize) | ||
# MonthBegin | ||
moff = MonthBegin(n=1, normalize=False) | ||
else: | ||
# MonthEnd | ||
return MonthEnd(n=1, normalize=self.normalize) | ||
moff = MonthEnd(n=1, normalize=False) | ||
return moff | ||
|
||
|
||
class CustomBusinessMonthEnd(_CustomBusinessMonth): | ||
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end') | ||
_prefix = 'CBM' | ||
@cache_readonly | ||
def month_roll(self): | ||
if self._prefix.endswith('S'): | ||
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. can you add a comment to this and cbday_roll. I like the syntax, but a future reader won't get this immediately. |
||
# MonthBegin | ||
roll_func = self.m_offset.rollback | ||
else: | ||
# MonthEnd | ||
roll_func = self.m_offset.rollforward | ||
return roll_func | ||
|
||
@apply_wraps | ||
def apply(self, other): | ||
# First move to month offset | ||
cur_mend = self.m_offset.rollforward(other) | ||
cur_month_offset_date = self.month_roll(other) | ||
|
||
# Find this custom month offset | ||
compare_date = self.cbday.rollback(cur_mend) | ||
n = liboffsets.roll_monthday(other, self.n, compare_date) | ||
compare_date = self.cbday_roll(cur_month_offset_date) | ||
n = liboffsets.roll_convention(other.day, self.n, compare_date.day) | ||
|
||
new = cur_mend + n * self.m_offset | ||
result = self.cbday.rollback(new) | ||
new = cur_month_offset_date + n * self.m_offset | ||
result = self.cbday_roll(new) | ||
return result | ||
|
||
|
||
class CustomBusinessMonthEnd(_CustomBusinessMonth): | ||
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end') | ||
_prefix = 'CBM' | ||
|
||
|
||
class CustomBusinessMonthBegin(_CustomBusinessMonth): | ||
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'beginning') | ||
_prefix = 'CBMS' | ||
|
||
@apply_wraps | ||
def apply(self, other): | ||
# First move to month offset | ||
cur_mbegin = self.m_offset.rollback(other) | ||
|
||
# Find this custom month offset | ||
compare_date = self.cbday.rollforward(cur_mbegin) | ||
n = liboffsets.roll_monthday(other, self.n, compare_date) | ||
|
||
new = cur_mbegin + n * self.m_offset | ||
result = self.cbday.rollforward(new) | ||
return result | ||
|
||
|
||
# --------------------------------------------------------------------- | ||
# Semi-Month Based Offset Classes | ||
|
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.
why not simply define
roll_func
(better name?) as a property (or function) or CBD?