Skip to content

Commit c6fc051

Browse files
authored
Fix bug calculation of holidays
Closes pandas-dev#31415
1 parent b1214af commit c6fc051

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

pandas/tseries/holiday.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
from pandas.errors import PerformanceWarning
99

10-
from pandas import DateOffset, Series, Timestamp, date_range
10+
from pandas import (
11+
DateOffset, DatetimeIndex, Series, Timestamp, concat, date_range,
12+
)
1113

1214
from pandas.tseries.offsets import Day, Easter
1315

@@ -183,19 +185,19 @@ class from pandas.tseries.offsets
183185
assert days_of_week is None or type(days_of_week) == tuple
184186
self.days_of_week = days_of_week
185187

186-
def __repr__(self) -> str:
188+
def __repr__(self):
187189
info = ""
188190
if self.year is not None:
189-
info += f"year={self.year}, "
190-
info += f"month={self.month}, day={self.day}, "
191+
info += "year={year}, ".format(year=self.year)
192+
info += "month={mon}, day={day}, ".format(mon=self.month, day=self.day)
191193

192194
if self.offset is not None:
193-
info += f"offset={self.offset}"
195+
info += "offset={offset}".format(offset=self.offset)
194196

195197
if self.observance is not None:
196-
info += f"observance={self.observance}"
198+
info += "observance={obs}".format(obs=self.observance)
197199

198-
repr = f"Holiday: {self.name} ({info})"
200+
repr = "Holiday: {name} ({info})".format(name=self.name, info=info)
199201
return repr
200202

201203
def dates(self, start_date, end_date, return_name=False):
@@ -344,9 +346,9 @@ class AbstractHolidayCalendar(metaclass=HolidayCalendarMetaClass):
344346
Abstract interface to create holidays following certain rules.
345347
"""
346348

347-
rules: List[Holiday] = []
349+
rules = [] # type: List[Holiday]
348350
start_date = Timestamp(datetime(1970, 1, 1))
349-
end_date = Timestamp(datetime(2200, 12, 31))
351+
end_date = Timestamp(datetime(2030, 12, 31))
350352
_cache = None
351353

352354
def __init__(self, name=None, rules=None):
@@ -363,7 +365,7 @@ def __init__(self, name=None, rules=None):
363365
"""
364366
super().__init__()
365367
if name is None:
366-
name = type(self).__name__
368+
name = self.__class__.__name__
367369
self.name = name
368370

369371
if rules is not None:
@@ -394,7 +396,8 @@ def holidays(self, start=None, end=None, return_name=False):
394396
"""
395397
if self.rules is None:
396398
raise Exception(
397-
f"Holiday Calendar {self.name} does not have any rules specified"
399+
"Holiday Calendar {name} does not have any "
400+
"rules specified".format(name=self.name)
398401
)
399402

400403
if start is None:
@@ -406,18 +409,17 @@ def holidays(self, start=None, end=None, return_name=False):
406409
start = Timestamp(start)
407410
end = Timestamp(end)
408411

409-
holidays = None
412+
holidays = []
410413
# If we don't have a cache or the dates are outside the prior cache, we
411414
# get them again
412415
if self._cache is None or start < self._cache[0] or end > self._cache[1]:
413416
for rule in self.rules:
414-
rule_holidays = rule.dates(start, end, return_name=True)
415-
416-
if holidays is None:
417-
holidays = rule_holidays
418-
else:
419-
holidays = holidays.append(rule_holidays)
420-
417+
holidays.append(rule.dates(start, end, return_name=True))
418+
419+
if holidays:
420+
holidays = concat(holidays)
421+
else:
422+
holidays = Series(index=DatetimeIndex([]), dtype=object)
421423
self._cache = (start, end, holidays.sort_index())
422424

423425
holidays = self._cache[2]

0 commit comments

Comments
 (0)