Skip to content

Commit 6d67cf9

Browse files
fujiaxiangjreback
authored andcommitted
BUG: bug in date_range with custom business hours and given periods (#30675)
1 parent 5e488a0 commit 6d67cf9

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ Datetimelike
834834
- Bug in :meth:`Series.cummin` and :meth:`Series.cummax` with timezone-aware dtype incorrectly dropping its timezone (:issue:`15553`)
835835
- Bug in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` where inplace addition and subtraction did not actually operate inplace (:issue:`24115`)
836836
- Bug in :func:`pandas.to_datetime` when called with ``Series`` storing ``IntegerArray`` raising ``TypeError`` instead of returning ``Series`` (:issue:`30050`)
837+
- Bug in :func:`date_range` with custom business hours as ``freq`` and given number of ``periods`` (:issue:`30593`)
837838

838839
Timedelta
839840
^^^^^^^^^

pandas/tests/indexes/datetimes/test_date_range.py

+16
Original file line numberDiff line numberDiff line change
@@ -945,3 +945,19 @@ def test_range_with_millisecond_resolution(self, start_end):
945945
result = pd.date_range(start=start, end=end, periods=2, closed="left")
946946
expected = DatetimeIndex([start])
947947
tm.assert_index_equal(result, expected)
948+
949+
950+
def test_date_range_with_custom_holidays():
951+
# GH 30593
952+
freq = pd.offsets.CustomBusinessHour(start="15:00", holidays=["2020-11-26"])
953+
result = pd.date_range(start="2020-11-25 15:00", periods=4, freq=freq)
954+
expected = pd.DatetimeIndex(
955+
[
956+
"2020-11-25 15:00:00",
957+
"2020-11-25 16:00:00",
958+
"2020-11-27 15:00:00",
959+
"2020-11-27 16:00:00",
960+
],
961+
freq=freq,
962+
)
963+
tm.assert_index_equal(result, expected)

pandas/tseries/offsets.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,15 @@ def apply(self, other):
896896

897897
# adjust by business days first
898898
if bd != 0:
899-
skip_bd = BusinessDay(n=bd)
899+
if isinstance(self, _CustomMixin): # GH 30593
900+
skip_bd = CustomBusinessDay(
901+
n=bd,
902+
weekmask=self.weekmask,
903+
holidays=self.holidays,
904+
calendar=self.calendar,
905+
)
906+
else:
907+
skip_bd = BusinessDay(n=bd)
900908
# midnight business hour may not on BusinessDay
901909
if not self.next_bday.is_on_offset(other):
902910
prev_open = self._prev_opening_time(other)

0 commit comments

Comments
 (0)