From 12b346a65ddc53ba3250083c82cfbdd6e81ceacd Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 3 Apr 2020 22:25:42 +0300 Subject: [PATCH 1/3] CLN: Added static types --- pandas/_libs/algos.pyx | 47 ++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 7a32b8957003e..0ca0ecb145374 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -50,19 +50,17 @@ from pandas._libs.khash cimport ( import pandas._libs.missing as missing -cdef float64_t FP_ERR = 1e-13 - -cdef float64_t NaN = np.NaN - -cdef int64_t NPY_NAT = get_nat() - -tiebreakers = { - 'average': TIEBREAK_AVERAGE, - 'min': TIEBREAK_MIN, - 'max': TIEBREAK_MAX, - 'first': TIEBREAK_FIRST, - 'dense': TIEBREAK_DENSE, -} +cdef: + float64_t FP_ERR = 1e-13 + float64_t NaN = np.NaN + int64_t NPY_NAT = get_nat() + dict tiebreakers = { + "average": TIEBREAK_AVERAGE, + "min": TIEBREAK_MIN, + "max": TIEBREAK_MAX, + "first": TIEBREAK_FIRST, + "dense": TIEBREAK_DENSE, + } cdef inline bint are_diff(object left, object right): @@ -120,6 +118,7 @@ cpdef ndarray[int64_t, ndim=1] unique_deltas(const int64_t[:] arr): kh_int64_t *table int ret = 0 list uniques = [] + ndarray[int64_t, ndim=1] result table = kh_init_int64() kh_resize_int64(table, 10) @@ -261,7 +260,7 @@ def kth_smallest(numeric[:] a, Py_ssize_t k) -> numeric: @cython.boundscheck(False) @cython.wraparound(False) -def nancorr(const float64_t[:, :] mat, bint cov=0, minp=None): +def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None): cdef: Py_ssize_t i, j, xi, yi, N, K bint minpv @@ -325,7 +324,7 @@ def nancorr(const float64_t[:, :] mat, bint cov=0, minp=None): @cython.boundscheck(False) @cython.wraparound(False) -def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1): +def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1) -> ndarray: cdef: Py_ssize_t i, j, xi, yi, N, K ndarray[float64_t, ndim=2] result @@ -426,6 +425,9 @@ def _validate_limit(nobs: int, limit=None) -> int: int The limit. """ + cdef: + int lim + if limit is None: lim = nobs else: @@ -581,7 +583,7 @@ D @cython.boundscheck(False) @cython.wraparound(False) -def backfill(ndarray[algos_t] old, ndarray[algos_t] new, limit=None): +def backfill(ndarray[algos_t] old, ndarray[algos_t] new, limit=None) -> ndarray: cdef: Py_ssize_t i, j, nleft, nright ndarray[int64_t, ndim=1] indexer @@ -810,18 +812,14 @@ def rank_1d( """ cdef: Py_ssize_t i, j, n, dups = 0, total_tie_count = 0, non_na_idx = 0 - ndarray[rank_t] sorted_data, values - ndarray[float64_t] ranks ndarray[int64_t] argsorted ndarray[uint8_t, cast=True] sorted_mask - rank_t val, nan_value - float64_t sum_ranks = 0 int tiebreak = 0 - bint keep_na = 0 + bint keep_na = False bint isnan, condition float64_t count = 0.0 @@ -1034,19 +1032,14 @@ def rank_2d( """ cdef: Py_ssize_t i, j, z, k, n, dups = 0, total_tie_count = 0 - Py_ssize_t infs - ndarray[float64_t, ndim=2] ranks ndarray[rank_t, ndim=2] values - ndarray[int64_t, ndim=2] argsorted - rank_t val, nan_value - float64_t sum_ranks = 0 int tiebreak = 0 - bint keep_na = 0 + bint keep_na = False float64_t count = 0.0 bint condition, skip_condition From 4380d59d0931b57c974442773fbce0bec5513502 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 4 Apr 2020 21:51:24 +0300 Subject: [PATCH 2/3] Revert cdef on the dict --- pandas/_libs/algos.pyx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 0ca0ecb145374..a2c543b9cf90b 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -54,13 +54,14 @@ cdef: float64_t FP_ERR = 1e-13 float64_t NaN = np.NaN int64_t NPY_NAT = get_nat() - dict tiebreakers = { - "average": TIEBREAK_AVERAGE, - "min": TIEBREAK_MIN, - "max": TIEBREAK_MAX, - "first": TIEBREAK_FIRST, - "dense": TIEBREAK_DENSE, - } + +tiebreakers = { + "average": TIEBREAK_AVERAGE, + "min": TIEBREAK_MIN, + "max": TIEBREAK_MAX, + "first": TIEBREAK_FIRST, + "dense": TIEBREAK_DENSE, +} cdef inline bint are_diff(object left, object right): From 0e60fb54cf20408693fb0ecfa35e678ec02a8cde Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 4 Apr 2020 22:20:28 +0300 Subject: [PATCH 3/3] Revert cdef of lim --- pandas/_libs/algos.pyx | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index a2c543b9cf90b..6b6ead795584f 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -426,9 +426,6 @@ def _validate_limit(nobs: int, limit=None) -> int: int The limit. """ - cdef: - int lim - if limit is None: lim = nobs else: