Skip to content

BUG: BusinessDay.apply_index with offset #37457

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
merged 7 commits into from
Nov 1, 2020
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.equals` and :meth:`TimedeltaIndex.equals` incorrectly considering ``int64`` indexes as equal (:issue:`36744`)
- 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`)
- Bug in :meth:`DatetimeArray.shift` incorrectly allowing ``fill_value`` with a mismatched timezone (:issue:`37299`)
- Bug in adding a :class:`BusinessDay` with nonzero ``offset`` to a non-scalar other (:issue:`37457`)

Timedelta
^^^^^^^^^
Expand Down
8 changes: 6 additions & 2 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,11 @@ cdef class BusinessDay(BusinessMixin):
@apply_array_wraps
def _apply_array(self, dtarr):
i8other = dtarr.view("i8")
return shift_bdays(i8other, self.n)
res = _shift_bdays(i8other, self.n)
if self.offset:
res = res.view("M8[ns]") + Timedelta(self.offset)
res = res.view("i8")
return res

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


cdef ndarray[int64_t] shift_bdays(const int64_t[:] i8other, int periods):
cdef ndarray[int64_t] _shift_bdays(const int64_t[:] i8other, int periods):
"""
Implementation of BusinessDay.apply_offset.

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ def test_with_offset(self):

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

def test_with_offset_index(self):
dti = DatetimeIndex([self.d])
result = dti + (self.offset + timedelta(hours=2))

expected = DatetimeIndex([datetime(2008, 1, 2, 2)])
tm.assert_index_equal(result, expected)

def test_eq(self):
assert self.offset2 == self.offset2

Expand Down Expand Up @@ -2642,6 +2649,13 @@ def test_with_offset(self):

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

def test_with_offset_index(self):
dti = DatetimeIndex([self.d])
result = dti + (self.offset + timedelta(hours=2))

expected = DatetimeIndex([datetime(2008, 1, 2, 2)])
tm.assert_index_equal(result, expected)

def test_eq(self):
assert self.offset2 == self.offset2

Expand Down