Skip to content

BUG: Holiday.dates called with date range outside of Holiday's start_date/end_date raises AttributeError when Holiday has observance and days_of_week #49925

@marcoanthonyho

Description

@marcoanthonyho

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd from pandas.tseries.holiday import Holiday, next_monday hol = Holiday("Dec 25", month=12, day=25, observance=next_monday, days_of_week=(5, 6), end_date=pd.Timestamp("2013-01-01")) hol.dates(pd.Timestamp("2020-01-01"), pd.Timestamp("2023-01-01"))

Issue Description

When a Holiday has an observance, days_of_week and a start_date/end_date where the holiday applies, if you run the dates method for a date range outside of the holiday's start_date/end_date you get an error. The problem arises because self.apply_rules(dates) returns an Index instead of a DatetimeIndex when it applies the observance over dates (an empty DatetimeIndex). If self.dates_of_week is not None self.dates tries to access the dayofweek attribute on an Index object which doesn't exist.

Python 3.11.0 | packaged by conda-forge | (main, Oct 25 2022, 06:12:32) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas as pd >>> from pandas.tseries.holiday import Holiday, next_monday >>> hol = Holiday("Dec 25", month=12, day=25, observance=next_monday, days_of_week=(5, 6), end_date=pd.Timestamp("2013-01-01")) >>> hol.dates(pd.Timestamp("2020-01-01"), pd.Timestamp("2023-01-01")) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\marco\anaconda3\envs\pandas\Lib\site-packages\pandas\tseries\holiday.py", line 274, in dates np.in1d(holiday_dates.dayofweek, self.days_of_week) ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Index' object has no attribute 'dayofweek' 

Expected Behavior

It should return an empty DatetimeIndex, of the same dtype as self._reference_dates

>>>import pandas as pd >>>from pandas.tseries.holiday import Holiday, next_monday >>>hol = Holiday("Dec 25", month=12, day=25, observance=next_monday, days_of_week=(5, 6), end_date=pd.Timestamp("2013-01-01")) >>>hol.dates(pd.Timestamp("2020-01-01"), pd.Timestamp("2023-01-01")) DatetimeIndex([], dtype='datetime64[ns]', freq='<DateOffset: years=1>') 

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.11.0.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Australia.1252

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

Labels

BugNeeds TriageIssue that has not been reviewed by a pandas team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions