Skip to content

REF: inherit BaseOffset in BusinessMixin #34181

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 2 commits into from
May 17, 2020
Merged
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
12 changes: 9 additions & 3 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -788,10 +788,13 @@ cdef class _Tick(ABCTick):
object.__setattr__(self, "n", state["n"])


class BusinessMixin:
class BusinessMixin(BaseOffset):
"""
Mixin to business types to provide related functions.
"""
def __init__(self, n=1, normalize=False, offset=timedelta(0)):
BaseOffset.__init__(self, n, normalize)
object.__setattr__(self, "_offset", offset)

@property
def offset(self):
Expand All @@ -815,7 +818,11 @@ class BusinessMixin:
class BusinessHourMixin(BusinessMixin):
_adjust_dst = False

def __init__(self, start="09:00", end="17:00", offset=timedelta(0)):
def __init__(
self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0)
):
BusinessMixin.__init__(self, n, normalize, offset)

# must be validated here to equality check
if np.ndim(start) == 0:
# i.e. not is_list_like
Expand Down Expand Up @@ -859,7 +866,6 @@ class BusinessHourMixin(BusinessMixin):

object.__setattr__(self, "start", start)
object.__setattr__(self, "end", end)
object.__setattr__(self, "_offset", offset)

def _repr_attrs(self) -> str:
out = super()._repr_attrs()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def to_offset(freq) -> Optional[DateOffset]:
)
stride = int(stride)
offset = _get_offset(name)
offset = offset * int(np.fabs(stride) * stride_sign)
offset = offset * int(np.fabs(stride) * stride_sign) # type: ignore
if delta is None:
delta = offset
else:
Expand Down
57 changes: 21 additions & 36 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def is_on_offset(self, dt):
return True


class SingleConstructorOffset(BaseOffset):
class SingleConstructorMixin:
_params = cache_readonly(BaseOffset._params.fget)
freqstr = cache_readonly(BaseOffset.freqstr.fget)

Expand All @@ -308,6 +308,10 @@ def _from_name(cls, suffix=None):
return cls()


class SingleConstructorOffset(SingleConstructorMixin, BaseOffset):
pass


class BusinessDay(BusinessMixin, SingleConstructorOffset):
"""
DateOffset subclass representing possibly n business days.
Expand All @@ -316,10 +320,6 @@ class BusinessDay(BusinessMixin, SingleConstructorOffset):
_prefix = "B"
_attributes = frozenset(["n", "normalize", "offset"])

def __init__(self, n=1, normalize=False, offset=timedelta(0)):
BaseOffset.__init__(self, n, normalize)
object.__setattr__(self, "_offset", offset)

def _offset_str(self) -> str:
def get_str(td):
off_str = ""
Expand Down Expand Up @@ -419,7 +419,15 @@ def is_on_offset(self, dt: datetime) -> bool:
return dt.weekday() < 5


class BusinessHourMixin(liboffsets.BusinessHourMixin):
class BusinessHour(SingleConstructorMixin, liboffsets.BusinessHourMixin):
"""
DateOffset subclass representing possibly n business hours.
"""

_prefix = "BH"
_anchor = 0
_attributes = frozenset(["n", "normalize", "start", "end", "offset"])

@cache_readonly
def next_bday(self):
"""
Expand Down Expand Up @@ -679,22 +687,6 @@ def _is_on_offset(self, dt):
return False


class BusinessHour(BusinessHourMixin, SingleConstructorOffset):
"""
DateOffset subclass representing possibly n business hours.
"""

_prefix = "BH"
_anchor = 0
_attributes = frozenset(["n", "normalize", "start", "end", "offset"])

def __init__(
self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0)
):
BaseOffset.__init__(self, n, normalize)
super().__init__(start=start, end=end, offset=offset)


class CustomBusinessDay(CustomMixin, BusinessDay):
"""
DateOffset subclass representing custom business days excluding holidays.
Expand Down Expand Up @@ -727,9 +719,7 @@ def __init__(
calendar=None,
offset=timedelta(0),
):
BaseOffset.__init__(self, n, normalize)
object.__setattr__(self, "_offset", offset)

BusinessDay.__init__(self, n, normalize, offset)
CustomMixin.__init__(self, weekmask, holidays, calendar)

@apply_wraps
Expand Down Expand Up @@ -772,7 +762,7 @@ def is_on_offset(self, dt: datetime) -> bool:
return np.is_busday(day64, busdaycal=self.calendar)


class CustomBusinessHour(CustomMixin, BusinessHourMixin, SingleConstructorOffset):
class CustomBusinessHour(CustomMixin, BusinessHour):
"""
DateOffset subclass representing possibly n custom business days.
"""
Expand All @@ -794,11 +784,8 @@ def __init__(
end="17:00",
offset=timedelta(0),
):
BaseOffset.__init__(self, n, normalize)
object.__setattr__(self, "_offset", offset)

BusinessHour.__init__(self, n, normalize, start=start, end=end, offset=offset)
CustomMixin.__init__(self, weekmask, holidays, calendar)
BusinessHourMixin.__init__(self, start=start, end=end, offset=offset)


# ---------------------------------------------------------------------
Expand Down Expand Up @@ -898,9 +885,7 @@ def __init__(
calendar=None,
offset=timedelta(0),
):
BaseOffset.__init__(self, n, normalize)
object.__setattr__(self, "_offset", offset)

BusinessMixin.__init__(self, n, normalize, offset)
CustomMixin.__init__(self, weekmask, holidays, calendar)

@cache_readonly
Expand Down Expand Up @@ -980,9 +965,9 @@ def __init__(self, n=1, normalize=False, day_of_month=None):
BaseOffset.__init__(self, n, normalize)

if day_of_month is None:
object.__setattr__(self, "day_of_month", self._default_day_of_month)
else:
object.__setattr__(self, "day_of_month", int(day_of_month))
day_of_month = self._default_day_of_month

object.__setattr__(self, "day_of_month", int(day_of_month))
if not self._min_day_of_month <= self.day_of_month <= 27:
raise ValueError(
"day_of_month must be "
Expand Down