Skip to content

BUG: Fix creating DatetimeIndex with BusinessHour Frequency #50530

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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ Indexing
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
- Bug in :meth:`DataFrame.isetitem` coercing extension array dtypes in :class:`DataFrame` to object (:issue:`49922`)
- Bug in :class:`BusinessHour` would cause creation of :class:`DatetimeIndex` to fail when no opening hour was included in the index (:issue:`49835`)
-

Missing
Expand Down
9 changes: 7 additions & 2 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1847,15 +1847,20 @@ cdef class BusinessHour(BusinessMixin):
earliest_start = self.start[0]
latest_start = self.start[-1]

if self.n == 0:
is_same_sign = sign > 0
else:
is_same_sign = self.n * sign >= 0

if not self.next_bday.is_on_offset(other):
# today is not business day
other = other + sign * self.next_bday
if self.n * sign >= 0:
if is_same_sign:
hour, minute = earliest_start.hour, earliest_start.minute
else:
hour, minute = latest_start.hour, latest_start.minute
else:
if self.n * sign >= 0:
if is_same_sign:
if latest_start < other.time():
# current time is after latest starting time in today
other = other + sign * self.next_bday
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/tseries/offsets/test_business_hour.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ def test_sub(self, dt, offset2, _offset):

assert dt - offset2 == dt + _offset(-3)

def test_multiply_by_zero(self, dt, offset1, offset2):
assert dt - 0 * offset1 == dt
assert dt + 0 * offset1 == dt
assert dt - 0 * offset2 == dt
assert dt + 0 * offset2 == dt

def testRollback1(
self,
dt,
Expand Down Expand Up @@ -972,6 +978,12 @@ def test_datetimeindex(self):
for idx in [idx1, idx2, idx3]:
tm.assert_index_equal(idx, expected)

def test_short_datetimeindex_creation(self):
# gh-49835
idx4 = date_range(start="2014-07-01 10:00", freq="BH", periods=1)
expected4 = DatetimeIndex(["2014-07-01 10:00"], freq="BH")
tm.assert_index_equal(idx4, expected4)

def test_bday_ignores_timedeltas(self):
idx = date_range("2010/02/01", "2010/02/10", freq="12H")
t1 = idx + BDay(offset=Timedelta(3, unit="H"))
Expand Down