Skip to content

Commit

Permalink
Merge pull request #357 from jack-of-some-trades/dev
Browse files Browse the repository at this point in the history
Schedule Generation Performance Optimization
  • Loading branch information
rsheftel authored Dec 8, 2024
2 parents f73d98a + 8f120a5 commit f7c23f5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pandas_market_calendars/market_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,15 @@ def days_at_time(self, days, market_time, day_offset=0):

def _tryholidays(self, cal, s, e):
try:
return cal.holidays(s, e)
# If the Calendar is all single Observance Holidays then it is far
# more efficient to extract and return those dates
observed_dates = _all_single_observance_rules(cal)
if observed_dates is not None:
return pd.DatetimeIndex(
[date for date in observed_dates if s <= date <= e]
)
else:
return cal.holidays(s, e)
except ValueError:
return pd.DatetimeIndex([])

Expand Down Expand Up @@ -920,3 +928,14 @@ def __setitem__(self, key, value):

def __delitem__(self, key):
return self.remove_time(key)


def _is_single_observance(holiday):
"Returns the Date of the Holiday if it is only observed once, None otherwise."
return holiday.start_date if holiday.start_date == holiday.end_date else None


def _all_single_observance_rules(calendar):
"Returns a list of timestamps if the Calendar's Rules are all single observance holidays, None Otherwise"
observances = [_is_single_observance(rule) for rule in calendar.rules]
return observances if all(observances) else None

0 comments on commit f7c23f5

Please sign in to comment.