@@ -20,7 +20,6 @@ cdef class BlockPlacement:
20
20
cdef:
21
21
slice _as_slice
22
22
object _as_array
23
-
24
23
bint _has_slice, _has_array, _is_known_slice_like
25
24
26
25
def __init__ (self , val ):
@@ -56,19 +55,21 @@ cdef class BlockPlacement:
56
55
def __str__ (self ) -> str:
57
56
cdef:
58
57
slice s = self ._ensure_has_slice()
58
+
59
59
if s is not None:
60
60
v = self ._as_slice
61
61
else:
62
62
v = self ._as_array
63
63
64
- return f' {type(self ).__name__}({v})'
64
+ return f" {type(self ).__name__}({v})"
65
65
66
66
def __repr__(self ) -> str:
67
67
return str(self )
68
68
69
69
def __len__(self ) -> int:
70
70
cdef:
71
71
slice s = self ._ensure_has_slice()
72
+
72
73
if s is not None:
73
74
return slice_len(s )
74
75
else:
@@ -78,6 +79,7 @@ cdef class BlockPlacement:
78
79
cdef:
79
80
slice s = self ._ensure_has_slice()
80
81
Py_ssize_t start, stop, step, _
82
+
81
83
if s is not None :
82
84
start, stop, step, _ = slice_get_indices_ex(s)
83
85
return iter (range (start, stop, step))
@@ -88,45 +90,52 @@ cdef class BlockPlacement:
88
90
def as_slice (self ) -> slice:
89
91
cdef:
90
92
slice s = self ._ensure_has_slice()
91
- if s is None:
92
- raise TypeError('Not slice-like')
93
- else:
93
+
94
+ if s is not None:
94
95
return s
96
+ else:
97
+ raise TypeError("Not slice-like")
95
98
96
99
@property
97
100
def indexer(self ):
98
101
cdef:
99
102
slice s = self ._ensure_has_slice()
103
+
100
104
if s is not None :
101
105
return s
102
106
else :
103
107
return self ._as_array
104
108
105
109
def isin (self , arr ):
106
110
from pandas.core.indexes.api import Int64Index
111
+
107
112
return Int64Index(self .as_array, copy = False ).isin(arr)
108
113
109
114
@property
110
115
def as_array (self ):
111
116
cdef:
112
117
Py_ssize_t start, stop, end, _
118
+
113
119
if not self ._has_array:
114
120
start, stop, step, _ = slice_get_indices_ex(self ._as_slice)
115
121
# NOTE: this is the C-optimized equivalent of
116
- # np.arange(start, stop, step, dtype=np.int64)
122
+ # ` np.arange(start, stop, step, dtype=np.int64)`
117
123
self ._as_array = cnp.PyArray_Arange(start, stop, step, NPY_INT64)
118
124
self ._has_array = True
125
+
119
126
return self ._as_array
120
127
121
128
@property
122
129
def is_slice_like (self ) -> bool:
123
130
cdef:
124
131
slice s = self ._ensure_has_slice()
132
+
125
133
return s is not None
126
134
127
135
def __getitem__(self , loc ):
128
136
cdef:
129
137
slice s = self ._ensure_has_slice()
138
+
130
139
if s is not None :
131
140
val = slice_getitem(s, loc)
132
141
else :
@@ -141,11 +150,12 @@ cdef class BlockPlacement:
141
150
return BlockPlacement(np.delete(self .as_array, loc, axis = 0 ))
142
151
143
152
def append (self , others ):
144
- if len (others) == 0 :
153
+ if not len (others):
145
154
return self
146
155
147
- return BlockPlacement(np.concatenate([self .as_array] +
148
- [o.as_array for o in others]))
156
+ return BlockPlacement(
157
+ np.concatenate([self .as_array] + [o.as_array for o in others])
158
+ )
149
159
150
160
cdef iadd(self , other):
151
161
cdef:
@@ -163,8 +173,7 @@ cdef class BlockPlacement:
163
173
start += other_int
164
174
stop += other_int
165
175
166
- if ((step > 0 and start < 0 ) or
167
- (step < 0 and stop < step)):
176
+ if (step > 0 and start < 0 ) or (step < 0 and stop < step):
168
177
raise ValueError (" iadd causes length change" )
169
178
170
179
if stop < 0 :
@@ -191,6 +200,7 @@ cdef class BlockPlacement:
191
200
if not self ._has_slice:
192
201
self ._as_slice = indexer_as_slice(self ._as_array)
193
202
self ._has_slice = True
203
+
194
204
return self ._as_slice
195
205
196
206
@@ -240,8 +250,7 @@ cdef slice slice_canonize(slice s):
240
250
return slice (start, stop, step)
241
251
242
252
243
- cpdef Py_ssize_t slice_len(
244
- slice slc, Py_ssize_t objlen = PY_SSIZE_T_MAX) except - 1 :
253
+ cpdef Py_ssize_t slice_len(slice slc, Py_ssize_t objlen = PY_SSIZE_T_MAX) except - 1 :
245
254
"""
246
255
Get length of a bounded slice.
247
256
@@ -258,8 +267,7 @@ cpdef Py_ssize_t slice_len(
258
267
if slc is None :
259
268
raise TypeError (" slc must be slice" )
260
269
261
- PySlice_GetIndicesEx(slc, objlen,
262
- & start, & stop, & step, & length)
270
+ PySlice_GetIndicesEx(slc, objlen, & start, & stop, & step, & length)
263
271
264
272
return length
265
273
@@ -277,8 +285,7 @@ cdef slice_get_indices_ex(slice slc, Py_ssize_t objlen=PY_SSIZE_T_MAX):
277
285
if slc is None :
278
286
raise TypeError (" slc should be a slice" )
279
287
280
- PySlice_GetIndicesEx(slc, objlen,
281
- & start, & stop, & step, & length)
288
+ PySlice_GetIndicesEx(slc, objlen, & start, & stop, & step, & length)
282
289
283
290
return start, stop, step, length
284
291
@@ -378,8 +385,7 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True):
378
385
# blockno handling.
379
386
cdef:
380
387
int64_t cur_blkno
381
- Py_ssize_t i, start, stop, n, diff
382
-
388
+ Py_ssize_t i, start, stop, n, diff, tot_len
383
389
object blkno
384
390
object group_dict = defaultdict(list )
385
391
0 commit comments