Skip to content

Commit 960de3a

Browse files
authored
CLN: use fused type for lib.has_infs (pandas-dev#42095)
1 parent 7984bd4 commit 960de3a

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

pandas/_libs/lib.pyi

+1-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ def array_equivalent_object(
219219
left: np.ndarray, # object[:]
220220
right: np.ndarray, # object[:]
221221
) -> bool: ...
222-
def has_infs_f8(arr: np.ndarray) -> bool: ... # const float64_t[:]
223-
def has_infs_f4(arr: np.ndarray) -> bool: ... # const float32_t[:]
222+
def has_infs(arr: np.ndarray) -> bool: ... # const floating[:]
224223
def get_reverse_indexer(
225224
indexer: np.ndarray, # const intp_t[:]
226225
length: int,

pandas/_libs/lib.pyx

+12-25
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from cpython.tuple cimport (
2525
PyTuple_New,
2626
PyTuple_SET_ITEM,
2727
)
28+
from cython cimport floating
2829

2930
PyDateTime_IMPORT
3031

@@ -519,36 +520,22 @@ def get_reverse_indexer(const intp_t[:] indexer, Py_ssize_t length) -> ndarray:
519520

520521
@cython.wraparound(False)
521522
@cython.boundscheck(False)
522-
def has_infs_f4(const float32_t[:] arr) -> bool:
523+
# Can add const once https://github.com/cython/cython/issues/1772 resolved
524+
def has_infs(floating[:] arr) -> bool:
523525
cdef:
524526
Py_ssize_t i, n = len(arr)
525-
float32_t inf, neginf, val
527+
floating inf, neginf, val
528+
bint ret = False
526529

527530
inf = np.inf
528531
neginf = -inf
529-
530-
for i in range(n):
531-
val = arr[i]
532-
if val == inf or val == neginf:
533-
return True
534-
return False
535-
536-
537-
@cython.wraparound(False)
538-
@cython.boundscheck(False)
539-
def has_infs_f8(const float64_t[:] arr) -> bool:
540-
cdef:
541-
Py_ssize_t i, n = len(arr)
542-
float64_t inf, neginf, val
543-
544-
inf = np.inf
545-
neginf = -inf
546-
547-
for i in range(n):
548-
val = arr[i]
549-
if val == inf or val == neginf:
550-
return True
551-
return False
532+
with nogil:
533+
for i in range(n):
534+
val = arr[i]
535+
if val == inf or val == neginf:
536+
ret = True
537+
break
538+
return ret
552539

553540

554541
def maybe_indices_to_slice(ndarray[intp_t] indices, int max_len):

pandas/core/nanops.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,8 @@ def _bn_ok_dtype(dtype: DtypeObj, name: str) -> bool:
177177

178178
def _has_infs(result) -> bool:
179179
if isinstance(result, np.ndarray):
180-
if result.dtype == "f8":
181-
return lib.has_infs_f8(result.ravel("K"))
182-
elif result.dtype == "f4":
183-
return lib.has_infs_f4(result.ravel("K"))
180+
if result.dtype == "f8" or result.dtype == "f4":
181+
return lib.has_infs(result.ravel("K"))
184182
try:
185183
return np.isinf(result).any()
186184
except (TypeError, NotImplementedError):

0 commit comments

Comments
 (0)