Skip to content
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

BUG: BusinessDay addition with non-nano #55608

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,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
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ cdef class BusinessDay(BusinessMixin):
reso = get_unit_from_dtype(dtarr.dtype)
res = self._shift_bdays(i8other, reso=reso)
if self.offset:
res = res.view(dtarr.dtype) + Timedelta(self.offset)
res = res.view(dtarr.dtype) + Timedelta(self.offset).as_unit("s")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i might be missing something, but wouldn't this regress if the offset contains sub seconds?

idx = pd.date_range("2010/02/01", "2010/02/10", freq="12h", unit='ns')
off = BDay(offset=pd.Timedelta(3, unit="ns"))
t1 = idx + off

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dangit good catch, will update

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated+greenish

res = res.view("i8")
return res

Expand Down
15 changes: 11 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,12 @@ 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("unit", ["s", "ms", "us", "ns"])
def test_bday_ignores_timedeltas(self, unit):
# GH#55608
idx = date_range("2010/02/01", "2010/02/10", freq="12h", unit=unit)
off = BDay(offset=Timedelta(3, unit="h"))
t1 = idx + off

expected = DatetimeIndex(
[
Expand All @@ -1011,9 +1014,13 @@ def test_bday_ignores_timedeltas(self):
"2010-02-11 03:00:00",
],
freq=None,
)
).as_unit(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(unit)
tm.assert_index_equal(pointwise, expected)


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