Skip to content

Commit 08ff553

Browse files
committed
Get rid of some of the branches in the code
1 parent 8089e67 commit 08ff553

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

pandas/_libs/window.pyx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ cdef _roll_min_max(ndarray[numeric] input, int64_t win, int64_t minp,
12231223
cdef:
12241224
numeric ai
12251225
bint is_variable, should_replace
1226+
int is_max_multiplier = (2 * is_max - 1)
12261227
int64_t N, i, removed
12271228
Py_ssize_t nobs = 0
12281229
deque Q[int64_t]
@@ -1254,25 +1255,17 @@ cdef _roll_min_max(ndarray[numeric] input, int64_t win, int64_t minp,
12541255
for i from 0 <= i < win:
12551256
ai = init_mm(input[i], &nobs, is_max)
12561257

1257-
if is_max:
1258-
while not Q.empty() and ai >= input[Q.back()]:
1259-
Q.pop_back()
1260-
else:
1261-
while not Q.empty() and ai <= input[Q.back()]:
1262-
Q.pop_back()
1258+
while Q.empty() and is_max_multiplier * (ai - input[Q.back()]) >= 0:
1259+
Q.pop_back()
12631260
Q.push_back(i)
12641261

12651262
for i from win <= i < N:
12661263
output[i-1] = calc_mm(minp, nobs, input[Q.front()])
12671264

12681265
ai = init_mm(input[i], &nobs, is_max)
12691266

1270-
if is_max:
1271-
while not Q.empty() and ai >= input[Q.back()]:
1272-
Q.pop_back()
1273-
else:
1274-
while not Q.empty() and ai <= input[Q.back()]:
1275-
Q.pop_back()
1267+
while Q.empty() and is_max_multiplier * (ai - input[Q.back()]) >= 0:
1268+
Q.pop_back()
12761269

12771270
while not Q.empty() and Q.front() <= i-win:
12781271
Q.pop_front()

0 commit comments

Comments
 (0)