Skip to content

Commit 7390963

Browse files
jbrockmendelgfyoung
authored andcommitted
CLN: Assorted _libs cleanups (#22235)
1 parent 475e391 commit 7390963

31 files changed

+201
-268
lines changed

pandas/_libs/algos_common_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ def put2d_{{name}}_{{dest_type}}(ndarray[{{c_type}}, ndim=2, cast=True] values,
523523
Py_ssize_t i, j, k
524524

525525
k = len(values)
526-
for j from 0 <= j < k:
526+
for j in range(k):
527527
i = indexer[j]
528528
out[i] = values[j, loc]
529529

pandas/_libs/algos_take_helper.pxi.in

+11-11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_dispatch(dtypes):
4646
fv = fill_value
4747

4848
%(nogil_str)s
49-
%(tab)sfor i from 0 <= i < n:
49+
%(tab)sfor i in range(n):
5050
%(tab)s idx = indexer[i]
5151
%(tab)s if idx == -1:
5252
%(tab)s out[i] = fv
@@ -74,24 +74,24 @@ def get_dispatch(dtypes):
7474
values.strides[1] == sizeof(%(c_type_out)s) and
7575
sizeof(%(c_type_out)s) * n >= 256):
7676

77-
for i from 0 <= i < n:
77+
for i in range(n):
7878
idx = indexer[i]
7979
if idx == -1:
80-
for j from 0 <= j < k:
80+
for j in range(k):
8181
out[i, j] = fv
8282
else:
8383
v = &values[idx, 0]
8484
o = &out[i, 0]
8585
memmove(o, v, <size_t>(sizeof(%(c_type_out)s) * k))
8686
return
8787

88-
for i from 0 <= i < n:
88+
for i in range(n):
8989
idx = indexer[i]
9090
if idx == -1:
91-
for j from 0 <= j < k:
91+
for j in range(k):
9292
out[i, j] = fv
9393
else:
94-
for j from 0 <= j < k:
94+
for j in range(k):
9595
out[i, j] = %(preval)svalues[idx, j]%(postval)s
9696
"""
9797

@@ -108,8 +108,8 @@ def get_dispatch(dtypes):
108108

109109
fv = fill_value
110110

111-
for i from 0 <= i < n:
112-
for j from 0 <= j < k:
111+
for i in range(n):
112+
for j in range(k):
113113
idx = indexer[j]
114114
if idx == -1:
115115
out[i, j] = fv
@@ -246,13 +246,13 @@ def take_2d_multi_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=2] values,
246246
k = len(idx1)
247247

248248
fv = fill_value
249-
for i from 0 <= i < n:
249+
for i in range(n):
250250
idx = idx0[i]
251251
if idx == -1:
252-
for j from 0 <= j < k:
252+
for j in range(k):
253253
out[i, j] = fv
254254
else:
255-
for j from 0 <= j < k:
255+
for j in range(k):
256256
if idx1[j] == -1:
257257
out[i, j] = fv
258258
else:

pandas/_libs/hashtable_func_helper.pxi.in

+4-4
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,18 @@ def duplicated_{{dtype}}({{scalar}}[:] values, object keep='first'):
161161
{{endif}}
162162
elif keep == 'first':
163163
{{if dtype == 'object'}}
164-
for i from 0 <= i < n:
164+
for i in range(n):
165165
kh_put_{{ttype}}(table, <PyObject*> values[i], &ret)
166166
out[i] = ret == 0
167167
{{else}}
168168
with nogil:
169-
for i from 0 <= i < n:
169+
for i in range(n):
170170
kh_put_{{ttype}}(table, values[i], &ret)
171171
out[i] = ret == 0
172172
{{endif}}
173173
else:
174174
{{if dtype == 'object'}}
175-
for i from 0 <= i < n:
175+
for i in range(n):
176176
value = values[i]
177177
k = kh_get_{{ttype}}(table, <PyObject*> value)
178178
if k != table.n_buckets:
@@ -185,7 +185,7 @@ def duplicated_{{dtype}}({{scalar}}[:] values, object keep='first'):
185185
out[i] = 0
186186
{{else}}
187187
with nogil:
188-
for i from 0 <= i < n:
188+
for i in range(n):
189189
value = values[i]
190190
k = kh_get_{{ttype}}(table, value)
191191
if k != table.n_buckets:

pandas/_libs/lib.pyx

+12-11
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,30 @@ cdef bint PY2 = sys.version_info[0] == 2
7878
cdef double nan = <double>np.NaN
7979

8080

81-
def values_from_object(object o):
81+
def values_from_object(object obj):
8282
""" return my values or the object if we are say an ndarray """
83-
cdef f
83+
cdef func # TODO: Does declaring this without a type accomplish anything?
8484

85-
f = getattr(o, 'get_values', None)
86-
if f is not None:
87-
o = f()
85+
func = getattr(obj, 'get_values', None)
86+
if func is not None:
87+
obj = func()
8888

89-
return o
89+
return obj
9090

9191

9292
@cython.wraparound(False)
9393
@cython.boundscheck(False)
94-
def memory_usage_of_objects(ndarray[object, ndim=1] arr):
94+
def memory_usage_of_objects(object[:] arr):
9595
""" return the memory usage of an object array in bytes,
9696
does not include the actual bytes of the pointers """
97-
cdef Py_ssize_t i, n
98-
cdef int64_t s = 0
97+
cdef:
98+
Py_ssize_t i, n
99+
int64_t size = 0
99100

100101
n = len(arr)
101102
for i in range(n):
102-
s += arr[i].__sizeof__()
103-
return s
103+
size += arr[i].__sizeof__()
104+
return size
104105

105106

106107
# ----------------------------------------------------------------------

pandas/_libs/ops.pyx

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
# cython: profile=False
32
import operator
43

54
from cpython cimport (PyFloat_Check, PyBool_Check,
@@ -21,7 +20,7 @@ from missing cimport checknull
2120

2221
@cython.wraparound(False)
2322
@cython.boundscheck(False)
24-
def scalar_compare(ndarray[object] values, object val, object op):
23+
def scalar_compare(object[:] values, object val, object op):
2524
"""
2625
Compare each element of `values` array with the scalar `val`, with
2726
the comparison operation described by `op`.
@@ -73,7 +72,7 @@ def scalar_compare(ndarray[object] values, object val, object op):
7372
else:
7473
try:
7574
result[i] = PyObject_RichCompareBool(x, val, flag)
76-
except (TypeError):
75+
except TypeError:
7776
result[i] = True
7877
elif flag == Py_EQ:
7978
for i in range(n):
@@ -85,7 +84,7 @@ def scalar_compare(ndarray[object] values, object val, object op):
8584
else:
8685
try:
8786
result[i] = PyObject_RichCompareBool(x, val, flag)
88-
except (TypeError):
87+
except TypeError:
8988
result[i] = False
9089

9190
else:
@@ -103,7 +102,7 @@ def scalar_compare(ndarray[object] values, object val, object op):
103102

104103
@cython.wraparound(False)
105104
@cython.boundscheck(False)
106-
def vec_compare(ndarray[object] left, ndarray[object] right, object op):
105+
def vec_compare(object[:] left, object[:] right, object op):
107106
"""
108107
Compare the elements of `left` with the elements of `right` pointwise,
109108
with the comparison operation described by `op`.
@@ -126,8 +125,8 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
126125
int flag
127126

128127
if n != len(right):
129-
raise ValueError('Arrays were different lengths: %d vs %d'
130-
% (n, len(right)))
128+
raise ValueError('Arrays were different lengths: {n} vs {nright}'
129+
.format(n=n, nright=len(right)))
131130

132131
if op is operator.lt:
133132
flag = Py_LT
@@ -170,7 +169,7 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
170169

171170
@cython.wraparound(False)
172171
@cython.boundscheck(False)
173-
def scalar_binop(ndarray[object] values, object val, object op):
172+
def scalar_binop(object[:] values, object val, object op):
174173
"""
175174
Apply the given binary operator `op` between each element of the array
176175
`values` and the scalar `val`.
@@ -187,13 +186,13 @@ def scalar_binop(ndarray[object] values, object val, object op):
187186
"""
188187
cdef:
189188
Py_ssize_t i, n = len(values)
190-
ndarray[object] result
189+
object[:] result
191190
object x
192191

193192
result = np.empty(n, dtype=object)
194193
if val is None or is_nan(val):
195-
result.fill(val)
196-
return result
194+
result[:] = val
195+
return result.base # `.base` to access underlying np.ndarray
197196

198197
for i in range(n):
199198
x = values[i]
@@ -202,12 +201,12 @@ def scalar_binop(ndarray[object] values, object val, object op):
202201
else:
203202
result[i] = op(x, val)
204203

205-
return maybe_convert_bool(result)
204+
return maybe_convert_bool(result.base)
206205

207206

208207
@cython.wraparound(False)
209208
@cython.boundscheck(False)
210-
def vec_binop(ndarray[object] left, ndarray[object] right, object op):
209+
def vec_binop(object[:] left, object[:] right, object op):
211210
"""
212211
Apply the given binary operator `op` pointwise to the elements of
213212
arrays `left` and `right`.
@@ -224,11 +223,11 @@ def vec_binop(ndarray[object] left, ndarray[object] right, object op):
224223
"""
225224
cdef:
226225
Py_ssize_t i, n = len(left)
227-
ndarray[object] result
226+
object[:] result
228227

229228
if n != len(right):
230-
raise ValueError('Arrays were different lengths: %d vs %d'
231-
% (n, len(right)))
229+
raise ValueError('Arrays were different lengths: {n} vs {nright}'
230+
.format(n=n, nright=len(right)))
232231

233232
result = np.empty(n, dtype=object)
234233

@@ -245,7 +244,7 @@ def vec_binop(ndarray[object] left, ndarray[object] right, object op):
245244
else:
246245
raise
247246

248-
return maybe_convert_bool(result)
247+
return maybe_convert_bool(result.base) # `.base` to access np.ndarray
249248

250249

251250
def maybe_convert_bool(ndarray[object] arr,
@@ -270,7 +269,7 @@ def maybe_convert_bool(ndarray[object] arr,
270269
if false_values is not None:
271270
false_vals = false_vals | set(false_values)
272271

273-
for i from 0 <= i < n:
272+
for i in range(n):
274273
val = arr[i]
275274

276275
if PyBool_Check(val):

pandas/_libs/sparse.pyx

+13-12
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ cdef class IntIndex(SparseIndex):
148148
new_indices = np.empty(min(
149149
len(xindices), len(yindices)), dtype=np.int32)
150150

151-
for xi from 0 <= xi < self.npoints:
151+
for xi in range(self.npoints):
152152
xind = xindices[xi]
153153

154154
while yi < y.npoints and yindices[yi] < xind:
@@ -292,7 +292,7 @@ cpdef get_blocks(ndarray[int32_t, ndim=1] indices):
292292

293293
# TODO: two-pass algorithm faster?
294294
prev = block = indices[0]
295-
for i from 1 <= i < npoints:
295+
for i in range(1, npoints):
296296
cur = indices[i]
297297
if cur - prev > 1:
298298
# new block
@@ -383,21 +383,22 @@ cdef class BlockIndex(SparseIndex):
383383
if len(blocs) != len(blengths):
384384
raise ValueError('block bound arrays must be same length')
385385

386-
for i from 0 <= i < self.nblocks:
386+
for i in range(self.nblocks):
387387
if i > 0:
388388
if blocs[i] <= blocs[i - 1]:
389389
raise ValueError('Locations not in ascending order')
390390

391391
if i < self.nblocks - 1:
392392
if blocs[i] + blengths[i] > blocs[i + 1]:
393-
raise ValueError('Block %d overlaps' % i)
393+
raise ValueError('Block {idx} overlaps'.format(idx=i))
394394
else:
395395
if blocs[i] + blengths[i] > self.length:
396-
raise ValueError('Block %d extends beyond end' % i)
396+
raise ValueError('Block {idx} extends beyond end'
397+
.format(idx=i))
397398

398399
# no zero-length blocks
399400
if blengths[i] == 0:
400-
raise ValueError('Zero-length block %d' % i)
401+
raise ValueError('Zero-length block {idx}'.format(idx=i))
401402

402403
def equals(self, other):
403404
if not isinstance(other, BlockIndex):
@@ -422,10 +423,10 @@ cdef class BlockIndex(SparseIndex):
422423

423424
indices = np.empty(self.npoints, dtype=np.int32)
424425

425-
for b from 0 <= b < self.nblocks:
426+
for b in range(self.nblocks):
426427
offset = self.locbuf[b]
427428

428-
for j from 0 <= j < self.lenbuf[b]:
429+
for j in range(self.lenbuf[b]):
429430
indices[i] = offset + j
430431
i += 1
431432

@@ -551,7 +552,7 @@ cdef class BlockIndex(SparseIndex):
551552
return -1
552553

553554
cum_len = 0
554-
for i from 0 <= i < self.nblocks:
555+
for i in range(self.nblocks):
555556
if index >= locs[i] and index < locs[i] + lens[i]:
556557
return cum_len + index - locs[i]
557558
cum_len += lens[i]
@@ -579,11 +580,11 @@ cdef class BlockIndex(SparseIndex):
579580
if self.npoints == 0:
580581
return results
581582

582-
for i from 0 <= i < n:
583+
for i in range(n):
583584
ind_val = indexer[i]
584585
if not (ind_val < 0 or self.length <= ind_val):
585586
cum_len = 0
586-
for j from 0 <= j < self.nblocks:
587+
for j in range(self.nblocks):
587588
if ind_val >= locs[j] and ind_val < locs[j] + lens[j]:
588589
results[i] = cum_len + ind_val - locs[j]
589590
cum_len += lens[j]
@@ -824,7 +825,7 @@ def get_reindexer(ndarray[object, ndim=1] values, dict index_map):
824825

825826
# out = np.empty(length, dtype=np.float64)
826827

827-
# for i from 0 <= i < length:
828+
# for i in range(length):
828829
# if indexer[i] == -1:
829830
# pass
830831

pandas/_libs/sparse_op_helper.pxi.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ cdef inline tuple block_op_{{opname}}_{{dtype}}(ndarray x_,
190190
# Wow, what a hack job. Need to do something about this
191191

192192
# walk the two SparseVectors, adding matched locations...
193-
for out_i from 0 <= out_i < out_index.npoints:
193+
for out_i in range(out_index.npoints):
194194
if yblock == yindex.nblocks:
195195
# use y fill value
196196
out[out_i] = {{(opname, 'x[xi]', 'yfill', dtype) | get_op}}
@@ -286,7 +286,7 @@ cdef inline tuple int_op_{{opname}}_{{dtype}}(ndarray x_, IntIndex xindex,
286286
out_indices = out_index.indices
287287

288288
# walk the two SparseVectors, adding matched locations...
289-
for out_i from 0 <= out_i < out_index.npoints:
289+
for out_i in range(out_index.npoints):
290290
if xi == xindex.npoints:
291291
# use x fill value
292292
out[out_i] = {{(opname, 'xfill', 'y[yi]', dtype) | get_op}}

pandas/_libs/src/compat_helper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The full license is in the LICENSE file, distributed with this software.
1111
#define PANDAS__LIBS_SRC_COMPAT_HELPER_H_
1212

1313
#include "Python.h"
14-
#include "helper.h"
14+
#include "inline_helper.h"
1515

1616
/*
1717
PySlice_GetIndicesEx changes signature in PY3

0 commit comments

Comments
 (0)