Skip to content

Commit d95f125

Browse files
authored
REF: inherit BaseOffset in BusinessMixin (#34181)
1 parent 83530bd commit d95f125

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

pandas/_libs/tslibs/offsets.pyx

+9-3
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,13 @@ cdef class _Tick(ABCTick):
852852
self.normalize = False
853853

854854

855-
class BusinessMixin:
855+
class BusinessMixin(BaseOffset):
856856
"""
857857
Mixin to business types to provide related functions.
858858
"""
859+
def __init__(self, n=1, normalize=False, offset=timedelta(0)):
860+
BaseOffset.__init__(self, n, normalize)
861+
object.__setattr__(self, "_offset", offset)
859862

860863
@property
861864
def offset(self):
@@ -879,7 +882,11 @@ class BusinessMixin:
879882
class BusinessHourMixin(BusinessMixin):
880883
_adjust_dst = False
881884

882-
def __init__(self, start="09:00", end="17:00", offset=timedelta(0)):
885+
def __init__(
886+
self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0)
887+
):
888+
BusinessMixin.__init__(self, n, normalize, offset)
889+
883890
# must be validated here to equality check
884891
if np.ndim(start) == 0:
885892
# i.e. not is_list_like
@@ -923,7 +930,6 @@ class BusinessHourMixin(BusinessMixin):
923930

924931
object.__setattr__(self, "start", start)
925932
object.__setattr__(self, "end", end)
926-
object.__setattr__(self, "_offset", offset)
927933

928934
def _repr_attrs(self) -> str:
929935
out = super()._repr_attrs()

pandas/tseries/offsets.py

+16-35
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ class BusinessDay(BusinessMixin, SingleConstructorOffset):
320320
_prefix = "B"
321321
_attributes = frozenset(["n", "normalize", "offset"])
322322

323-
def __init__(self, n=1, normalize=False, offset=timedelta(0)):
324-
BaseOffset.__init__(self, n, normalize)
325-
object.__setattr__(self, "_offset", offset)
326-
327323
def _offset_str(self) -> str:
328324
def get_str(td):
329325
off_str = ""
@@ -423,7 +419,15 @@ def is_on_offset(self, dt: datetime) -> bool:
423419
return dt.weekday() < 5
424420

425421

426-
class BusinessHourMixin(liboffsets.BusinessHourMixin):
422+
class BusinessHour(SingleConstructorMixin, liboffsets.BusinessHourMixin):
423+
"""
424+
DateOffset subclass representing possibly n business hours.
425+
"""
426+
427+
_prefix = "BH"
428+
_anchor = 0
429+
_attributes = frozenset(["n", "normalize", "start", "end", "offset"])
430+
427431
@cache_readonly
428432
def next_bday(self):
429433
"""
@@ -683,22 +687,6 @@ def _is_on_offset(self, dt):
683687
return False
684688

685689

686-
class BusinessHour(BusinessHourMixin, SingleConstructorOffset):
687-
"""
688-
DateOffset subclass representing possibly n business hours.
689-
"""
690-
691-
_prefix = "BH"
692-
_anchor = 0
693-
_attributes = frozenset(["n", "normalize", "start", "end", "offset"])
694-
695-
def __init__(
696-
self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0)
697-
):
698-
BaseOffset.__init__(self, n, normalize)
699-
super().__init__(start=start, end=end, offset=offset)
700-
701-
702690
class CustomBusinessDay(CustomMixin, BusinessDay):
703691
"""
704692
DateOffset subclass representing custom business days excluding holidays.
@@ -731,9 +719,7 @@ def __init__(
731719
calendar=None,
732720
offset=timedelta(0),
733721
):
734-
BaseOffset.__init__(self, n, normalize)
735-
object.__setattr__(self, "_offset", offset)
736-
722+
BusinessDay.__init__(self, n, normalize, offset)
737723
CustomMixin.__init__(self, weekmask, holidays, calendar)
738724

739725
@apply_wraps
@@ -776,7 +762,7 @@ def is_on_offset(self, dt: datetime) -> bool:
776762
return np.is_busday(day64, busdaycal=self.calendar)
777763

778764

779-
class CustomBusinessHour(CustomMixin, BusinessHourMixin, SingleConstructorOffset):
765+
class CustomBusinessHour(CustomMixin, BusinessHour):
780766
"""
781767
DateOffset subclass representing possibly n custom business days.
782768
"""
@@ -798,11 +784,8 @@ def __init__(
798784
end="17:00",
799785
offset=timedelta(0),
800786
):
801-
BaseOffset.__init__(self, n, normalize)
802-
object.__setattr__(self, "_offset", offset)
803-
787+
BusinessHour.__init__(self, n, normalize, start=start, end=end, offset=offset)
804788
CustomMixin.__init__(self, weekmask, holidays, calendar)
805-
BusinessHourMixin.__init__(self, start=start, end=end, offset=offset)
806789

807790

808791
# ---------------------------------------------------------------------
@@ -902,9 +885,7 @@ def __init__(
902885
calendar=None,
903886
offset=timedelta(0),
904887
):
905-
BaseOffset.__init__(self, n, normalize)
906-
object.__setattr__(self, "_offset", offset)
907-
888+
BusinessMixin.__init__(self, n, normalize, offset)
908889
CustomMixin.__init__(self, weekmask, holidays, calendar)
909890

910891
@cache_readonly
@@ -984,9 +965,9 @@ def __init__(self, n=1, normalize=False, day_of_month=None):
984965
BaseOffset.__init__(self, n, normalize)
985966

986967
if day_of_month is None:
987-
object.__setattr__(self, "day_of_month", self._default_day_of_month)
988-
else:
989-
object.__setattr__(self, "day_of_month", int(day_of_month))
968+
day_of_month = self._default_day_of_month
969+
970+
object.__setattr__(self, "day_of_month", int(day_of_month))
990971
if not self._min_day_of_month <= self.day_of_month <= 27:
991972
raise ValueError(
992973
"day_of_month must be "

0 commit comments

Comments
 (0)