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
Show file tree
Hide file tree
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
docstrings, de-privatize, types
  • Loading branch information
jbrockmendel committed Dec 13, 2017
commit ec3b24d13b9f05465dedbcf31752dbadd040da69
40 changes: 37 additions & 3 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,23 @@ cpdef int get_day_of_month(datetime other, day_opt) except? -1:
raise ValueError(day_opt)


cpdef int roll_monthday(int n, other, compare):
cpdef int roll_monthday(other, int n, compare):
"""
Possibly increment or decrement the number of periods to shift
based on rollforward/rollbackward conventions.

Parameters
----------
other : datetime or Timestamp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs typing

n : number of periods to increment, before adjusting for rolling
day_opt : 'start', 'end', 'business_start', 'business_end'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not match the signature

The convention to use in finding the day in a given month against
which to compare for rollforward/rollbackward decisions.

Returns
-------
n : int number of periods to increment
"""
# Either `other` and `compare` are _both_ datetimes or they are both
# integers for days in the same month.

Expand All @@ -836,8 +852,25 @@ cpdef int roll_monthday(int n, other, compare):
return n


cpdef inline int roll_qtrday(other, n, month, day_opt='start',
int modby=3) except? -1:
cpdef int roll_qtrday(datetime other, int n, int month, day_opt='start',
int modby=3) except? -1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make day_opt non-optional

"""
Possibly increment or decrement the number of periods to shift
based on rollforward/rollbackward conventions.

Parameters
----------
other : datetime or Timestamp
n : number of periods to increment, before adjusting for rolling
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not match the signature

month : reference month giving the first month of the year
day_opt : 'start', 'end', 'business_start', 'business_end'
The convention to use in finding the day in a given month against
which to compare for rollforward/rollbackward decisions.

Returns
-------
n : int number of periods to increment
"""
# TODO: type `other` as datetime-or-pandas_datetimestruct?
# TODO: Merge this with roll_yearday by setting modby=12 there?
# code de-duplication versus perf hit?
Expand Down Expand Up @@ -869,6 +902,7 @@ cpdef int roll_yearday(other, n, month, day_opt='start') except? -1:
----------
other : datetime or Timestamp
n : number of periods to increment, before adjusting for rolling
month : reference month giving the first month of the year
day_opt : 'start', 'end'
'start': returns 1
'end': returns last day of the month
Expand Down
12 changes: 6 additions & 6 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def onOffset(self, dt):
@apply_wraps
def apply(self, other):
compare_day = self._get_offset_day(other)
n = liboffsets.roll_monthday(self.n, other.day, compare_day)
n = liboffsets.roll_monthday(other.day, self.n, compare_day)
return shift_month(other, n, self._day_opt)

@apply_index_wraps
Expand Down Expand Up @@ -1036,7 +1036,7 @@ def apply(self, other):

# Find this custom month offset
compare_day = self.cbday.rollback(cur_mend)
n = liboffsets.roll_monthday(self.n, other, compare_day)
n = liboffsets.roll_monthday(other, self.n, compare_day)

new = cur_mend + n * self.m_offset
result = self.cbday.rollback(new)
Expand All @@ -1054,7 +1054,7 @@ def apply(self, other):

# Find this custom month offset
compare_day = self.cbday.rollforward(cur_mbegin)
n = liboffsets.roll_monthday(self.n, other, compare_day)
n = liboffsets.roll_monthday(other, self.n, compare_day)

new = cur_mbegin + n * self.m_offset
result = self.cbday.rollforward(new)
Expand Down Expand Up @@ -1095,7 +1095,7 @@ def rule_code(self):
@apply_wraps
def apply(self, other):
# shift `other` to self.day_of_month, incrementing `n` if necessary
n = liboffsets.roll_monthday(self.n, other.day, self.day_of_month)
n = liboffsets.roll_monthday(other.day, self.n, self.day_of_month)

days_in_month = tslib.monthrange(other.year, other.month)[1]

Expand Down Expand Up @@ -1366,7 +1366,7 @@ def apply(self, other):
base = other
offsetOfMonth = self.getOffsetOfMonth(other)

months = liboffsets.roll_monthday(self.n, other, offsetOfMonth)
months = liboffsets.roll_monthday(other, self.n, offsetOfMonth)

other = self.getOffsetOfMonth(shift_month(other, months, 'start'))
other = datetime(other.year, other.month, other.day, base.hour,
Expand Down Expand Up @@ -1444,7 +1444,7 @@ def __init__(self, n=1, normalize=False, weekday=None):
def apply(self, other):
offsetOfMonth = self.getOffsetOfMonth(other)

months = liboffsets.roll_monthday(self.n, other, offsetOfMonth)
months = liboffsets.roll_monthday(other, self.n, offsetOfMonth)

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

Expand Down