11cimport numpy as np
22cimport cython
33import numpy as np
4+ import sys
45
56from numpy cimport *
67
@@ -10,6 +11,7 @@ cdef extern from "numpy/arrayobject.h":
1011 cdef enum NPY_TYPES:
1112 NPY_intp " NPY_INTP"
1213
14+
1315from cpython cimport (PyDict_New, PyDict_GetItem, PyDict_SetItem,
1416 PyDict_Contains, PyDict_Keys,
1517 Py_INCREF, PyTuple_SET_ITEM,
@@ -18,7 +20,14 @@ from cpython cimport (PyDict_New, PyDict_GetItem, PyDict_SetItem,
1820 PyBytes_Check,
1921 PyTuple_SetItem,
2022 PyTuple_New,
21- PyObject_SetAttrString)
23+ PyObject_SetAttrString,
24+ PyBytes_GET_SIZE,
25+ PyUnicode_GET_SIZE)
26+
27+ try :
28+ from cpython cimport PyString_GET_SIZE
29+ except ImportError :
30+ from cpython cimport PyUnicode_GET_SIZE as PyString_GET_SIZE
2231
2332cdef extern from " Python.h" :
2433 Py_ssize_t PY_SSIZE_T_MAX
@@ -32,7 +41,6 @@ cdef extern from "Python.h":
3241 Py_ssize_t * slicelength) except - 1
3342
3443
35-
3644cimport cpython
3745
3846isnan = np.isnan
@@ -896,23 +904,32 @@ def clean_index_list(list obj):
896904
897905 return maybe_convert_objects(converted), 0
898906
907+
908+ ctypedef fused pandas_string:
909+ str
910+ unicode
911+ bytes
912+
913+
899914@ cython.boundscheck (False )
900915@ cython.wraparound (False )
901- def max_len_string_array (ndarray arr ):
916+ cpdef Py_ssize_t max_len_string_array(pandas_string[:] arr):
902917 """ return the maximum size of elements in a 1-dim string array """
903918 cdef:
904- int i, m, l
905- int length = arr.shape[0 ]
906- object v
919+ Py_ssize_t i, m = 0 , l = 0 , length = arr.shape[0 ]
920+ pandas_string v
907921
908- m = 0
909- for i from 0 <= i < length:
922+ for i in range (length):
910923 v = arr[i]
911- if PyString_Check(v) or PyBytes_Check(v) or PyUnicode_Check(v):
912- l = len (v)
913-
914- if l > m:
915- m = l
924+ if PyString_Check(v):
925+ l = PyString_GET_SIZE(v)
926+ elif PyBytes_Check(v):
927+ l = PyBytes_GET_SIZE(v)
928+ elif PyUnicode_Check(v):
929+ l = PyUnicode_GET_SIZE(v)
930+
931+ if l > m:
932+ m = l
916933
917934 return m
918935
0 commit comments