Skip to content

Use cache_readonly attrs to minimize attrs set in __init__ #17450

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
Sep 17, 2017
Merged
Changes from 1 commit
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
44 changes: 28 additions & 16 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from dateutil.relativedelta import relativedelta, weekday
from dateutil.easter import easter
from pandas._libs import tslib, Timestamp, OutOfBoundsDatetime, Timedelta
from pandas.util._decorators import cache_readonly

import functools
import operator
Expand Down Expand Up @@ -938,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 @@ -1570,6 +1573,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 @@ -1581,7 +1585,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 @@ -1985,13 +1988,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 @@ -2324,12 +2320,28 @@ def __init__(self, n=1, normalize=False, **kwds):
raise ValueError('{variation} is not a valid variation'
.format(variation=self.variation))

@cache_readonly
def _rd_forward(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I would spell things out. (_relativedelta_forward)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

if self.variation == "nearest":
weekday_offset = weekday(self.weekday)
return relativedelta(weekday=weekday_offset)
else:
return None

@cache_readonly
def _rd_backward(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(-1))
else:
self._offset_lwom = LastWeekOfMonth(n=1, weekday=self.weekday)
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 @@ -2550,7 +2562,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 @@ -2660,9 +2675,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