Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: CustomBusinessDay not respecting calendar #60647

Open
2 of 3 tasks
benmgrant opened this issue Jan 2, 2025 · 3 comments
Open
2 of 3 tasks

BUG: CustomBusinessDay not respecting calendar #60647

benmgrant opened this issue Jan 2, 2025 · 3 comments

Comments

@benmgrant
Copy link

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

from pandas.tseries.offsets import CustomBusinessDay
import pandas_market_calendars as mcal

nyse = mcal.get_calendar('NYSE')
us_bd = CustomBusinessDay(calendar=nyse)

date_range = pd.date_range('2024-12-20', periods=10, freq=us_bd)
correct_schedule = nyse.schedule(start_date='2024-12-23', end_date='2025-01-10')

Issue Description

CustomBusinessDay is not properly showing the correct calendar holiday dates. When displaying date_range in the code above you'll see that 2024-12-25 is being included which was a market holiday and is not shown in correct_schedule in the code above.

Expected Behavior

I would expect for us_bd to respect the dates in 'correct_schedule' and not include market holidays as it has in the past. When running todays date minus 1 us_bd it is showing new years day which also should be excluded and the correct result should yield '2024-12-31'

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.10.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 207 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.0
numpy : 1.26.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : 3.0.8
pytest : 8.3.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.21.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : 2.10.1
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@benmgrant benmgrant added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 2, 2025
@benmgrant benmgrant changed the title BUG: BUG: CustomBusinessDay not respecting calendar Jan 2, 2025
@rhshadrach
Copy link
Member

rhshadrach commented Jan 2, 2025

Thanks for the report. The CustomBusinessDay takes an np.busdaycalendar, but the object your passing is not an instance of this class. It is subsequently ignored. I'm not familiar with the package, but pandas_market_calendar has a date_range function - should you not be using this instead?

On the pandas side, I think we should raise when a calendar that is not an np.busdaycalender is passed instead of silently ignoring it.

@rhshadrach rhshadrach added Frequency DateOffsets good first issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 2, 2025
@sreekailash
Copy link

sreekailash commented Jan 4, 2025

@benmgrant - This is expected behavior as @rhshadrach pointed out however there should be a message if a wrong object type is passed. The CustomBusinessDay function's calendar parameter takes the busdaycalendar object as an input.

Alternate solution : You can fix this by using the following code snippet - us_bd = nyse.holidays() directly instead of calling the CustomBusinessDay as this returns the right object that can be read by the date_range function.

Complete code:

from pandas.tseries.offsets import CustomBusinessDay
import pandas_market_calendars as mcal

nyse = mcal.get_calendar('NYSE')
us_bd = nyse.holidays()

date_range = pd.date_range('2024-12-23', periods=10, freq=us_bd)
correct_schedule = nyse.schedule(start_date='2024-12-23', end_date='2025-01-10')

Hope this helps in the mean time. I will create a PR to raise a warning message when a wrong object is passed to CustomBusinessDay

@sreekailash sreekailash removed their assignment Jan 4, 2025
@rhshadrach
Copy link
Member

I will create a PR to raise a warning message when a wrong object is passed to CustomBusinessDay

I think we should raise an error, not a warning message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants