Skip to content

Commit

Permalink
Use cache_readonly attrs to minimize attrs set in __init__ (#17450)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Sep 17, 2017
1 parent 26b461b commit 553a829
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,14 @@ def __init__(self, n=1, normalize=False, **kwds):
self.normalize = normalize
super(BusinessHour, self).__init__(**kwds)

@cache_readonly
def next_bday(self):
# used for moving to next businessday
if self.n >= 0:
nb_offset = 1
else:
nb_offset = -1
self.next_bday = BusinessDay(n=nb_offset)
return BusinessDay(n=nb_offset)


class CustomBusinessDay(BusinessDay):
Expand Down Expand Up @@ -1562,6 +1564,7 @@ class Week(DateOffset):
Always generate specific day of week. 0 for Monday
"""
_adjust_dst = True
_inc = timedelta(weeks=1)

def __init__(self, n=1, normalize=False, **kwds):
self.n = n
Expand All @@ -1573,7 +1576,6 @@ def __init__(self, n=1, normalize=False, **kwds):
raise ValueError('Day must be 0<=day<=6, got {day}'
.format(day=self.weekday))

self._inc = timedelta(weeks=1)
self.kwds = kwds

def isAnchored(self):
Expand Down Expand Up @@ -1977,13 +1979,6 @@ class QuarterEnd(QuarterOffset):
_default_startingMonth = 3
_prefix = 'Q'

def __init__(self, n=1, normalize=False, **kwds):
self.n = n
self.normalize = normalize
self.startingMonth = kwds.get('startingMonth', 3)

self.kwds = kwds

def isAnchored(self):
return (self.n == 1 and self.startingMonth is not None)

Expand Down Expand Up @@ -2316,12 +2311,28 @@ def __init__(self, n=1, normalize=False, **kwds):
raise ValueError('{variation} is not a valid variation'
.format(variation=self.variation))

@cache_readonly
def _relativedelta_forward(self):
if self.variation == "nearest":
weekday_offset = weekday(self.weekday)
self._rd_forward = relativedelta(weekday=weekday_offset)
self._rd_backward = relativedelta(weekday=weekday_offset(-1))
return relativedelta(weekday=weekday_offset)
else:
self._offset_lwom = LastWeekOfMonth(n=1, weekday=self.weekday)
return None

@cache_readonly
def _relativedelta_backward(self):
if self.variation == "nearest":
weekday_offset = weekday(self.weekday)
return relativedelta(weekday=weekday_offset(-1))
else:
return None

@cache_readonly
def _offset_lwom(self):
if self.variation == "nearest":
return None
else:
return LastWeekOfMonth(n=1, weekday=self.weekday)

def isAnchored(self):
return self.n == 1 \
Expand Down Expand Up @@ -2425,8 +2436,8 @@ def _get_year_end_nearest(self, dt):
if target_date.weekday() == self.weekday:
return target_date
else:
forward = target_date + self._rd_forward
backward = target_date + self._rd_backward
forward = target_date + self._relativedelta_forward
backward = target_date + self._relativedelta_backward

if forward - target_date < target_date - backward:
return forward
Expand Down Expand Up @@ -2542,7 +2553,10 @@ def __init__(self, n=1, normalize=False, **kwds):
if self.n == 0:
raise ValueError('N cannot be 0')

self._offset = FY5253(
@cache_readonly
def _offset(self):
kwds = self.kwds
return FY5253(
startingMonth=kwds['startingMonth'],
weekday=kwds["weekday"],
variation=kwds["variation"])
Expand Down Expand Up @@ -2652,9 +2666,6 @@ class Easter(DateOffset):
"""
_adjust_dst = True

def __init__(self, n=1, **kwds):
super(Easter, self).__init__(n, **kwds)

@apply_wraps
def apply(self, other):
currentEaster = easter(other.year)
Expand Down

0 comments on commit 553a829

Please sign in to comment.