Skip to content

CLN: use cimports for type checks #56002

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 1 commit into from
Nov 17, 2023
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
14 changes: 2 additions & 12 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ from cpython.object cimport (
Py_EQ,
PyObject,
PyObject_RichCompareBool,
PyTypeObject,
)
from cpython.ref cimport Py_INCREF
from cpython.sequence cimport PySequence_Check
Expand Down Expand Up @@ -67,10 +66,6 @@ from numpy cimport (

cnp.import_array()

cdef extern from "Python.h":
# Note: importing extern-style allows us to declare these as nogil
# functions, whereas `from cpython cimport` does not.
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil

cdef extern from "numpy/arrayobject.h":
# cython's numpy.dtype specification is incorrect, which leads to
Expand All @@ -89,9 +84,6 @@ cdef extern from "numpy/arrayobject.h":
object fields
tuple names

PyTypeObject PySignedIntegerArrType_Type
PyTypeObject PyUnsignedIntegerArrType_Type

cdef extern from "pandas/parser/pd_parser.h":
int floatify(object, float64_t *result, int *maybe_int) except -1
void PandasParser_IMPORT()
Expand Down Expand Up @@ -1437,14 +1429,12 @@ cdef class Seen:
self.sint_ = (
self.sint_
or (oINT64_MIN <= val < 0)
# Cython equivalent of `isinstance(val, np.signedinteger)`
or PyObject_TypeCheck(val, &PySignedIntegerArrType_Type)
or isinstance(val, cnp.signedinteger)
)
self.uint_ = (
self.uint_
or (oINT64_MAX < val <= oUINT64_MAX)
# Cython equivalent of `isinstance(val, np.unsignedinteger)`
or PyObject_TypeCheck(val, &PyUnsignedIntegerArrType_Type)
or isinstance(val, cnp.unsignedinteger)
)

@property
Expand Down
9 changes: 5 additions & 4 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cdef extern from "Python.h":
object PyUnicode_DecodeLocale(const char *str, const char *errors) nogil


cimport numpy as cnp
from numpy cimport (
PyArray_Check,
float64_t,
Expand Down Expand Up @@ -54,7 +55,7 @@ cdef inline bint is_integer_object(object obj) noexcept:
"""
Cython equivalent of

`isinstance(val, (int, long, np.integer)) and not isinstance(val, bool)`
`isinstance(val, (int, np.integer)) and not isinstance(val, (bool, np.timedelta64))`

Parameters
----------
Expand All @@ -68,13 +69,13 @@ cdef inline bint is_integer_object(object obj) noexcept:
-----
This counts np.timedelta64 objects as integers.
"""
return (not PyBool_Check(obj) and PyArray_IsIntegerScalar(obj)
return (not PyBool_Check(obj) and isinstance(obj, (int, cnp.integer))
and not is_timedelta64_object(obj))


cdef inline bint is_float_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, (float, np.float64))`
Cython equivalent of `isinstance(val, (float, np.floating))`

Parameters
----------
Expand All @@ -90,7 +91,7 @@ cdef inline bint is_float_object(object obj) noexcept nogil:

cdef inline bint is_complex_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, (complex, np.complex128))`
Cython equivalent of `isinstance(val, (complex, np.complexfloating))`

Parameters
----------
Expand Down