Skip to content

Commit b88139d

Browse files
committed
Use proper types so that we work with python3
1 parent 13b7474 commit b88139d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

doc/source/whatsnew/v0.16.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Performance Improvements
171171

172172
- Improved csv write performance with mixed dtypes, including datetimes by up to 5x (:issue:`9940`)
173173
- Improved csv write performance generally by 2x (:issue:`9940`)
174-
174+
- Improved the performance of ``pd.lib.max_len_string_array`` by 5-7x (:issue:`10024`)
175175

176176

177177

pandas/lib.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cimport numpy as np
22
cimport cython
33
import numpy as np
4+
import sys
45

56
from 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+
1315
from cpython cimport (PyDict_New, PyDict_GetItem, PyDict_SetItem,
1416
PyDict_Contains, PyDict_Keys,
1517
Py_INCREF, PyTuple_SET_ITEM,
@@ -19,10 +21,14 @@ from cpython cimport (PyDict_New, PyDict_GetItem, PyDict_SetItem,
1921
PyTuple_SetItem,
2022
PyTuple_New,
2123
PyObject_SetAttrString,
22-
PyString_GET_SIZE,
2324
PyBytes_GET_SIZE,
2425
PyUnicode_GET_SIZE)
2526

27+
try:
28+
from cpython cimport PyString_GET_SIZE
29+
except ImportError:
30+
from cpython cimport PyUnicode_GET_SIZE as PyString_GET_SIZE
31+
2632
cdef extern from "Python.h":
2733
Py_ssize_t PY_SSIZE_T_MAX
2834

@@ -35,7 +41,6 @@ cdef extern from "Python.h":
3541
Py_ssize_t *slicelength) except -1
3642

3743

38-
3944
cimport cpython
4045

4146
isnan = np.isnan
@@ -903,6 +908,7 @@ def clean_index_list(list obj):
903908
ctypedef fused pandas_t:
904909
str
905910
unicode
911+
bytes
906912

907913

908914
@cython.boundscheck(False)

pandas/tests/test_lib.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@
88
import pandas.util.testing as tm
99
from pandas.compat import u
1010

11+
1112
class TestMisc(tm.TestCase):
1213

1314
def test_max_len_string_array(self):
1415

15-
arr = np.array(['foo','b',np.nan],dtype='object')
16-
self.assertTrue(max_len_string_array(arr),3)
16+
arr = a = np.array(['foo', 'b', np.nan], dtype='object')
17+
self.assertTrue(max_len_string_array(arr), 3)
1718

1819
# unicode
19-
arr = arr.astype('U').astype(object)
20-
self.assertTrue(max_len_string_array(arr),3)
20+
arr = a.astype('U').astype(object)
21+
self.assertTrue(max_len_string_array(arr), 3)
22+
23+
# bytes for python3
24+
arr = a.astype('S').astype(object)
25+
self.assertTrue(max_len_string_array(arr), 3)
2126

2227
# raises
2328
tm.assertRaises(TypeError,
2429
lambda: max_len_string_array(arr.astype('U')))
2530

31+
2632
class TestIsscalar(tm.TestCase):
33+
2734
def test_isscalar_builtin_scalars(self):
2835
self.assertTrue(isscalar(None))
2936
self.assertTrue(isscalar(True))

0 commit comments

Comments
 (0)