Skip to content

Commit 1e2f9c7

Browse files
author
Si Wei How
committed
Edit comments and tests
1 parent 12894ec commit 1e2f9c7

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

pandas/tests/tseries/offsets/test_offsets.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -769,19 +769,31 @@ def setup_method(self, method):
769769

770770
def test_constructor_errors(self):
771771
from datetime import time as dt_time
772-
with pytest.raises(ValueError):
772+
with pytest.raises(ValueError,
773+
match='time data must be specified only with hour and minute'):
773774
BusinessHour(start=dt_time(11, 0, 5))
774-
with pytest.raises(ValueError):
775+
with pytest.raises(ValueError,
776+
match="time data must match '%H:%M' format"):
775777
BusinessHour(start='AAA')
776-
with pytest.raises(ValueError):
778+
with pytest.raises(ValueError,
779+
match="time data must match '%H:%M' format"):
777780
BusinessHour(start='14:00:05')
778-
with pytest.raises(ValueError):
781+
with pytest.raises(ValueError,
782+
match='number of starting time cannot be 0'):
779783
BusinessHour(start=[])
780-
with pytest.raises(ValueError):
784+
with pytest.raises(ValueError,
785+
match='number of ending time cannot be 0'):
786+
BusinessHour(end=[])
787+
with pytest.raises(ValueError,
788+
match='number of starting time and ending time '
789+
'must be the same'):
781790
BusinessHour(start=['09:00', '11:00'])
782-
with pytest.raises(ValueError):
791+
with pytest.raises(ValueError,
792+
match='number of starting time and ending time '
793+
'must be the same'):
783794
BusinessHour(start=['09:00', '11:00'], end=['10:00'])
784-
with pytest.raises(ValueError):
795+
with pytest.raises(ValueError,
796+
match=r'invalid starting and ending time\(s\)'):
785797
BusinessHour(start=['09:00', '11:00'], end=['12:00', '20:00'])
786798

787799
def test_different_normalize_equals(self):

pandas/tseries/offsets.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,14 @@ def __init__(self, start='09:00', end='17:00', offset=timedelta(0)):
582582
# must be validated here to equality check
583583
if _iterable_not_string(start):
584584
start = np.asarray(start)
585+
if len(start) == 0:
586+
raise ValueError('number of starting time cannot be 0')
585587
else:
586588
start = np.array([start])
587589
if _iterable_not_string(end):
588590
end = np.asarray(end)
591+
if len(end) == 0:
592+
raise ValueError('number of ending time cannot be 0')
589593
else:
590594
end = np.array([end])
591595

@@ -595,7 +599,7 @@ def __init__(self, start='09:00', end='17:00', offset=timedelta(0)):
595599

596600
# Validation of input
597601
if len(start) != len(end):
598-
raise ValueError('number of starting time and ending time'
602+
raise ValueError('number of starting time and ending time '
599603
'must be the same')
600604
num_openings = len(start)
601605

@@ -639,15 +643,17 @@ def _get_daytime_flag(self, start, end):
639643

640644
def _next_opening_time(self, other, dir=1):
641645
"""
642-
If n is positive, return tomorrow's business day opening time.
643-
Otherwise yesterday's business day's opening time.
646+
If self.n and dir have the same sign, return the earliest opening time
647+
later than or equal to current time.
648+
Otherwise the latest opening time earlier than or equal to current time.
644649
645650
Opening time always locates on BusinessDay.
646-
Otherwise, closing time may not if business hour extends over midnight.
651+
However, closing time may not if business hour extends over midnight.
647652
"""
648653
earliest_start = self.start[0]
649654
latest_start = self.start[-1]
650655
if not self.next_bday.onOffset(other):
656+
# today is not business day
651657
other = other + dir * self.next_bday
652658
if self.n * dir >= 0:
653659
return datetime(other.year, other.month, other.day,
@@ -657,28 +663,34 @@ def _next_opening_time(self, other, dir=1):
657663
latest_start.hour, latest_start.minute)
658664
else:
659665
if self.n * dir >= 0 and latest_start < other.time():
666+
# current time is after latest starting time in today
660667
other = other + dir * self.next_bday
661668
return datetime(other.year, other.month, other.day,
662669
earliest_start.hour, earliest_start.minute)
663670
elif self.n * dir < 0 and other.time() < earliest_start:
671+
# current time is before earliest starting time in today
664672
other = other + dir * self.next_bday
665673
return datetime(other.year, other.month, other.day,
666674
latest_start.hour, latest_start.minute)
667675
if self.n * dir >= 0:
676+
# find earliest starting time later than or equal to current time
668677
for st in self.start:
669678
if other.time() <= st:
670679
return datetime(other.year, other.month, other.day,
671680
st.hour, st.minute)
672681
else:
682+
# find latest starting time earlier than or equal to current time
673683
for st in reversed(self.start):
674684
if other.time() >= st:
675685
return datetime(other.year, other.month, other.day,
676686
st.hour, st.minute)
677687

678688
def _prev_opening_time(self, other):
679689
"""
680-
If n is positive, return yesterday's business day opening time.
681-
Otherwise yesterday business day's opening time.
690+
If n is positive, return the latest opening time earlier than or equal
691+
to current time.
692+
Otherwise the earliest opening time later than or equal to current time.
693+
682694
"""
683695
return self._next_opening_time(other, dir=-1)
684696

0 commit comments

Comments
 (0)