Skip to content

Commit cef3c85

Browse files
committed
Merge pull request pandas-dev#9555 from dhirschfeld/calendar-cache
Moved caching in AbstractHolidayCalendar to the instance level
2 parents 42bb915 + b1e8d9f commit cef3c85

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,4 @@ Bug Fixes
155155

156156
- Bug where dividing a dataframe containing values of type ``Decimal`` by another ``Decimal`` would raise. (:issue:`9787`)
157157
- Bug where using DataFrames asfreq would remove the name of the index. (:issue:`9885`)
158+
- Changed caching in ``AbstractHolidayCalendar`` to be at the instance level rather than at the class level as the latter can result in unexpected behaviour. (:issue:`9552`)

pandas/tseries/holiday.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class AbstractHolidayCalendar(object):
279279
rules = []
280280
start_date = Timestamp(datetime(1970, 1, 1))
281281
end_date = Timestamp(datetime(2030, 12, 31))
282-
_holiday_cache = None
282+
_cache = None
283283

284284
def __init__(self, name=None, rules=None):
285285
"""
@@ -351,14 +351,6 @@ def holidays(self, start=None, end=None, return_name=False):
351351
else:
352352
return holidays.index
353353

354-
@property
355-
def _cache(self):
356-
return self.__class__._holiday_cache
357-
358-
@_cache.setter
359-
def _cache(self, values):
360-
self.__class__._holiday_cache = values
361-
362354
@staticmethod
363355
def merge_class(base, other):
364356
"""

pandas/tseries/tests/test_holiday.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
from datetime import datetime
33
import pandas.util.testing as tm
4+
from pandas import DatetimeIndex
45
from pandas.tseries.holiday import (
56
USFederalHolidayCalendar, USMemorialDay, USThanksgivingDay,
67
nearest_workday, next_monday_or_tuesday, next_monday,
@@ -50,6 +51,29 @@ def test_calendar(self):
5051
self.assertEqual(list(holidays_2.to_pydatetime()),
5152
self.holiday_list)
5253

54+
def test_calendar_caching(self):
55+
# Test for issue #9552
56+
57+
class TestCalendar(AbstractHolidayCalendar):
58+
def __init__(self, name=None, rules=None):
59+
super(TestCalendar, self).__init__(
60+
name=name,
61+
rules=rules
62+
)
63+
64+
jan1 = TestCalendar(rules=[Holiday('jan1', year=2015, month=1, day=1)])
65+
jan2 = TestCalendar(rules=[Holiday('jan2', year=2015, month=1, day=2)])
66+
67+
tm.assert_index_equal(
68+
jan1.holidays(),
69+
DatetimeIndex(['01-Jan-2015'])
70+
)
71+
tm.assert_index_equal(
72+
jan2.holidays(),
73+
DatetimeIndex(['02-Jan-2015'])
74+
)
75+
76+
5377
class TestHoliday(tm.TestCase):
5478

5579
def setUp(self):

0 commit comments

Comments
 (0)