Skip to content

Commit c6d0bc0

Browse files
DavidKleindienstDavid Kleindienstmroeschke
authored
BUG: Fix creating DatetimeIndex with BusinessHour Frequency (#50530)
* Fix BusinessHour times 0 and add tests * Codestyle * Codestyle * Add test & whatsnew * Whatsnew formatting * Separate tests * Whatsnew changes * whatsnew added class reference * Update doc/source/whatsnew/v2.0.0.rst Co-authored-by: David Kleindienst <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 531ac29 commit c6d0bc0

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ Indexing
881881
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
882882
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
883883
- Bug in :meth:`DataFrame.isetitem` coercing extension array dtypes in :class:`DataFrame` to object (:issue:`49922`)
884+
- Bug in :class:`BusinessHour` would cause creation of :class:`DatetimeIndex` to fail when no opening hour was included in the index (:issue:`49835`)
884885
-
885886

886887
Missing

pandas/_libs/tslibs/offsets.pyx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1847,15 +1847,20 @@ cdef class BusinessHour(BusinessMixin):
18471847
earliest_start = self.start[0]
18481848
latest_start = self.start[-1]
18491849

1850+
if self.n == 0:
1851+
is_same_sign = sign > 0
1852+
else:
1853+
is_same_sign = self.n * sign >= 0
1854+
18501855
if not self.next_bday.is_on_offset(other):
18511856
# today is not business day
18521857
other = other + sign * self.next_bday
1853-
if self.n * sign >= 0:
1858+
if is_same_sign:
18541859
hour, minute = earliest_start.hour, earliest_start.minute
18551860
else:
18561861
hour, minute = latest_start.hour, latest_start.minute
18571862
else:
1858-
if self.n * sign >= 0:
1863+
if is_same_sign:
18591864
if latest_start < other.time():
18601865
# current time is after latest starting time in today
18611866
other = other + sign * self.next_bday

pandas/tests/tseries/offsets/test_business_hour.py

+12
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ def test_sub(self, dt, offset2, _offset):
241241

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

244+
def test_multiply_by_zero(self, dt, offset1, offset2):
245+
assert dt - 0 * offset1 == dt
246+
assert dt + 0 * offset1 == dt
247+
assert dt - 0 * offset2 == dt
248+
assert dt + 0 * offset2 == dt
249+
244250
def testRollback1(
245251
self,
246252
dt,
@@ -972,6 +978,12 @@ def test_datetimeindex(self):
972978
for idx in [idx1, idx2, idx3]:
973979
tm.assert_index_equal(idx, expected)
974980

981+
def test_short_datetimeindex_creation(self):
982+
# gh-49835
983+
idx4 = date_range(start="2014-07-01 10:00", freq="BH", periods=1)
984+
expected4 = DatetimeIndex(["2014-07-01 10:00"], freq="BH")
985+
tm.assert_index_equal(idx4, expected4)
986+
975987
def test_bday_ignores_timedeltas(self):
976988
idx = date_range("2010/02/01", "2010/02/10", freq="12H")
977989
t1 = idx + BDay(offset=Timedelta(3, unit="H"))

0 commit comments

Comments
 (0)