Skip to content

Commit 63c631f

Browse files
authored
CLN/STY: pandas/_libs/internals.pyx (#32801)
1 parent 3415832 commit 63c631f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

pandas/_libs/internals.pyx

+25-19
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ cdef class BlockPlacement:
2020
cdef:
2121
slice _as_slice
2222
object _as_array
23-
2423
bint _has_slice, _has_array, _is_known_slice_like
2524

2625
def __init__(self, val):
@@ -56,19 +55,21 @@ cdef class BlockPlacement:
5655
def __str__(self) -> str:
5756
cdef:
5857
slice s = self._ensure_has_slice()
58+
5959
if s is not None:
6060
v = self._as_slice
6161
else:
6262
v = self._as_array
6363

64-
return f'{type(self).__name__}({v})'
64+
return f"{type(self).__name__}({v})"
6565

6666
def __repr__(self) -> str:
6767
return str(self)
6868

6969
def __len__(self) -> int:
7070
cdef:
7171
slice s = self._ensure_has_slice()
72+
7273
if s is not None:
7374
return slice_len(s)
7475
else:
@@ -78,6 +79,7 @@ cdef class BlockPlacement:
7879
cdef:
7980
slice s = self._ensure_has_slice()
8081
Py_ssize_t start, stop, step, _
82+
8183
if s is not None:
8284
start, stop, step, _ = slice_get_indices_ex(s)
8385
return iter(range(start, stop, step))
@@ -88,45 +90,52 @@ cdef class BlockPlacement:
8890
def as_slice(self) -> slice:
8991
cdef:
9092
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:
9495
return s
96+
else:
97+
raise TypeError("Not slice-like")
9598

9699
@property
97100
def indexer(self):
98101
cdef:
99102
slice s = self._ensure_has_slice()
103+
100104
if s is not None:
101105
return s
102106
else:
103107
return self._as_array
104108

105109
def isin(self, arr):
106110
from pandas.core.indexes.api import Int64Index
111+
107112
return Int64Index(self.as_array, copy=False).isin(arr)
108113

109114
@property
110115
def as_array(self):
111116
cdef:
112117
Py_ssize_t start, stop, end, _
118+
113119
if not self._has_array:
114120
start, stop, step, _ = slice_get_indices_ex(self._as_slice)
115121
# 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)`
117123
self._as_array = cnp.PyArray_Arange(start, stop, step, NPY_INT64)
118124
self._has_array = True
125+
119126
return self._as_array
120127

121128
@property
122129
def is_slice_like(self) -> bool:
123130
cdef:
124131
slice s = self._ensure_has_slice()
132+
125133
return s is not None
126134

127135
def __getitem__(self, loc):
128136
cdef:
129137
slice s = self._ensure_has_slice()
138+
130139
if s is not None:
131140
val = slice_getitem(s, loc)
132141
else:
@@ -141,11 +150,12 @@ cdef class BlockPlacement:
141150
return BlockPlacement(np.delete(self.as_array, loc, axis=0))
142151

143152
def append(self, others):
144-
if len(others) == 0:
153+
if not len(others):
145154
return self
146155

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+
)
149159

150160
cdef iadd(self, other):
151161
cdef:
@@ -163,8 +173,7 @@ cdef class BlockPlacement:
163173
start += other_int
164174
stop += other_int
165175

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):
168177
raise ValueError("iadd causes length change")
169178

170179
if stop < 0:
@@ -191,6 +200,7 @@ cdef class BlockPlacement:
191200
if not self._has_slice:
192201
self._as_slice = indexer_as_slice(self._as_array)
193202
self._has_slice = True
203+
194204
return self._as_slice
195205

196206

@@ -240,8 +250,7 @@ cdef slice slice_canonize(slice s):
240250
return slice(start, stop, step)
241251

242252

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:
245254
"""
246255
Get length of a bounded slice.
247256
@@ -258,8 +267,7 @@ cpdef Py_ssize_t slice_len(
258267
if slc is None:
259268
raise TypeError("slc must be slice")
260269

261-
PySlice_GetIndicesEx(slc, objlen,
262-
&start, &stop, &step, &length)
270+
PySlice_GetIndicesEx(slc, objlen, &start, &stop, &step, &length)
263271

264272
return length
265273

@@ -277,8 +285,7 @@ cdef slice_get_indices_ex(slice slc, Py_ssize_t objlen=PY_SSIZE_T_MAX):
277285
if slc is None:
278286
raise TypeError("slc should be a slice")
279287

280-
PySlice_GetIndicesEx(slc, objlen,
281-
&start, &stop, &step, &length)
288+
PySlice_GetIndicesEx(slc, objlen, &start, &stop, &step, &length)
282289

283290
return start, stop, step, length
284291

@@ -378,8 +385,7 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True):
378385
# blockno handling.
379386
cdef:
380387
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
383389
object blkno
384390
object group_dict = defaultdict(list)
385391

0 commit comments

Comments
 (0)