Skip to content

Commit b6e35ff

Browse files
jbrockmendeljreback
authored andcommitted
[CLN] More Misc Cleanups in _libs (#22287)
1 parent a3c50a6 commit b6e35ff

12 files changed

+118
-93
lines changed

pandas/_libs/algos.pxd

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
from util cimport numeric
2-
from numpy cimport float64_t, double_t
2+
33

44
cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k) nogil
55

6+
67
cdef inline Py_ssize_t swap(numeric *a, numeric *b) nogil:
7-
cdef numeric t
8+
cdef:
9+
numeric t
810

911
# cython doesn't allow pointer dereference so use array syntax
1012
t = a[0]
1113
a[0] = b[0]
1214
b[0] = t
1315
return 0
1416

17+
1518
cdef enum TiebreakEnumType:
1619
TIEBREAK_AVERAGE
1720
TIEBREAK_MIN,

pandas/_libs/algos.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ tiebreakers = {
4545
}
4646

4747

48-
cdef inline are_diff(object left, object right):
48+
cdef inline bint are_diff(object left, object right):
4949
try:
5050
return fabs(left - right) > FP_ERR
5151
except TypeError:

pandas/_libs/algos_common_helper.pxi.in

+33-26
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ cpdef map_indices_{{name}}(ndarray[{{c_type}}] index):
6868

6969
@cython.boundscheck(False)
7070
@cython.wraparound(False)
71-
def pad_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
72-
limit=None):
73-
cdef Py_ssize_t i, j, nleft, nright
74-
cdef ndarray[int64_t, ndim=1] indexer
75-
cdef {{c_type}} cur, next
76-
cdef int lim, fill_count = 0
71+
def pad_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new, limit=None):
72+
cdef:
73+
Py_ssize_t i, j, nleft, nright
74+
ndarray[int64_t, ndim=1] indexer
75+
{{c_type}} cur, next
76+
int lim, fill_count = 0
7777

7878
nleft = len(old)
7979
nright = len(new)
@@ -135,9 +135,10 @@ def pad_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
135135
def pad_inplace_{{name}}(ndarray[{{c_type}}] values,
136136
ndarray[uint8_t, cast=True] mask,
137137
limit=None):
138-
cdef Py_ssize_t i, N
139-
cdef {{c_type}} val
140-
cdef int lim, fill_count = 0
138+
cdef:
139+
Py_ssize_t i, N
140+
{{c_type}} val
141+
int lim, fill_count = 0
141142

142143
N = len(values)
143144

@@ -171,9 +172,10 @@ def pad_inplace_{{name}}(ndarray[{{c_type}}] values,
171172
def pad_2d_inplace_{{name}}(ndarray[{{c_type}}, ndim=2] values,
172173
ndarray[uint8_t, ndim=2] mask,
173174
limit=None):
174-
cdef Py_ssize_t i, j, N, K
175-
cdef {{c_type}} val
176-
cdef int lim, fill_count = 0
175+
cdef:
176+
Py_ssize_t i, j, N, K
177+
{{c_type}} val
178+
int lim, fill_count = 0
177179

178180
K, N = (<object> values).shape
179181

@@ -233,10 +235,11 @@ D
233235
@cython.wraparound(False)
234236
def backfill_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
235237
limit=None):
236-
cdef Py_ssize_t i, j, nleft, nright
237-
cdef ndarray[int64_t, ndim=1] indexer
238-
cdef {{c_type}} cur, prev
239-
cdef int lim, fill_count = 0
238+
cdef:
239+
Py_ssize_t i, j, nleft, nright
240+
ndarray[int64_t, ndim=1] indexer
241+
{{c_type}} cur, prev
242+
int lim, fill_count = 0
240243

