Skip to content

Implement _get_lastbday #18234

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

Closed
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,22 @@ 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

Note: `days_in_month` argument is only included to match the signature
of _get_lastbday.
"""
first = 1
if wkday == 5: # on Saturday
Expand Down
39 changes: 19 additions & 20 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from pandas._libs.tslibs.offsets import (
ApplyTypeError,
as_datetime, _is_normalized,
_get_firstbday, _get_calendar, _to_dt64, _validate_business_time,
_get_firstbday, _get_lastbday,
_get_calendar, _to_dt64, _validate_business_time,
_int_to_weekday, _weekday_to_int,
_determine_offset,
apply_index_wraps,
Expand Down Expand Up @@ -1184,19 +1185,24 @@ class BusinessMonthEnd(MonthOffset):
def apply(self, other):
n = self.n
wkday, days_in_month = tslib.monthrange(other.year, other.month)
lastBDay = days_in_month - max(((wkday + days_in_month - 1)
% 7) - 4, 0)
last_bday = _get_lastbday(wkday, days_in_month)

if n > 0 and not other.day >= lastBDay:
if n > 0 and not other.day >= last_bday:
n = n - 1
elif n <= 0 and other.day > lastBDay:
elif n <= 0 and other.day > last_bday:
n = n + 1
other = other + relativedelta(months=n, day=31)

if other.weekday() > 4:
other = other - BDay()
return other

def onOffset(self, dt):
if self.normalize and not _is_normalized(dt):
return False
wkday, days_in_month = tslib.monthrange(dt.year, dt.month)
return dt.day == _get_lastbday(wkday, days_in_month)


class BusinessMonthBegin(MonthOffset):
"""DateOffset of one business month at beginning"""
Expand Down Expand Up @@ -1226,13 +1232,8 @@ def apply(self, other):
def onOffset(self, dt):
if self.normalize and not _is_normalized(dt):
return False
first_weekday, _ = tslib.monthrange(dt.year, dt.month)
if first_weekday == 5:
return dt.day == 3
elif first_weekday == 6:
return dt.day == 2
else:
return dt.day == 1
wkday, days_in_month = tslib.monthrange(dt.year, dt.month)
return dt.day == _get_firstbday(wkday, days_in_month)


class CustomBusinessMonthEnd(BusinessMixin, MonthOffset):
Expand Down Expand Up @@ -1704,16 +1705,15 @@ def apply(self, other):
other.microsecond)

wkday, days_in_month = tslib.monthrange(other.year, other.month)
lastBDay = days_in_month - max(((wkday + days_in_month - 1)
% 7) - 4, 0)
last_bday = _get_lastbday(wkday, days_in_month)

monthsToGo = 3 - ((other.month - self.startingMonth) % 3)
if monthsToGo == 3:
monthsToGo = 0

if n > 0 and not (other.day >= lastBDay and monthsToGo == 0):
if n > 0 and not (other.day >= last_bday and monthsToGo == 0):
n = n - 1
elif n <= 0 and other.day > lastBDay and monthsToGo == 0:
elif n <= 0 and other.day > last_bday and monthsToGo == 0:
n = n + 1

other = other + relativedelta(months=monthsToGo + 3 * n, day=31)
Expand Down Expand Up @@ -1876,17 +1876,16 @@ class BYearEnd(YearOffset):
def apply(self, other):
n = self.n
wkday, days_in_month = tslib.monthrange(other.year, self.month)
lastBDay = (days_in_month -
max(((wkday + days_in_month - 1) % 7) - 4, 0))
last_bday = _get_lastbday(wkday, days_in_month)

years = n
if n > 0:
if (other.month < self.month or
(other.month == self.month and other.day < lastBDay)):
(other.month == self.month and other.day < last_bday)):
years -= 1
elif n <= 0:
if (other.month > self.month or
(other.month == self.month and other.day > lastBDay)):
(other.month == self.month and other.day > last_bday)):
years += 1

other = other + relativedelta(years=years)
Expand Down