Skip to content

Commit 7b04e97

Browse files
author
Si Wei How
committed
code abstraction
1 parent dcd8a00 commit 7b04e97

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

pandas/tseries/offsets.py

+18-43
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,12 @@ def __init__(self, start='09:00', end='17:00', offset=timedelta(0)):
591591
raise ValueError('number of starting time and ending time'
592592
'must be the same')
593593
num_openings = len(start)
594-
openings = sorted(zip(start, end))
595-
start = tuple(x for x, _ in openings)
596-
end = tuple(x for _, x in openings)
594+
595+
# sort starting and ending time by starting time
596+
index = np.argsort(start)
597+
start = tuple(np.array(start)[index])
598+
end = tuple(np.array(end)[index])
599+
597600
total_secs = 0
598601
for i in range(num_openings):
599602
total_secs += self._get_business_hours_by_sec(start[i], end[i])
@@ -627,7 +630,7 @@ def next_bday(self):
627630
def _get_daytime_flag(self, start, end):
628631
return start < end
629632

630-
def _next_opening_time(self, other):
633+
def _next_opening_time(self, other, dir=1):
631634
"""
632635
If n is positive, return tomorrow's business day opening time.
633636
Otherwise yesterday's business day's opening time.
@@ -638,23 +641,23 @@ def _next_opening_time(self, other):
638641
earliest_start = self.start[0]
639642
latest_start = self.start[-1]
640643
if not self.next_bday.onOffset(other):
641-
other = other + self.next_bday
642-
if self.n >= 0:
644+
other = other + dir * self.next_bday
645+
if self.n * dir >= 0:
643646
return datetime(other.year, other.month, other.day,
644647
earliest_start.hour, earliest_start.minute)
645648
else:
646649
return datetime(other.year, other.month, other.day,
647650
latest_start.hour, latest_start.minute)
648651
else:
649-
if self.n >= 0 and latest_start < other.time():
650-
other = other + self.next_bday
652+
if self.n * dir >= 0 and latest_start < other.time():
653+
other = other + dir * self.next_bday
651654
return datetime(other.year, other.month, other.day,
652655
earliest_start.hour, earliest_start.minute)
653-
elif self.n < 0 and other.time() < earliest_start:
654-
other = other + self.next_bday
656+
elif self.n * dir < 0 and other.time() < earliest_start:
657+
other = other + dir * self.next_bday
655658
return datetime(other.year, other.month, other.day,
656659
latest_start.hour, latest_start.minute)
657-
if self.n >= 0:
660+
if self.n * dir >= 0:
658661
for st in self.start:
659662
if other.time() <= st:
660663
return datetime(other.year, other.month, other.day,
@@ -670,35 +673,7 @@ def _prev_opening_time(self, other):
670673
If n is positive, return yesterday's business day opening time.
671674
Otherwise yesterday business day's opening time.
672675
"""
673-
earliest_start = self.start[0]
674-
latest_start = self.start[-1]
675-
if not self.next_bday.onOffset(other):
676-
other = other - self.next_bday
677-
if self.n < 0:
678-
return datetime(other.year, other.month, other.day,
679-
earliest_start.hour, earliest_start.minute)
680-
else:
681-
return datetime(other.year, other.month, other.day,
682-
latest_start.hour, latest_start.minute)
683-
else:
684-
if self.n < 0 and latest_start < other.time():
685-
other = other - self.next_bday
686-
return datetime(other.year, other.month, other.day,
687-
earliest_start.hour, earliest_start.minute)
688-
elif self.n >= 0 and other.time() < earliest_start:
689-
other = other - self.next_bday
690-
return datetime(other.year, other.month, other.day,
691-
latest_start.hour, latest_start.minute)
692-
if self.n < 0:
693-
for st in self.start:
694-
if other.time() <= st:
695-
return datetime(other.year, other.month, other.day,
696-
st.hour, st.minute)
697-
else:
698-
for st in reversed(self.start):
699-
if other.time() >= st:
700-
return datetime(other.year, other.month, other.day,
701-
st.hour, st.minute)
676+
return self._next_opening_time(other, dir=-1)
702677

703678
def _get_business_hours_by_sec(self, start, end):
704679
"""
@@ -862,10 +837,10 @@ def _onOffset(self, dt):
862837

863838
def _repr_attrs(self):
864839
out = super()._repr_attrs()
865-
hours = ','.join('{}-{}'.format(st.strftime('%H:%M'), en.strftime('%H:%M'))
840+
hours = ','.join('{}-{}'.format(
841+
st.strftime('%H:%M'), en.strftime('%H:%M'))
866842
for st, en in zip(self.start, self.end))
867-
attrs = ['{prefix}={hours}'.format(prefix=self._prefix,
868-
hours=hours)]
843+
attrs = ['{prefix}={hours}'.format(prefix=self._prefix, hours=hours)]
869844
out += ': ' + ', '.join(attrs)
870845
return out
871846

0 commit comments

Comments
 (0)