Skip to content

Commit c95e943

Browse files
authored
remove ctypedef class from lib.pyx (#56018)
* remove ctypedef class from lib.pyx * fixes * try something for bytes * feedback * fix comparisons
1 parent 37ae5c4 commit c95e943

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

pandas/_libs/lib.pyx

+10-27
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,6 @@ from numpy cimport (
6767
cnp.import_array()
6868

6969

70-
cdef extern from "numpy/arrayobject.h":
71-
# cython's numpy.dtype specification is incorrect, which leads to
72-
# errors in issubclass(self.dtype.type, np.bool_), so we directly
73-
# include the correct version
74-
# https://github.com/cython/cython/issues/2022
75-
76-
ctypedef class numpy.dtype [object PyArray_Descr]:
77-
# Use PyDataType_* macros when possible, however there are no macros
78-
# for accessing some of the fields, so some are defined. Please
79-
# ask on cython-dev if you need more.
80-
cdef:
81-
int type_num
82-
int itemsize "elsize"
83-
char byteorder
84-
object fields
85-
tuple names
86-
8770
cdef extern from "pandas/parser/pd_parser.h":
8871
int floatify(object, float64_t *result, int *maybe_int) except -1
8972
void PandasParser_IMPORT()
@@ -1735,10 +1718,10 @@ cdef class Validator:
17351718

17361719
cdef:
17371720
Py_ssize_t n
1738-
dtype dtype
1721+
cnp.dtype dtype
17391722
bint skipna
17401723

1741-
def __cinit__(self, Py_ssize_t n, dtype dtype=np.dtype(np.object_),
1724+
def __cinit__(self, Py_ssize_t n, cnp.dtype dtype=np.dtype(np.object_),
17421725
bint skipna=False):
17431726
self.n = n
17441727
self.dtype = dtype
@@ -1819,7 +1802,7 @@ cdef class BoolValidator(Validator):
18191802
return util.is_bool_object(value)
18201803

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

18241807

18251808
cpdef bint is_bool_array(ndarray values, bint skipna=False):
@@ -1836,7 +1819,7 @@ cdef class IntegerValidator(Validator):
18361819
return util.is_integer_object(value)
18371820

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

18411824

18421825
# Note: only python-exposed for tests
@@ -1868,7 +1851,7 @@ cdef class IntegerFloatValidator(Validator):
18681851
return util.is_integer_object(value) or util.is_float_object(value)
18691852

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

18731856

18741857
cdef bint is_integer_float_array(ndarray values, bint skipna=True):
@@ -1885,7 +1868,7 @@ cdef class FloatValidator(Validator):
18851868
return util.is_float_object(value)
18861869

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

18901873

18911874
# Note: only python-exposed for tests
@@ -1904,7 +1887,7 @@ cdef class ComplexValidator(Validator):
19041887
)
19051888

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

19091892

19101893
cdef bint is_complex_array(ndarray values):
@@ -1933,7 +1916,7 @@ cdef class StringValidator(Validator):
19331916
return isinstance(value, str)
19341917

19351918
cdef bint is_array_typed(self) except -1:
1936-
return issubclass(self.dtype.type, np.str_)
1919+
return self.dtype.type_num == cnp.NPY_UNICODE
19371920

19381921

19391922
cpdef bint is_string_array(ndarray values, bint skipna=False):
@@ -1950,7 +1933,7 @@ cdef class BytesValidator(Validator):
19501933
return isinstance(value, bytes)
19511934

19521935
cdef bint is_array_typed(self) except -1:
1953-
return issubclass(self.dtype.type, np.bytes_)
1936+
return self.dtype.type_num == cnp.NPY_STRING
19541937

19551938

19561939
cdef bint is_bytes_array(ndarray values, bint skipna=False):
@@ -1965,7 +1948,7 @@ cdef class TemporalValidator(Validator):
19651948
cdef:
19661949
bint all_generic_na
19671950

1968-
def __cinit__(self, Py_ssize_t n, dtype dtype=np.dtype(np.object_),
1951+
def __cinit__(self, Py_ssize_t n, cnp.dtype dtype=np.dtype(np.object_),
19691952
bint skipna=False):
19701953
self.n = n
19711954
self.dtype = dtype

0 commit comments

Comments
 (0)