Skip to content

Commit 455b868

Browse files
authored
BUG: BusinessDay addition with non-nano (#55608)
* BUG: BusinessDay addition with non-nano * GH ref * fix nanos case
1 parent d6aea32 commit 455b868

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ Datetimelike
324324
- Bug in :meth:`DatetimeIndex.union` returning object dtype for tz-aware indexes with the same timezone but different units (:issue:`55238`)
325325
- Bug in :meth:`Tick.delta` with very large ticks raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
326326
- 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`)
327+
- 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`)
327328
- 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`)
328329
- 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`)
329330
-

pandas/_libs/tslibs/offsets.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,6 @@ cdef class BusinessDay(BusinessMixin):
18501850
res = self._shift_bdays(i8other, reso=reso)
18511851
if self.offset:
18521852
res = res.view(dtarr.dtype) + Timedelta(self.offset)
1853-
res = res.view("i8")
18541853
return res
18551854

18561855
def is_on_offset(self, dt: datetime) -> bool:

pandas/tests/tseries/offsets/test_business_hour.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,9 +984,17 @@ def test_short_datetimeindex_creation(self):
984984
expected4 = DatetimeIndex(["2014-07-01 10:00"], freq="bh")
985985
tm.assert_index_equal(idx4, expected4)
986986

987-
def test_bday_ignores_timedeltas(self):
988-
idx = date_range("2010/02/01", "2010/02/10", freq="12h")
989-
t1 = idx + BDay(offset=Timedelta(3, unit="h"))
987+
@pytest.mark.parametrize("td_unit", ["s", "ms", "us", "ns"])
988+
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
989+
def test_bday_ignores_timedeltas(self, unit, td_unit):
990+
# GH#55608
991+
idx = date_range("2010/02/01", "2010/02/10", freq="12h", unit=unit)
992+
td = Timedelta(3, unit="h").as_unit(td_unit)
993+
off = BDay(offset=td)
994+
t1 = idx + off
995+
996+
exp_reso = max(td._creso, idx._data._creso)
997+
exp_unit = {7: "s", 8: "ms", 9: "us", 10: "ns"}[exp_reso]
990998

991999
expected = DatetimeIndex(
9921000
[
@@ -1011,9 +1019,22 @@ def test_bday_ignores_timedeltas(self):
10111019
"2010-02-11 03:00:00",
10121020
],
10131021
freq=None,
1014-
)
1022+
).as_unit(exp_unit)
10151023
tm.assert_index_equal(t1, expected)
10161024

1025+
# TODO(GH#55564): as_unit will be unnecessary
1026+
pointwise = DatetimeIndex([x + off for x in idx]).as_unit(exp_unit)
1027+
tm.assert_index_equal(pointwise, expected)
1028+
1029+
def test_add_bday_offset_nanos(self):
1030+
# GH#55608
1031+
idx = date_range("2010/02/01", "2010/02/10", freq="12h", unit="ns")
1032+
off = BDay(offset=Timedelta(3, unit="ns"))
1033+
1034+
result = idx + off
1035+
expected = DatetimeIndex([x + off for x in idx])
1036+
tm.assert_index_equal(result, expected)
1037+
10171038

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

0 commit comments

Comments
 (0)