From 4c82b17ecc80efa02d713b47e6a92b7036c7098a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Sun, 16 Feb 2025 19:30:19 -0800 Subject: [PATCH] add more conditional nogil --- pandas/_libs/algos.pyx | 28 +---------------------- pandas/_libs/hashtable_func_helper.pxi.in | 15 +----------- pandas/_libs/reshape.pyx | 22 +----------------- 3 files changed, 3 insertions(+), 62 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index f2b4baf508986..60ee73ef6b43f 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -818,33 +818,7 @@ def is_monotonic(const numeric_object_t[:] arr, bint timelike): if timelike and arr[0] == NPY_NAT: return False, False, False - if numeric_object_t is not object: - with nogil: - prev = arr[0] - for i in range(1, n): - cur = arr[i] - if timelike and cur == NPY_NAT: - is_monotonic_inc = 0 - is_monotonic_dec = 0 - break - if cur < prev: - is_monotonic_inc = 0 - elif cur > prev: - is_monotonic_dec = 0 - elif cur == prev: - is_unique = 0 - else: - # cur or prev is NaN - is_monotonic_inc = 0 - is_monotonic_dec = 0 - break - if not is_monotonic_inc and not is_monotonic_dec: - is_monotonic_inc = 0 - is_monotonic_dec = 0 - break - prev = cur - else: - # object-dtype, identical to above except we cannot use `with nogil` + with nogil(numeric_object_t is not object): prev = arr[0] for i in range(1, n): cur = arr[i] diff --git a/pandas/_libs/hashtable_func_helper.pxi.in b/pandas/_libs/hashtable_func_helper.pxi.in index 5500fadb73b6d..f957ebdeaf67a 100644 --- a/pandas/_libs/hashtable_func_helper.pxi.in +++ b/pandas/_libs/hashtable_func_helper.pxi.in @@ -415,20 +415,7 @@ def mode(ndarray[htfunc_t] values, bint dropna, const uint8_t[:] mask=None): modes = np.empty(nkeys, dtype=values.dtype) - if htfunc_t is not object: - with nogil: - for k in range(nkeys): - count = counts[k] - if count == max_count: - j += 1 - elif count > max_count: - max_count = count - j = 0 - else: - continue - - modes[j] = keys[k] - else: + with nogil(htfunc_t is not object): for k in range(nkeys): count = counts[k] if count == max_count: diff --git a/pandas/_libs/reshape.pyx b/pandas/_libs/reshape.pyx index 28ea06739e0c8..51c1c75ba631b 100644 --- a/pandas/_libs/reshape.pyx +++ b/pandas/_libs/reshape.pyx @@ -40,27 +40,7 @@ def unstack(const numeric_object_t[:, :] values, const uint8_t[:] mask, cdef: Py_ssize_t i, j, w, nulls, s, offset - if numeric_object_t is not object: - # evaluated at compile-time - with nogil: - for i in range(stride): - - nulls = 0 - for j in range(length): - - for w in range(width): - - offset = j * width + w - - if mask[offset]: - s = i * width + w - new_values[j, s] = values[offset - nulls, i] - new_mask[j, s] = 1 - else: - nulls += 1 - - else: - # object-dtype, identical to above but we cannot use nogil + with nogil(numeric_object_t is not object): for i in range(stride): nulls = 0