Skip to content
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

Cache holidays #117

Merged
merged 1 commit into from
Sep 3, 2020
Merged
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
13 changes: 8 additions & 5 deletions pandas_market_calendars/market_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, open_time=None, close_time=None):
"""
self._open_time = self.open_time_default if open_time is None else open_time
self._close_time = self.close_time_default if close_time is None else close_time
self._holidays = None

@classmethod
def factory(cls, name, open_time=None, close_time=None):
Expand Down Expand Up @@ -212,11 +213,13 @@ def holidays(self):

:return: CustomBusinessDay object of holidays
"""
return CustomBusinessDay(
if self._holidays is None:
self._holidays = CustomBusinessDay(
holidays=self.adhoc_holidays,
calendar=self.regular_holidays,
weekmask=self.weekmask,
)
return self._holidays

def valid_days(self, start_date, end_date, tz='UTC'):
"""
Expand All @@ -233,7 +236,7 @@ def schedule(self, start_date, end_date, tz='UTC'):
"""
Generates the schedule DataFrame. The resulting DataFrame will have all the valid business days as the index
and columns for the market opening datetime (market_open) and closing datetime (market_close). All time zones
are set to UTC by default. Setting the tz parameter will convert the columns to the desired timezone,
are set to UTC by default. Setting the tz parameter will convert the columns to the desired timezone,
such as 'America/New_York'

:param start_date: start date
Expand Down Expand Up @@ -263,18 +266,18 @@ def schedule(self, start_date, end_date, tz='UTC'):
# Overwrite the special opens and closes on top of the standard ones.
_overwrite_special_dates(_all_days, opens, _special_opens)
_overwrite_special_dates(_all_days, closes, _special_closes)

result = DataFrame(index=_all_days.tz_localize(None), columns=['market_open', 'market_close'],
data={'market_open': opens, 'market_close': closes})

if self.break_start:
result['break_start'] = days_at_time(_all_days, self.break_start, self.tz).tz_convert(tz)
temp = result[['market_open', 'break_start']].max(axis=1)
result['break_start'] = temp
result['break_end'] = days_at_time(_all_days, self.break_end, self.tz).tz_convert(tz)
temp = result[['market_close', 'break_end']].min(axis=1)
result['break_end'] = temp

return result

@staticmethod
Expand Down