Skip to content

Error in business-day logic in CustomBusinessDay? #27761

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

Closed
rhstanton opened this issue Aug 5, 2019 · 4 comments · Fixed by #27790
Closed

Error in business-day logic in CustomBusinessDay? #27761

rhstanton opened this issue Aug 5, 2019 · 4 comments · Fixed by #27790
Labels
Bug Datetime Datetime data dtype
Milestone

Comments

@rhstanton
Copy link
Contributor

Code Sample, a copy-pastable example if possible

# Your code here

# Labor Day calendar

import pandas as pd
from pandas.tseries.holiday import AbstractHolidayCalendar, USLaborDay

class testCalendar(AbstractHolidayCalendar):
    rules = [
        USLaborDay,
    ]

cal = testCalendar()
workDay = pd.offsets.CustomBusinessDay(calendar=cal)

# Print dates of Labor Day
for yr in [2021, 2032]:
    print("Labor day", yr, "is on ",  cal.holidays(pd.to_datetime(str(yr-1)+'-12-31'), 
                                                    pd.to_datetime(str(yr) + '-12-31')))

print("\n")
                                                                   
# Check next working day for Saturday before Labor Day
for dateText in ['2021-09-04', '2032-09-04']:
    date = pd.to_datetime(dateText)
    print(dateText, "is a", date.strftime('%A') + ". Roll forward to", date + 0 * workDay)

Problem description

Here's the output:

Labor day 2021 is on DatetimeIndex(['2021-09-06'], dtype='datetime64[ns]', freq='WOM-1MON')
Labor day 2032 is on DatetimeIndex(['2032-09-06'], dtype='datetime64[ns]', freq='WOM-1MON')

2021-09-04 is a Saturday. Roll forward to 2021-09-07 00:00:00
2032-09-04 is a Saturday. Roll forward to 2032-09-06 00:00:00

The calendar knows that Labor day is on Monday, Sept. 6 in both 2021 and 2032. However, when I ask to move to the next working day from Saturday, Sept. 4, I get the (correct) answer Sept.7 for 2021, but the (wrong) answer Sept 6 (which is Labor Day) for 2032.

Is this a bug, or am I doing something wrong?

Thanks.

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Darwin
OS-release: 17.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 5.0.1
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.12
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 7.6.1
sphinx: 2.1.2
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: 1.2.1
tables: 3.5.2
numexpr: 2.6.9
feather: None
matplotlib: 3.1.0
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.8
lxml.etree: 4.3.4
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.5
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jbrockmendel
Copy link
Member

tseries.holidays gets relatively little attention, so it is entirely plausible that this is a bug.

I've got a guess as to the cause, would like you to confirm. Is it the case that the behavior is correct for 2030 but broken for 2031? And correct for 1970 but broken for 1969?

@rhstanton
Copy link
Contributor Author

You're correct. Here are my results so far:

1969: Broken
1970: OK
2021: OK
2030: OK
2031: Broken
2032: Broken

@jbrockmendel
Copy link
Member

That's good news indeed. Then I'm pretty sure the issue is caued by the start_date and end_date defined on L348-349 of tseries.holiday. Extending those dates out further will probably fix this problem. That might come with a performance penalty (or else I expect the original author would have had these go out further in the first place), so it isn't obvious how far out they should be extended.

A PR for this would be welcome.

@rhstanton
Copy link
Contributor Author

Let me take a look. We need to be able to go out at least 50 years.

@gfyoung gfyoung added Bug Datetime Datetime data dtype labels Aug 6, 2019
@jreback jreback added this to the 1.0 milestone Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants