Skip to content

Commit 6c69a84

Browse files
authored
Ensure more const typing (#40119)
1 parent 8f87907 commit 6c69a84

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

pandas/_libs/window/aggregations.pyx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ cdef inline void remove_sum(float64_t val, int64_t *nobs, float64_t *sum_x,
116116
def roll_sum(const float64_t[:] values, ndarray[int64_t] start,
117117
ndarray[int64_t] end, int64_t minp):
118118
cdef:
119+
Py_ssize_t i, j
119120
float64_t sum_x = 0, compensation_add = 0, compensation_remove = 0
120121
int64_t s, e
121-
int64_t nobs = 0, i, j, N = len(values)
122+
int64_t nobs = 0, N = len(values)
122123
ndarray[float64_t] output
123124
bint is_monotonic_increasing_bounds
124125

@@ -493,12 +494,13 @@ cdef inline void remove_skew(float64_t val, int64_t *nobs,
493494
def roll_skew(ndarray[float64_t] values, ndarray[int64_t] start,
494495
ndarray[int64_t] end, int64_t minp):
495496
cdef:
497+
Py_ssize_t i, j
496498
float64_t val, prev, min_val, mean_val, sum_val = 0
497499
float64_t compensation_xxx_add = 0, compensation_xxx_remove = 0
498500
float64_t compensation_xx_add = 0, compensation_xx_remove = 0
499501
float64_t compensation_x_add = 0, compensation_x_remove = 0
500502
float64_t x = 0, xx = 0, xxx = 0
501-
int64_t nobs = 0, i, j, N = len(values), nobs_mean = 0
503+
int64_t nobs = 0, N = len(values), nobs_mean = 0
502504
int64_t s, e
503505
ndarray[float64_t] output, mean_array, values_copy
504506
bint is_monotonic_increasing_bounds
@@ -674,13 +676,14 @@ cdef inline void remove_kurt(float64_t val, int64_t *nobs,
674676
def roll_kurt(ndarray[float64_t] values, ndarray[int64_t] start,
675677
ndarray[int64_t] end, int64_t minp):
676678
cdef:
679+
Py_ssize_t i, j
677680
float64_t val, prev, mean_val, min_val, sum_val = 0
678681
float64_t compensation_xxxx_add = 0, compensation_xxxx_remove = 0
679682
float64_t compensation_xxx_remove = 0, compensation_xxx_add = 0
680683
float64_t compensation_xx_remove = 0, compensation_xx_add = 0
681684
float64_t compensation_x_remove = 0, compensation_x_add = 0
682685
float64_t x = 0, xx = 0, xxx = 0, xxxx = 0
683-
int64_t nobs = 0, i, j, s, e, N = len(values), nobs_mean = 0
686+
int64_t nobs = 0, s, e, N = len(values), nobs_mean = 0
684687
ndarray[float64_t] output, values_copy
685688
bint is_monotonic_increasing_bounds
686689

@@ -754,15 +757,13 @@ def roll_kurt(ndarray[float64_t] values, ndarray[int64_t] start,
754757
def roll_median_c(const float64_t[:] values, ndarray[int64_t] start,
755758
ndarray[int64_t] end, int64_t minp):
756759
cdef:
757-
float64_t val, res, prev
758-
bint err = False
759-
int ret = 0
760-
skiplist_t *sl
761760
Py_ssize_t i, j
761+
bint err = False, is_monotonic_increasing_bounds
762+
int midpoint, ret = 0
762763
int64_t nobs = 0, N = len(values), s, e, win
763-
int midpoint
764+
float64_t val, res, prev
765+
skiplist_t *sl
764766
ndarray[float64_t] output
765-
bint is_monotonic_increasing_bounds
766767

767768
is_monotonic_increasing_bounds = is_monotonic_increasing_start_end_bounds(
768769
start, end
@@ -933,8 +934,8 @@ cdef _roll_min_max(ndarray[numeric] values,
933934
bint is_max):
934935
cdef:
935936
numeric ai
936-
int64_t i, k, curr_win_size, start
937-
Py_ssize_t nobs = 0, N = len(values)
937+
int64_t curr_win_size, start
938+
Py_ssize_t i, k, nobs = 0, N = len(values)
938939
deque Q[int64_t] # min/max always the front
939940
deque W[int64_t] # track the whole window for nobs compute
940941
ndarray[float64_t, ndim=1] output
@@ -1017,14 +1018,14 @@ def roll_quantile(const float64_t[:] values, ndarray[int64_t] start,
10171018
O(N log(window)) implementation using skip list
10181019
"""
10191020
cdef:
1021+
Py_ssize_t i, j, s, e, N = len(values), idx
1022+
int ret = 0
1023+
int64_t nobs = 0, win
10201024
float64_t val, prev, midpoint, idx_with_fraction
1021-
skiplist_t *skiplist
1022-
int64_t nobs = 0, i, j, s, e, N = len(values), win
1023-
Py_ssize_t idx
1024-
ndarray[float64_t] output
10251025
float64_t vlow, vhigh
1026+
skiplist_t *skiplist
10261027
InterpolationType interpolation_type
1027-
int ret = 0
1028+
ndarray[float64_t] output
10281029

10291030
if quantile <= 0.0 or quantile >= 1.0:
10301031
raise ValueError(f"quantile value {quantile} not in [0, 1]")
@@ -1041,10 +1042,10 @@ def roll_quantile(const float64_t[:] values, ndarray[int64_t] start,
10411042
# actual skiplist ops outweigh any window computation costs
10421043
output = np.empty(N, dtype=float)
10431044

1044-
if (end - start).max() == 0:
1045+
win = (end - start).max()
1046+
if win == 0:
10451047
output[:] = NaN
10461048
return output
1047-
win = (end - start).max()
10481049
skiplist = skiplist_init(<int>win)
10491050
if skiplist == NULL:
10501051
raise MemoryError("skiplist_init failed")
@@ -1473,9 +1474,9 @@ def roll_weighted_var(const float64_t[:] values, const float64_t[:] weights,
14731474
# ----------------------------------------------------------------------
14741475
# Exponentially weighted moving average
14751476

1476-
def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
1477-
float64_t com, bint adjust, bint ignore_na, float64_t[:] times,
1478-
float64_t halflife):
1477+
def ewma(const float64_t[:] vals, const int64_t[:] start, const int64_t[:] end,
1478+
int minp, float64_t com, bint adjust, bint ignore_na,
1479+
const float64_t[:] times, float64_t halflife):
14791480
"""
14801481
Compute exponentially-weighted moving average using center-of-mass.
14811482
@@ -1486,8 +1487,10 @@ def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
14861487
end: ndarray (int64 type)
14871488
minp : int
14881489
com : float64
1489-
adjust : int
1490+
adjust : bool
14901491
ignore_na : bool
1492+
times : ndarray (float64 type)
1493+
halflife : float64
14911494
14921495
Returns
14931496
-------
@@ -1496,7 +1499,7 @@ def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
14961499

14971500
cdef:
14981501
Py_ssize_t i, j, s, e, nobs, win_size, N = len(vals), M = len(start)
1499-
float64_t[:] sub_vals
1502+
const float64_t[:] sub_vals
15001503
ndarray[float64_t] sub_output, output = np.empty(N, dtype=float)
15011504
float64_t alpha, old_wt_factor, new_wt, weighted_avg, old_wt, cur, delta
15021505
bint is_observation
@@ -1555,8 +1558,9 @@ def ewma(float64_t[:] vals, int64_t[:] start, int64_t[:] end, int minp,
15551558
# Exponentially weighted moving covariance
15561559

15571560

1558-
def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
1559-
float64_t[:] input_y, float64_t com, bint adjust, bint ignore_na, bint bias):
1561+
def ewmcov(const float64_t[:] input_x, const int64_t[:] start, const int64_t[:] end,
1562+
int minp, const float64_t[:] input_y, float64_t com, bint adjust,
1563+
bint ignore_na, bint bias):
15601564
"""
15611565
Compute exponentially-weighted moving variance using center-of-mass.
15621566
@@ -1568,9 +1572,9 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
15681572
minp : int
15691573
input_y : ndarray (float64 type)
15701574
com : float64
1571-
adjust : int
1575+
adjust : bool
15721576
ignore_na : bool
1573-
bias : int
1577+
bias : bool
15741578
15751579
Returns
15761580
-------
@@ -1583,7 +1587,7 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
15831587
float64_t alpha, old_wt_factor, new_wt, mean_x, mean_y, cov
15841588
float64_t sum_wt, sum_wt2, old_wt, cur_x, cur_y, old_mean_x, old_mean_y
15851589
float64_t numerator, denominator
1586-
float64_t[:] sub_x_vals, sub_y_vals
1590+
const float64_t[:] sub_x_vals, sub_y_vals
15871591
ndarray[float64_t] sub_out, output = np.empty(N, dtype=float)
15881592
bint is_observation
15891593

@@ -1594,6 +1598,8 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
15941598
return output
15951599

15961600
alpha = 1. / (1. + com)
1601+
old_wt_factor = 1. - alpha
1602+
new_wt = 1. if adjust else alpha
15971603

15981604
for j in range(L):
15991605
s = start[j]
@@ -1603,9 +1609,6 @@ def ewmcov(float64_t[:] input_x, int64_t[:] start, int64_t[:] end, int minp,
16031609
win_size = len(sub_x_vals)
16041610
sub_out = np.empty(win_size, dtype=float)
16051611

1606-
old_wt_factor = 1. - alpha
1607-
new_wt = 1. if adjust else alpha
1608-
16091612
mean_x = sub_x_vals[0]
16101613
mean_y = sub_y_vals[0]
16111614
is_observation = (mean_x == mean_x) and (mean_y == mean_y)

0 commit comments

Comments
 (0)