Skip to content

CLN: use fused type for lib.has_infs #42095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ def array_equivalent_object(
left: np.ndarray, # object[:]
right: np.ndarray, # object[:]
) -> bool: ...
def has_infs_f8(arr: np.ndarray) -> bool: ... # const float64_t[:]
def has_infs_f4(arr: np.ndarray) -> bool: ... # const float32_t[:]
def has_infs(arr: np.ndarray) -> bool: ... # const floating[:]
def get_reverse_indexer(
indexer: np.ndarray, # const intp_t[:]
length: int,
Expand Down
37 changes: 12 additions & 25 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ from cpython.tuple cimport (
PyTuple_New,
PyTuple_SET_ITEM,
)
from cython cimport floating

PyDateTime_IMPORT

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

@cython.wraparound(False)
@cython.boundscheck(False)
def has_infs_f4(const float32_t[:] arr) -> bool:
# Can add const once https://github.com/cython/cython/issues/1772 resolved
def has_infs(floating[:] arr) -> bool:
cdef:
Py_ssize_t i, n = len(arr)
float32_t inf, neginf, val
floating inf, neginf, val
bint ret = False

inf = np.inf
neginf = -inf

for i in range(n):
val = arr[i]
if val == inf or val == neginf:
return True
return False


@cython.wraparound(False)
@cython.boundscheck(False)
def has_infs_f8(const float64_t[:] arr) -> bool:
cdef:
Py_ssize_t i, n = len(arr)
float64_t inf, neginf, val

inf = np.inf
neginf = -inf

for i in range(n):
val = arr[i]
if val == inf or val == neginf:
return True
return False
with nogil:
for i in range(n):
val = arr[i]
if val == inf or val == neginf:
ret = True
break
return ret


def maybe_indices_to_slice(ndarray[intp_t] indices, int max_len):
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ def _bn_ok_dtype(dtype: DtypeObj, name: str) -> bool:

def _has_infs(result) -> bool:
if isinstance(result, np.ndarray):
if result.dtype == "f8":
return lib.has_infs_f8(result.ravel("K"))
elif result.dtype == "f4":
return lib.has_infs_f4(result.ravel("K"))
if result.dtype == "f8" or result.dtype == "f4":
return lib.has_infs(result.ravel("K"))
try:
return np.isinf(result).any()
except (TypeError, NotImplementedError):
Expand Down