Skip to content

remove ctypedef class from lib.pyx #56018

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 6 commits into from
Nov 17, 2023
Merged
Changes from 3 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
36 changes: 10 additions & 26 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ cdef extern from "Python.h":
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil

cdef extern from "numpy/arrayobject.h":
# cython's numpy.dtype specification is incorrect, which leads to
# errors in issubclass(self.dtype.type, np.bool_), so we directly
# include the correct version
# https://github.com/cython/cython/issues/2022

ctypedef class numpy.dtype [object PyArray_Descr]:
# Use PyDataType_* macros when possible, however there are no macros
# for accessing some of the fields, so some are defined. Please
# ask on cython-dev if you need more.
cdef:
int type_num
int itemsize "elsize"
char byteorder
object fields
tuple names

PyTypeObject PySignedIntegerArrType_Type
PyTypeObject PyUnsignedIntegerArrType_Type

Expand Down Expand Up @@ -1746,10 +1730,10 @@ cdef class Validator:

cdef:
Py_ssize_t n
dtype dtype
cnp.dtype dtype
bint skipna

def __cinit__(self, Py_ssize_t n, dtype dtype=np.dtype(np.object_),
def __cinit__(self, Py_ssize_t n, cnp.dtype dtype=np.dtype(np.object_),
bint skipna=False):
self.n = n
self.dtype = dtype
Expand Down Expand Up @@ -1830,7 +1814,7 @@ cdef class BoolValidator(Validator):
return util.is_bool_object(value)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.bool_)
return cnp.PyDataType_ISBOOL(self.dtype)


cpdef bint is_bool_array(ndarray values, bint skipna=False):
Expand All @@ -1847,7 +1831,7 @@ cdef class IntegerValidator(Validator):
return util.is_integer_object(value)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.integer)
return cnp.PyDataType_ISINTEGER(self.dtype)


# Note: only python-exposed for tests
Expand Down Expand Up @@ -1879,7 +1863,7 @@ cdef class IntegerFloatValidator(Validator):
return util.is_integer_object(value) or util.is_float_object(value)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.integer)
return cnp.PyDataType_ISINTEGER(self.dtype)


cdef bint is_integer_float_array(ndarray values, bint skipna=True):
Expand All @@ -1896,7 +1880,7 @@ cdef class FloatValidator(Validator):
return util.is_float_object(value)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.floating)
return cnp.PyDataType_ISFLOAT(self.dtype)


# Note: only python-exposed for tests
Expand All @@ -1915,7 +1899,7 @@ cdef class ComplexValidator(Validator):
)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.complexfloating)
return cnp.PyDataType_ISCOMPLEX(self.dtype)


cdef bint is_complex_array(ndarray values):
Expand Down Expand Up @@ -1944,7 +1928,7 @@ cdef class StringValidator(Validator):
return isinstance(value, str)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.str_)
return cnp.PyDataType_ISSTRING(self.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is also wrong, ISSTRING checks for bytes (NPY_STRING) OR unicode. You want == NPY_UNICODE here if you use this.



cpdef bint is_string_array(ndarray values, bint skipna=False):
Expand All @@ -1961,7 +1945,7 @@ cdef class BytesValidator(Validator):
return isinstance(value, bytes)

cdef bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.bytes_)
return self.dtype.type == cnp.NPY_BYTE or self.dtype.type == cnp.NPY_UBYTE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @seberg is this the right way to check for bytes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, its NPY_STRING (string is NPY_UNICODE, leftover from python 2)



cdef bint is_bytes_array(ndarray values, bint skipna=False):
Expand All @@ -1976,7 +1960,7 @@ cdef class TemporalValidator(Validator):
cdef:
bint all_generic_na

def __cinit__(self, Py_ssize_t n, dtype dtype=np.dtype(np.object_),
def __cinit__(self, Py_ssize_t n, cnp.dtype dtype=np.dtype(np.object_),
bint skipna=False):
self.n = n
self.dtype = dtype
Expand Down