Skip to content

Commit

Permalink
BUG: BusinessDay addition with non-nano (#55608)
Browse files Browse the repository at this point in the history
* BUG: BusinessDay addition with non-nano

* GH ref

* fix nanos case
  • Loading branch information
jbrockmendel authored Oct 25, 2023
1 parent d6aea32 commit 455b868
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.union` returning object dtype for tz-aware indexes with the same timezone but different units (:issue:`55238`)
- Bug in :meth:`Tick.delta` with very large ticks raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
- Bug in adding or subtracting a :class:`Week` offset to a ``datetime64`` :class:`Series`, :class:`Index`, or :class:`DataFrame` column with non-nanosecond resolution returning incorrect results (:issue:`55583`)
- Bug in addition or subtraction of :class:`BusinessDay` offset with ``offset`` attribute to non-nanosecond :class:`Index`, :class:`Series`, or :class:`DataFrame` column giving incorrect results (:issue:`55608`)
- Bug in addition or subtraction of :class:`DateOffset` objects with microsecond components to ``datetime64`` :class:`Index`, :class:`Series`, or :class:`DataFrame` columns with non-nanosecond resolution (:issue:`55595`)
- Bug in addition or subtraction of very large :class:`Tick` objects with :class:`Timestamp` or :class:`Timedelta` objects raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
-
Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,6 @@ cdef class BusinessDay(BusinessMixin):
res = self._shift_bdays(i8other, reso=reso)
if self.offset:
res = res.view(dtarr.dtype) + Timedelta(self.offset)
res = res.view("i8")
return res

def is_on_offset(self, dt: datetime) -> bool:
Expand Down
29 changes: 25 additions & 4 deletions pandas/tests/tseries/offsets/test_business_hour.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,9 +984,17 @@ def test_short_datetimeindex_creation(self):
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"))
@pytest.mark.parametrize("td_unit", ["s", "ms", "us", "ns"])
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
def test_bday_ignores_timedeltas(self, unit, td_unit):
# GH#55608
idx = date_range("2010/02/01", "2010/02/10", freq="12h", unit=unit)
td = Timedelta(3, unit="h").as_unit(td_unit)
off = BDay(offset=td)
t1 = idx + off

exp_reso = max(td._creso, idx._data._creso)
exp_unit = {7: "s", 8: "ms", 9: "us", 10: "ns"}[exp_reso]

expected = DatetimeIndex(
[
Expand All @@ -1011,9 +1019,22 @@ def test_bday_ignores_timedeltas(self):
"2010-02-11 03:00:00",
],
freq=None,
)
).as_unit(exp_unit)
tm.assert_index_equal(t1, expected)

# TODO(GH#55564): as_unit will be unnecessary
pointwise = DatetimeIndex([x + off for x in idx]).as_unit(exp_unit)
tm.assert_index_equal(pointwise, expected)

def test_add_bday_offset_nanos(self):
# GH#55608
idx = date_range("2010/02/01", "2010/02/10", freq="12h", unit="ns")
off = BDay(offset=Timedelta(3, unit="ns"))

result = idx + off
expected = DatetimeIndex([x + off for x in idx])
tm.assert_index_equal(result, expected)


class TestOpeningTimes:
# opening time should be affected by sign of n, not by n's value and end
Expand Down

0 comments on commit 455b868

Please sign in to comment.