Skip to content

Commit 29eb7a4

Browse files
author
David Hirschfeld
committed
Fix bug in calculation of holidays
Closes #31415
1 parent feee467 commit 29eb7a4

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

doc/source/whatsnew/v1.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ Other
223223
- Appending a dictionary to a :class:`DataFrame` without passing ``ignore_index=True`` will raise ``TypeError: Can only append a dict if ignore_index=True``
224224
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
225225
- Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`)
226+
- Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`)
227+
-
226228

227229
.. ---------------------------------------------------------------------------
228230

pandas/tests/tseries/holiday/test_calendar.py

+11
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ class testCalendar(AbstractHolidayCalendar):
9898
Sat_before_Labor_Day_2031 = to_datetime("2031-08-30")
9999
next_working_day = Sat_before_Labor_Day_2031 + 0 * workDay
100100
assert next_working_day == to_datetime("2031-09-02")
101+
102+
103+
def test_no_holidays_calendar():
104+
# Test for issue #31415
105+
106+
class NoHolidaysCalendar(AbstractHolidayCalendar):
107+
pass
108+
109+
cal = NoHolidaysCalendar()
110+
holidays = cal.holidays(Timestamp("01-Jan-2020"), Timestamp("01-Jan-2021"))
111+
assert holidays.empty

pandas/tseries/holiday.py

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

88
from pandas.errors import PerformanceWarning
99

10-
from pandas import DateOffset, Series, Timestamp, date_range
10+
from pandas import DateOffset, DatetimeIndex, Series, Timestamp, concat, date_range
1111

1212
from pandas.tseries.offsets import Day, Easter
1313

@@ -406,17 +406,14 @@ def holidays(self, start=None, end=None, return_name=False):
406406
start = Timestamp(start)
407407
end = Timestamp(end)
408408

409-
holidays = None
410409
# If we don't have a cache or the dates are outside the prior cache, we
411410
# get them again
412411
if self._cache is None or start < self._cache[0] or end > self._cache[1]:
413-
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)
412+
holidays = [rule.dates(start, end, return_name=True) for rule in self.rules]
413+
if holidays:
414+
holidays = concat(holidays)
415+
else:
416+
holidays = Series(index=DatetimeIndex([]), dtype=object)
420417

421418
self._cache = (start, end, holidays.sort_index())
422419

0 commit comments

Comments
 (0)