Skip to content

BUG: bug in date_range with custom business hours and given periods #30675

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ Datetimelike
- Bug in :meth:`Series.cummin` and :meth:`Series.cummax` with timezone-aware dtype incorrectly dropping its timezone (:issue:`15553`)
- Bug in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` where inplace addition and subtraction did not actually operate inplace (:issue:`24115`)
- Bug in :func:`pandas.to_datetime` when called with ``Series`` storing ``IntegerArray`` raising ``TypeError`` instead of returning ``Series`` (:issue:`30050`)
- Bug in :func:`date_range` with custom business hours as ``freq`` and given number of ``periods`` (:issue:`30593`)

Timedelta
^^^^^^^^^
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,19 @@ def test_range_with_millisecond_resolution(self, start_end):
result = pd.date_range(start=start, end=end, periods=2, closed="left")
expected = DatetimeIndex([start])
tm.assert_index_equal(result, expected)


def test_date_range_with_custom_holidays():
# GH 30593
freq = pd.offsets.CustomBusinessHour(start="15:00", holidays=["2020-11-26"])
result = pd.date_range(start="2020-11-25 15:00", periods=4, freq=freq)
expected = pd.DatetimeIndex(
[
"2020-11-25 15:00:00",
"2020-11-25 16:00:00",
"2020-11-27 15:00:00",
"2020-11-27 16:00:00",
],
freq=freq,
)
tm.assert_index_equal(result, expected)
10 changes: 9 additions & 1 deletion pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,15 @@ def apply(self, other):

# adjust by business days first
if bd != 0:
skip_bd = BusinessDay(n=bd)
if isinstance(self, _CustomMixin): # GH 30593
skip_bd = CustomBusinessDay(
n=bd,
weekmask=self.weekmask,
holidays=self.holidays,
calendar=self.calendar,
)
else:
skip_bd = BusinessDay(n=bd)
# midnight business hour may not on BusinessDay
if not self.next_bday.is_on_offset(other):
prev_open = self._prev_opening_time(other)
Expand Down