Skip to content

Commit 34925b3

Browse files
authored
BUG: BusinessDay.apply_index with offset (pandas-dev#37457)
* BUG: BusinessDay.apply_index with offset * whatsnew
1 parent 8384eaf commit 34925b3

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ Datetimelike
390390
- Bug in :meth:`DatetimeIndex.equals` and :meth:`TimedeltaIndex.equals` incorrectly considering ``int64`` indexes as equal (:issue:`36744`)
391391
- Bug in :meth:`TimedeltaIndex.sum` and :meth:`Series.sum` with ``timedelta64`` dtype on an empty index or series returning ``NaT`` instead of ``Timedelta(0)`` (:issue:`31751`)
392392
- Bug in :meth:`DatetimeArray.shift` incorrectly allowing ``fill_value`` with a mismatched timezone (:issue:`37299`)
393+
- Bug in adding a :class:`BusinessDay` with nonzero ``offset`` to a non-scalar other (:issue:`37457`)
393394

394395
Timedelta
395396
^^^^^^^^^

pandas/_libs/tslibs/offsets.pyx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,11 @@ cdef class BusinessDay(BusinessMixin):
13881388
@apply_array_wraps
13891389
def _apply_array(self, dtarr):
13901390
i8other = dtarr.view("i8")
1391-
return shift_bdays(i8other, self.n)
1391+
res = _shift_bdays(i8other, self.n)
1392+
if self.offset:
1393+
res = res.view("M8[ns]") + Timedelta(self.offset)
1394+
res = res.view("i8")
1395+
return res
13921396

13931397
def is_on_offset(self, dt: datetime) -> bool:
13941398
if self.normalize and not _is_normalized(dt):
@@ -3778,7 +3782,7 @@ cdef inline void _shift_quarters(const int64_t[:] dtindex,
37783782
out[i] = dtstruct_to_dt64(&dts)
37793783

37803784

3781-
cdef ndarray[int64_t] shift_bdays(const int64_t[:] i8other, int periods):
3785+
cdef ndarray[int64_t] _shift_bdays(const int64_t[:] i8other, int periods):
37823786
"""
37833787
Implementation of BusinessDay.apply_offset.
37843788

pandas/tests/tseries/offsets/test_offsets.py

+14
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,13 @@ def test_with_offset(self):
750750

751751
assert (self.d + offset) == datetime(2008, 1, 2, 2)
752752

753+
def test_with_offset_index(self):
754+
dti = DatetimeIndex([self.d])
755+
result = dti + (self.offset + timedelta(hours=2))
756+
757+
expected = DatetimeIndex([datetime(2008, 1, 2, 2)])
758+
tm.assert_index_equal(result, expected)
759+
753760
def test_eq(self):
754761
assert self.offset2 == self.offset2
755762

@@ -2642,6 +2649,13 @@ def test_with_offset(self):
26422649

26432650
assert (self.d + offset) == datetime(2008, 1, 2, 2)
26442651

2652+
def test_with_offset_index(self):
2653+
dti = DatetimeIndex([self.d])
2654+
result = dti + (self.offset + timedelta(hours=2))
2655+
2656+
expected = DatetimeIndex([datetime(2008, 1, 2, 2)])
2657+
tm.assert_index_equal(result, expected)
2658+
26452659
def test_eq(self):
26462660
assert self.offset2 == self.offset2
26472661

0 commit comments

Comments
 (0)