Skip to content

Disable class level caching in AbstractHolidayCalendar #9552

Closed
@dhirschfeld

Description

@dhirschfeld

By caching at the class level it prevents instances from defining different rules - e.g.

import numpy as np
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday

class HolidayCalendar(AbstractHolidayCalendar):
    def __init__(self, rules):
        self.rules = list(np.atleast_1d(rules))

jan1 = HolidayCalendar(Holiday('jan1', year=2015, month=1, day=1))
jan2 = HolidayCalendar(Holiday('jan2', year=2015, month=1, day=2))


jan1.holidays()
Out[2]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-01-01]
Length: 1, Freq: None, Timezone: None

jan2.holidays()
Out[3]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-01-01]    <--------------------------- uses cached result from *other* instance!
Length: 1, Freq: None, Timezone: None
import numpy as np
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday

class HolidayCalendar(AbstractHolidayCalendar):
    def __init__(self, rules):
        self.rules = list(np.atleast_1d(rules))

jan1 = HolidayCalendar(Holiday('jan1', year=2015, month=1, day=1))
jan2 = HolidayCalendar(Holiday('jan2', year=2015, month=1, day=2))


jan2.holidays()
Out[2]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-01-02]
Length: 1, Freq: None, Timezone: None

jan1.holidays()
Out[3]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-01-02]    <--------------------------- uses cached result from *other* instance!
Length: 1, Freq: None, Timezone: None

https://github.com/pydata/pandas/blob/a72d95163b4d268012709255d7a52bbe5c1a7eb6/pandas/tseries/holiday.py#L351-L357

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions