Skip to content

BUG: error calculating BusinessHourMixin.apply for long business hour per day #26381

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 6 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Timedelta

- Bug in :func:`TimedeltaIndex.intersection` where for non-monotonic indices in some cases an empty ``Index`` was returned when in fact an intersection existed (:issue:`25913`)
- Bug with comparisons between :class:`Timedelta` and ``NaT`` raising ``TypeError`` (:issue:`26039`)
-
- Bug in adding of business hour to the timestamp with an ending time landing in the next business time period (:pull:`26381`)
Copy link
Member

@mroeschke mroeschke May 15, 2019

Choose a reason for hiding this comment

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

Bug when adding or subtracting a :class:`BusinessHour` to a :class:`Timestamp` with the resulting time landing in a following or prior day respectively (:issue:`26381`)


Timezones
^^^^^^^^^
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,23 @@ def test_opening_time(self, case):
datetime(2014, 7, 7, 19, 30): datetime(2014, 7, 5, 4, 30),
datetime(2014, 7, 7, 19, 30, 30): datetime(2014, 7, 5, 4, 30, 30)}))

# long business hours
apply_cases.append((BusinessHour(n=4, start='00:00', end='23:00'), {
Copy link
Contributor

Choose a reason for hiding this comment

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

would love a separate PR to split up test_offsets.py a bit :->

datetime(2014, 7, 3, 22): datetime(2014, 7, 4, 3),
datetime(2014, 7, 4, 22): datetime(2014, 7, 7, 3),
datetime(2014, 7, 3, 22, 30): datetime(2014, 7, 4, 3, 30),
datetime(2014, 7, 3, 22, 20): datetime(2014, 7, 4, 3, 20),
datetime(2014, 7, 4, 22, 30, 30): datetime(2014, 7, 7, 3, 30, 30),
datetime(2014, 7, 4, 22, 30, 20): datetime(2014, 7, 7, 3, 30, 20)}))

apply_cases.append((BusinessHour(n=-4, start='00:00', end='23:00'), {
datetime(2014, 7, 4, 3): datetime(2014, 7, 3, 22),
datetime(2014, 7, 7, 3): datetime(2014, 7, 4, 22),
datetime(2014, 7, 4, 3, 30): datetime(2014, 7, 3, 22, 30),
datetime(2014, 7, 4, 3, 20): datetime(2014, 7, 3, 22, 20),
datetime(2014, 7, 7, 3, 30, 30): datetime(2014, 7, 4, 22, 30, 30),
datetime(2014, 7, 7, 3, 30, 20): datetime(2014, 7, 4, 22, 30, 20)}))

@pytest.mark.parametrize('case', apply_cases)
def test_apply(self, case):
offset, cases = case
Expand Down
17 changes: 7 additions & 10 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ def rollforward(self, dt):

@apply_wraps
def apply(self, other):
daytime = self._get_daytime_flag
businesshours = self._get_business_hours_by_sec
bhdelta = timedelta(seconds=businesshours)

Expand Down Expand Up @@ -731,21 +730,19 @@ def apply(self, other):
result = other + timedelta(hours=hours, minutes=minutes)

# because of previous adjustment, time will be larger than start
if ((daytime and (result.time() < self.start or
self.end < result.time())) or
not daytime and (self.end < result.time() < self.start)):
if n >= 0:
bday_edge = self._prev_opening_time(other)
bday_edge = bday_edge + bhdelta
# calculate remainder
if n >= 0:
bday_edge = self._prev_opening_time(other) + bhdelta
if bday_edge < result:
bday_remain = result - bday_edge
result = self._next_opening_time(other)
result += bday_remain
else:
bday_edge = self._next_opening_time(other)
else:
bday_edge = self._next_opening_time(other)
if bday_edge > result:
bday_remain = result - bday_edge
result = self._next_opening_time(result) + bhdelta
result += bday_remain

# edge handling
if n >= 0:
if result.time() == self.end:
Expand Down