241244
nleft = len(old)
242245
nright = len(new)
@@ -299,9 +302,10 @@ def backfill_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
299302
def backfill_inplace_{{name}}(ndarray[{{c_type}}] values,
300303
ndarray[uint8_t, cast=True] mask,
301304
limit=None):
302-
cdef Py_ssize_t i, N
303-
cdef {{c_type}} val
304-
cdef int lim, fill_count = 0
305+
cdef:
306+
Py_ssize_t i, N
307+
{{c_type}} val
308+
int lim, fill_count = 0
305309

306310
N = len(values)
307311

@@ -335,9 +339,10 @@ def backfill_inplace_{{name}}(ndarray[{{c_type}}] values,
335339
def backfill_2d_inplace_{{name}}(ndarray[{{c_type}}, ndim=2] values,
336340
ndarray[uint8_t, ndim=2] mask,
337341
limit=None):
338-
cdef Py_ssize_t i, j, N, K
339-
cdef {{c_type}} val
340-
cdef int lim, fill_count = 0
342+
cdef:
343+
Py_ssize_t i, j, N, K
344+
{{c_type}} val
345+
int lim, fill_count = 0
341346

342347
K, N = (<object> values).shape
343348

@@ -428,10 +433,10 @@ def is_monotonic_{{name}}(ndarray[{{c_type}}] arr, bint timelike):
428433
@cython.wraparound(False)
429434
@cython.boundscheck(False)
430435
def arrmap_{{name}}(ndarray[{{c_type}}] index, object func):
431-
cdef Py_ssize_t length = index.shape[0]
432-
cdef Py_ssize_t i = 0
433-
434-
cdef ndarray[object] result = np.empty(length, dtype=np.object_)
436+
cdef:
437+
Py_ssize_t length = index.shape[0]
438+
Py_ssize_t i = 0
439+
ndarray[object] result = np.empty(length, dtype=np.object_)
435440

436441
from pandas._libs.lib import maybe_convert_objects
437442

@@ -535,6 +540,7 @@ def put2d_{{name}}_{{dest_type}}(ndarray[{{c_type}}, ndim=2, cast=True] values,
535540

536541
cdef int PLATFORM_INT = (<ndarray> np.arange(0, dtype=np.intp)).descr.type_num
537542

543+
538544
cpdef ensure_platform_int(object arr):
539545
# GH3033, GH1392
540546
# platform int is the size of the int pointer, e.g. np.intp
@@ -546,6 +552,7 @@ cpdef ensure_platform_int(object arr):
546552
else:
547553
return np.array(arr, dtype=np.intp)
548554

555+
549556
cpdef ensure_object(object arr):
550557
if util.is_array(arr):
551558
if (<ndarray> arr).descr.type_num == NPY_OBJECT:

pandas/_libs/groupby.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ cdef double nan = NaN
2727

2828

2929
cdef inline float64_t median_linear(float64_t* a, int n) nogil:
30-
cdef int i, j, na_count = 0
31-
cdef float64_t result
32-
cdef float64_t* tmp
30+
cdef:
31+
int i, j, na_count = 0
32+
float64_t result
33+
float64_t* tmp
3334

3435
if n == 0:
3536
return NaN
@@ -318,7 +319,7 @@ def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
318319

319320
# If we move to the next group, reset
320321
# the fill_idx and counter
321-
if i == N - 1 or labels[idx] != labels[sorted_labels[i+1]]:
322+
if i == N - 1 or labels[idx] != labels[sorted_labels[i + 1]]:
322323
curr_fill_idx = -1
323324
filled_vals = 0
324325

pandas/_libs/hashing.pyx

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ def hash_object_array(object[:] arr, object key, object encoding='utf8'):
4848
k = <bytes>key.encode(encoding)
4949
kb = <uint8_t *>k
5050
if len(k) != 16:
51-
raise ValueError(
52-
'key should be a 16-byte string encoded, got {!r} (len {})'.format(
53-
k, len(k)))
51+
raise ValueError("key should be a 16-byte string encoded, "
52+
"got {key} (len {klen})".format(key=k, klen=len(k)))
5453

5554
n = len(arr)
5655

@@ -70,8 +69,9 @@ def hash_object_array(object[:] arr, object key, object encoding='utf8'):
7069
data = <bytes>str(val).encode(encoding)
7170

7271
else:
73-
raise TypeError("{} of type {} is not a valid type for hashing, "
74-
"must be string or null".format(val, type(val)))
72+
raise TypeError("{val} of type {typ} is not a valid type "
73+
"for hashing, must be string or null"
74+
.format(val=val, typ=type(val)))
7575

7676
l = len(data)
7777
lens[i] = l
@@ -134,9 +134,9 @@ cdef inline void _sipround(uint64_t* v0, uint64_t* v1,
134134

135135
cpdef uint64_t siphash(bytes data, bytes key) except? 0:
136136
if len(key) != 16:
137-
raise ValueError(
138-
'key should be a 16-byte bytestring, got {!r} (len {})'.format(
139-
key, len(key)))
137+
raise ValueError("key should be a 16-byte bytestring, "
138+
"got {key} (len {klen})"
139+
.format(key=key, klen=len(key)))
140140
return low_level_siphash(data, len(data), key)
141141

142142

pandas/_libs/hashtable_class_helper.pxi.in

+10-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ cdef class {{name}}Vector:
115115

116116
if needs_resize(self.data):
117117
if self.external_view_exists:
118-
raise ValueError("external reference but Vector.resize() needed")
118+
raise ValueError("external reference but "
119+
"Vector.resize() needed")
119120
self.resize()
120121

121122
append_data_{{dtype}}(self.data, x)
@@ -194,6 +195,7 @@ cdef class StringVector:
194195
for i in range(len(x)):
195196
self.append(x[i])
196197

198+
197199
cdef class ObjectVector:
198200

199201
cdef:
@@ -215,7 +217,8 @@ cdef class ObjectVector:
215217
cdef inline append(self, object o):
216218
if self.n == self.m:
217219
if self.external_view_exists:
218-
raise ValueError("external reference but Vector.resize() needed")
220+
raise ValueError("external reference but "
221+
"Vector.resize() needed")
219222
self.m = max(self.m * 2, _INIT_VEC_CAP)
220223
self.ao.resize(self.m, refcheck=False)
221224
self.data = <PyObject**> self.ao.data
@@ -405,8 +408,9 @@ cdef class {{name}}HashTable(HashTable):
405408
if needs_resize(ud):
406409
with gil:
407410
if uniques.external_view_exists:
408-
raise ValueError("external reference to uniques held, "
409-
"but Vector.resize() needed")
411+
raise ValueError("external reference to "
412+
"uniques held, but "
413+
"Vector.resize() needed")
410414
uniques.resize()
411415
append_data_{{dtype}}(ud, val)
412416
labels[i] = count
@@ -742,8 +746,10 @@ cdef class StringHashTable(HashTable):
742746

743747
return np.asarray(labels)
744748

749+
745750
na_sentinel = object
746751

752+
747753
cdef class PyObjectHashTable(HashTable):
748754

749755
def __init__(self, size_hint=1):

pandas/_libs/hashtable_func_helper.pxi.in

-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ def ismember_{{dtype}}({{scalar}}[:] arr, {{scalar}}[:] values):
273273
{{endfor}}
274274

275275

276-
277276
#----------------------------------------------------------------------
278277
# Mode Computations
279278
#----------------------------------------------------------------------

pandas/_libs/reduction.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ is_numpy_prior_1_6_2 = LooseVersion(np.__version__) < '1.6.2'
2424
cdef _get_result_array(object obj, Py_ssize_t size, Py_ssize_t cnt):
2525

2626
if (util.is_array(obj) or
27-
isinstance(obj, list) and len(obj) == cnt or
27+
(isinstance(obj, list) and len(obj) == cnt) or
2828
getattr(obj, 'shape', None) == (cnt,)):
2929
raise ValueError('function does not reduce')
3030

pandas/_libs/tslib.pyx

+12-10
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ cdef inline object create_datetime_from_ts(
6161
return datetime(dts.year, dts.month, dts.day, dts.hour,
6262
dts.min, dts.sec, dts.us, tz)
6363

64+
6465
cdef inline object create_date_from_ts(
6566
int64_t value, npy_datetimestruct dts,
6667
object tz, object freq):
6768
""" convenience routine to construct a datetime.date from its parts """
6869
return date(dts.year, dts.month, dts.day)
6970

71+
7072
cdef inline object create_time_from_ts(
7173
int64_t value, npy_datetimestruct dts,
7274
object tz, object freq):
@@ -350,8 +352,8 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
350352

351353
if ((fvalues < _NS_LOWER_BOUND).any()
352354
or (fvalues > _NS_UPPER_BOUND).any()):
353-
raise OutOfBoundsDatetime(
354-
"cannot convert input with unit '{0}'".format(unit))
355+
raise OutOfBoundsDatetime("cannot convert input with unit "
356+
"'{unit}'".format(unit=unit))
355357
result = (iresult * m).astype('M8[ns]')
356358
iresult = result.view('i8')
357359
iresult[mask] = iNaT
@@ -377,8 +379,8 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
377379
except OverflowError:
378380
if is_raise:
379381
raise OutOfBoundsDatetime(
380-
"cannot convert input {0} with the unit "
381-
"'{1}'".format(val, unit))
382+
"cannot convert input {val} with the unit "
383+
"'{unit}'".format(val=val, unit=unit))
382384
elif is_ignore:
383385
raise AssertionError
384386
iresult[i] = NPY_NAT
@@ -393,16 +395,16 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
393395
except ValueError:
394396
if is_raise:
395397
raise ValueError(
396-
"non convertible value {0} with the unit "
397-
"'{1}'".format(val, unit))
398+
"non convertible value {val} with the unit "
399+
"'{unit}'".format(val=val, unit=unit))
398400
elif is_ignore:
399401
raise AssertionError
400402
iresult[i] = NPY_NAT
401403
except:
402404
if is_raise:
403405
raise OutOfBoundsDatetime(
404-
"cannot convert input {0} with the unit "
405-
"'{1}'".format(val, unit))
406+
"cannot convert input {val} with the unit "
407+
"'{unit}'".format(val=val, unit=unit))
406408
elif is_ignore:
407409
raise AssertionError
408410
iresult[i] = NPY_NAT
@@ -695,8 +697,8 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
695697
if is_coerce:
696698
iresult[i] = NPY_NAT
697699
else:
698-
raise TypeError("{0} is not convertible to datetime"
699-
.format(type(val)))
700+
raise TypeError("{typ} is not convertible to datetime"
701+
.format(typ=type(val)))
700702

701703
if seen_datetime and seen_integer:
702704
# we have mixed datetimes & integers

pandas/_libs/tslibs/timezones.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cpdef bint tz_compare(object start, object end)
1010
cpdef object get_timezone(object tz)
1111
cpdef object maybe_get_tz(object tz)
1212

13-
cpdef get_utcoffset(tzinfo, obj)
13+
cdef get_utcoffset(tzinfo, obj)
1414
cdef bint is_fixed_offset(object tz)
1515

1616
cdef object get_dst_info(object tz)

pandas/_libs/tslibs/timezones.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ cdef inline object tz_cache_key(object tz):
149149
# UTC Offsets
150150

151151

152-
cpdef get_utcoffset(tzinfo, obj):
152+
cdef get_utcoffset(tzinfo, obj):
153153
try:
154154
return tzinfo._utcoffset
155155
except AttributeError:
@@ -186,7 +186,7 @@ cdef object get_utc_trans_times_from_dateutil_tz(object tz):
186186
return new_trans
187187

188188

189-
cpdef int64_t[:] unbox_utcoffsets(object transinfo):
189+
cdef int64_t[:] unbox_utcoffsets(object transinfo):
190190
cdef:
191191
Py_ssize_t i, sz
192192
int64_t[:] arr

0 commit comments

Comments
 (0)