Skip to content

Commit c040353

Browse files
jbrockmendeljreback
authored andcommitted
pythonize cython code (#22638)
1 parent 7ce722c commit c040353

31 files changed

+152
-155
lines changed

.coveragerc

-30
This file was deleted.

pandas/_libs/algos.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
cimport cython
4-
from cython cimport Py_ssize_t
3+
import cython
4+
from cython import Py_ssize_t
55

66
from libc.stdlib cimport malloc, free
77
from libc.string cimport memmove
@@ -114,7 +114,7 @@ cpdef ndarray[int64_t, ndim=1] unique_deltas(ndarray[int64_t] arr):
114114

115115
@cython.wraparound(False)
116116
@cython.boundscheck(False)
117-
def is_lexsorted(list list_of_arrays):
117+
def is_lexsorted(list_of_arrays: list) -> bint:
118118
cdef:
119119
Py_ssize_t i
120120
Py_ssize_t n, nlevels

pandas/_libs/hashing.pyx

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# at https://github.com/veorq/SipHash
44

55
import cython
6-
from cpython cimport PyBytes_Check, PyUnicode_Check
76
from libc.stdlib cimport malloc, free
87

98
import numpy as np
@@ -44,6 +43,7 @@ def hash_object_array(object[:] arr, object key, object encoding='utf8'):
4443
char **vecs
4544
char *cdata
4645
object val
46+
list datas = []
4747

4848
k = <bytes>key.encode(encoding)
4949
kb = <uint8_t *>k
@@ -57,12 +57,11 @@ def hash_object_array(object[:] arr, object key, object encoding='utf8'):
5757
vecs = <char **> malloc(n * sizeof(char *))
5858
lens = <uint64_t*> malloc(n * sizeof(uint64_t))
5959

60-
cdef list datas = []
6160
for i in range(n):
6261
val = arr[i]
63-
if PyBytes_Check(val):
62+
if isinstance(val, bytes):
6463
data = <bytes>val
65-
elif PyUnicode_Check(val):
64+
elif isinstance(val, unicode):
6665
data = <bytes>val.encode(encoding)
6766
elif val is None or is_nan(val):
6867
# null, stringify and encode
@@ -132,15 +131,6 @@ cdef inline void _sipround(uint64_t* v0, uint64_t* v1,
132131
v2[0] = _rotl(v2[0], 32)
133132

134133

135-
# TODO: This appears unused; remove?
136-
cpdef uint64_t siphash(bytes data, bytes key) except? 0:
137-
if len(key) != 16:
138-
raise ValueError("key should be a 16-byte bytestring, "
139-
"got {key} (len {klen})"
140-
.format(key=key, klen=len(key)))
141-
return low_level_siphash(data, len(data), key)
142-
143-
144134
@cython.cdivision(True)
145135
cdef uint64_t low_level_siphash(uint8_t* data, size_t datalen,
146136
uint8_t* key) nogil:

pandas/_libs/index.pyx

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from datetime import datetime, timedelta, date
33

4-
cimport cython
5-
6-
from cpython cimport PyTuple_Check, PyList_Check
7-
from cpython.slice cimport PySlice_Check
4+
import cython
85

96
import numpy as np
107
cimport numpy as cnp
@@ -30,15 +27,15 @@ cdef int64_t iNaT = util.get_nat()
3027

3128

3229
cdef inline bint is_definitely_invalid_key(object val):
33-
if PyTuple_Check(val):
30+
if isinstance(val, tuple):
3431
try:
3532
hash(val)
3633
except TypeError:
3734
return True
3835

3936
# we have a _data, means we are a NDFrame
40-
return (PySlice_Check(val) or util.is_array(val)
41-
or PyList_Check(val) or hasattr(val, '_data'))
37+
return (isinstance(val, slice) or util.is_array(val)
38+
or isinstance(val, list) or hasattr(val, '_data'))
4239

4340

4441
cpdef get_value_at(ndarray arr, object loc, object tz=None):
@@ -88,7 +85,7 @@ cdef class IndexEngine:
8885
void* data_ptr
8986

9087
loc = self.get_loc(key)
91-
if PySlice_Check(loc) or util.is_array(loc):
88+
if isinstance(loc, slice) or util.is_array(loc):
9289
return arr[loc]
9390
else:
9491
return get_value_at(arr, loc, tz=tz)
@@ -640,7 +637,7 @@ cdef class BaseMultiIndexCodesEngine:
640637
def get_loc(self, object key):
641638
if is_definitely_invalid_key(key):
642639
raise TypeError("'{key}' is an invalid key".format(key=key))
643-
if not PyTuple_Check(key):
640+
if not isinstance(key, tuple):
644641
raise KeyError(key)
645642
try:
646643
indices = [0 if checknull(v) else lev.get_loc(v) + 1

pandas/_libs/internals.pyx

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
cimport cython
4-
from cython cimport Py_ssize_t
3+
import cython
4+
from cython import Py_ssize_t
55

66
from cpython cimport PyObject
7-
from cpython.slice cimport PySlice_Check
87

98
cdef extern from "Python.h":
109
Py_ssize_t PY_SSIZE_T_MAX
@@ -30,14 +29,15 @@ cdef class BlockPlacement:
3029
cdef bint _has_slice, _has_array, _is_known_slice_like
3130

3231
def __init__(self, val):
33-
cdef slice slc
32+
cdef:
33+
slice slc
3434

3535
self._as_slice = None
3636
self._as_array = None
3737
self._has_slice = False
3838
self._has_array = False
3939

40-
if PySlice_Check(val):
40+
if isinstance(val, slice):
4141
slc = slice_canonize(val)
4242

4343
if slc.start != slc.stop:
@@ -55,7 +55,8 @@ cdef class BlockPlacement:
5555
self._has_array = True
5656

5757
def __str__(self):
58-
cdef slice s = self._ensure_has_slice()
58+
cdef:
59+
slice s = self._ensure_has_slice()
5960
if s is not None:
6061
v = self._as_slice
6162
else:
@@ -66,15 +67,17 @@ cdef class BlockPlacement:
6667
__repr__ = __str__
6768

6869
def __len__(self):
69-
cdef slice s = self._ensure_has_slice()
70+
cdef:
71+
slice s = self._ensure_has_slice()
7072
if s is not None:
7173
return slice_len(s)
7274
else:
7375
return len(self._as_array)
7476

7577
def __iter__(self):
76-
cdef slice s = self._ensure_has_slice()
77-
cdef Py_ssize_t start, stop, step, _
78+
cdef:
79+
slice s = self._ensure_has_slice()
80+
Py_ssize_t start, stop, step, _
7881
if s is not None:
7982
start, stop, step, _ = slice_get_indices_ex(s)
8083
return iter(range(start, stop, step))
@@ -83,15 +86,17 @@ cdef class BlockPlacement:
8386

8487
@property
8588
def as_slice(self):
86-
cdef slice s = self._ensure_has_slice()
89+
cdef:
90+
slice s = self._ensure_has_slice()
8791
if s is None:
8892
raise TypeError('Not slice-like')
8993
else:
9094
return s
9195

9296
@property
9397
def indexer(self):
94-
cdef slice s = self._ensure_has_slice()
98+
cdef:
99+
slice s = self._ensure_has_slice()
95100
if s is not None:
96101
return s
97102
else:
@@ -103,7 +108,8 @@ cdef class BlockPlacement:
103108

104109
@property
105110
def as_array(self):
106-
cdef Py_ssize_t start, stop, end, _
111+
cdef:
112+
Py_ssize_t start, stop, end, _
107113
if not self._has_array:
108114
start, stop, step, _ = slice_get_indices_ex(self._as_slice)
109115
self._as_array = np.arange(start, stop, step,
@@ -113,17 +119,19 @@ cdef class BlockPlacement:
113119

114120
@property
115121
def is_slice_like(self):
116-
cdef slice s = self._ensure_has_slice()
122+
cdef:
123+
slice s = self._ensure_has_slice()
117124
return s is not None
118125

119126
def __getitem__(self, loc):
120-
cdef slice s = self._ensure_has_slice()
127+
cdef:
128+
slice s = self._ensure_has_slice()
121129
if s is not None:
122130
val = slice_getitem(s, loc)
123131
else:
124132
val = self._as_array[loc]
125133

126-
if not PySlice_Check(val) and val.ndim == 0:
134+
if not isinstance(val, slice) and val.ndim == 0:
127135
return val
128136

129137
return BlockPlacement(val)
@@ -139,8 +147,9 @@ cdef class BlockPlacement:
139147
[o.as_array for o in others]))
140148

141149
cdef iadd(self, other):
142-
cdef slice s = self._ensure_has_slice()
143-
cdef Py_ssize_t other_int, start, stop, step, l
150+
cdef:
151+
slice s = self._ensure_has_slice()
152+
Py_ssize_t other_int, start, stop, step, l
144153

145154
if isinstance(other, int) and s is not None:
146155
other_int = <Py_ssize_t>other
@@ -184,7 +193,7 @@ cdef class BlockPlacement:
184193
return self._as_slice
185194

186195

187-
cdef slice_canonize(slice s):
196+
cdef slice slice_canonize(slice s):
188197
"""
189198
Convert slice to canonical bounded form.
190199
"""
@@ -282,7 +291,7 @@ def slice_getitem(slice slc not None, ind):
282291

283292
s_start, s_stop, s_step, s_len = slice_get_indices_ex(slc)
284293

285-
if PySlice_Check(ind):
294+
if isinstance(ind, slice):
286295
ind_start, ind_stop, ind_step, ind_len = slice_get_indices_ex(ind,
287296
s_len)
288297

pandas/_libs/interval.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ cdef class Interval(IntervalMixin):
271271
return ((self.left < key if self.open_left else self.left <= key) and
272272
(key < self.right if self.open_right else key <= self.right))
273273

274-
def __richcmp__(self, other, int op):
274+
def __richcmp__(self, other, op: int):
275275
if hasattr(other, 'ndim'):
276276
# let numpy (or IntervalIndex) handle vectorization
277277
return NotImplemented

0 commit comments

Comments
 (0)