Skip to content

Pandas date_range does not work when using periods and adding holiday #30593

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
tqdo opened this issue Jan 1, 2020 · 3 comments · Fixed by #30675
Closed

Pandas date_range does not work when using periods and adding holiday #30593

tqdo opened this issue Jan 1, 2020 · 3 comments · Fixed by #30675
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@tqdo
Copy link

tqdo commented Jan 1, 2020

This code works fine

pd.date_range(start='2020-11-25 10:00',periods=14,
              freq=pd.offsets.CustomBusinessHour(start='10:00'))

but if I add holidays then it produces more than 14 periods

pd.date_range(start='2020-11-25 10:00',periods=14,
              freq=pd.offsets.CustomBusinessHour(start='10:00',holidays=['2020-11-26']))

Output:

DatetimeIndex(['2020-11-25 10:00:00', '2020-11-25 11:00:00',
               '2020-11-25 12:00:00', '2020-11-25 13:00:00',
               '2020-11-25 14:00:00', '2020-11-25 15:00:00',
               '2020-11-25 16:00:00', '2020-11-27 10:00:00',
               '2020-11-27 11:00:00', '2020-11-27 12:00:00',
               '2020-11-27 13:00:00', '2020-11-27 14:00:00',
               '2020-11-27 15:00:00', '2020-11-27 16:00:00',
               '2020-11-30 10:00:00', '2020-11-30 11:00:00',
               '2020-11-30 12:00:00', '2020-11-30 13:00:00',
               '2020-11-30 14:00:00', '2020-11-30 15:00:00',
               '2020-11-30 16:00:00', '2020-12-01 10:00:00',
               '2020-12-01 11:00:00', '2020-12-01 12:00:00',
               '2020-12-01 13:00:00', '2020-12-01 14:00:00',
               '2020-12-01 15:00:00', '2020-12-01 16:00:00',
               '2020-12-02 10:00:00', '2020-12-02 11:00:00',
               '2020-12-02 12:00:00'],
              dtype='datetime64[ns]', freq='CBH')

If I replace periods with the corresponding end then everything works fine.

pd.date_range(start='2020-11-25 10:00',end='2020-11-27 16:00:00',
              freq=pd.offsets.CustomBusinessHour(start='10:00',holidays=['2020-11-26']))

I am not sure why date_range has this weird behavior. Appreciate any help.

@tqdo
Copy link
Author

tqdo commented Jan 1, 2020

This does not explain the behavior but just subseting the output gets the job done.

pd.date_range(start='2020-11-25 10:00',periods=14,
              freq=pd.offsets.CustomBusinessHour(start='10:00',holidays=['2020-11-26']))[:14]

Edit: Actually this does not work because the code below only produces 13 records, not more than 20

pd.date_range(start='2020-11-25 10:00',periods=20,
              freq=pd.offsets.CustomBusinessHour(start='10:00',holidays=['2020-11-26']))

@tqdo tqdo closed this as completed Jan 1, 2020
@tqdo tqdo reopened this Jan 1, 2020
@fujiaxiang
Copy link
Member

This is a bug in applying a custom-business-hour offset onto a datetime.

Will get this fixed.

@tqdo
Copy link
Author

tqdo commented Jan 4, 2020

Not sure whether this is related to the above bug but this one also produces wrong result

pd.Timestamp('2020-11-25 10:00')+ 7*pd.offsets.CustomBusinessHour(start='10:00',holidays=['2020-11-26'])

and

pd.Timestamp('2020-11-25 10:00')+ 8*pd.offsets.CustomBusinessHour(start='10:00',holidays=['2020-11-26'])

@jreback jreback added this to the 1.0 milestone Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants