Skip to content

Commit 6384462

Browse files
authored
REF: add indices prop to sparse (pandas-dev#43827)
1 parent 24ccae7 commit 6384462

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

pandas/_libs/sparse.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class SparseIndex:
1717
def ngaps(self) -> int: ...
1818
@property
1919
def nbytes(self) -> int: ...
20+
@property
21+
def indices(self) -> npt.NDArray[np.int32]: ...
2022
def equals(self, other) -> bool: ...
2123
def lookup(self, index: int) -> np.int32: ...
2224
def lookup_array(self, indexer: npt.NDArray[np.int32]) -> npt.NDArray[np.int32]: ...

pandas/_libs/sparse.pyx

+8-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ cdef class IntIndex(SparseIndex):
7272
def nbytes(self) -> int:
7373
return self.indices.nbytes
7474

75-
def check_integrity(self):
75+
cdef check_integrity(self):
7676
"""
7777
Checks the following:
7878
@@ -118,7 +118,7 @@ cdef class IntIndex(SparseIndex):
118118
def ngaps(self) -> int:
119119
return self.length - self.npoints
120120

121-
def to_int_index(self):
121+
cpdef to_int_index(self):
122122
return self
123123

124124
def to_block_index(self):
@@ -327,7 +327,7 @@ cdef class BlockIndex(SparseIndex):
327327
def ngaps(self) -> int:
328328
return self.length - self.npoints
329329

330-
cpdef check_integrity(self):
330+
cdef check_integrity(self):
331331
"""
332332
Check:
333333
- Locations are in ascending order
@@ -375,7 +375,7 @@ cdef class BlockIndex(SparseIndex):
375375
def to_block_index(self):
376376
return self
377377

378-
def to_int_index(self):
378+
cpdef to_int_index(self):
379379
cdef:
380380
int32_t i = 0, j, b
381381
int32_t offset
@@ -392,6 +392,10 @@ cdef class BlockIndex(SparseIndex):
392392

393393
return IntIndex(self.length, indices)
394394

395+
@property
396+
def indices(self):
397+
return self.to_int_index().indices
398+
395399
cpdef BlockIndex intersect(self, SparseIndex other):
396400
"""
397401
Intersect two BlockIndex objects

pandas/core/arrays/sparse/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def to_coo(self):
344344
if sp_arr.fill_value != 0:
345345
raise ValueError("fill value must be 0 when converting to COO matrix")
346346

347-
row = sp_arr.sp_index.to_int_index().indices
347+
row = sp_arr.sp_index.indices
348348
cols.append(np.repeat(col, len(row)))
349349
rows.append(row)
350350
data.append(sp_arr.sp_values.astype(dtype, copy=False))

pandas/core/arrays/sparse/array.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
574574
dtype = object
575575

576576
out = np.full(self.shape, fill_value, dtype=dtype)
577-
out[self.sp_index.to_int_index().indices] = self.sp_values
577+
out[self.sp_index.indices] = self.sp_values
578578
return out
579579

580580
def __setitem__(self, key, value):
@@ -796,7 +796,7 @@ def _first_fill_value_loc(self):
796796
if len(self) == 0 or self.sp_index.npoints == len(self):
797797
return -1
798798

799-
indices = self.sp_index.to_int_index().indices
799+
indices = self.sp_index.indices
800800
if not len(indices) or indices[0] > 0:
801801
return 0
802802

@@ -903,7 +903,7 @@ def __getitem__(
903903
if end < 0:
904904
end += len(self)
905905

906-
indices = self.sp_index.to_int_index().indices
906+
indices = self.sp_index.indices
907907
keep_inds = np.flatnonzero((indices >= start) & (indices < end))
908908
sp_vals = self.sp_values[keep_inds]
909909

@@ -1111,7 +1111,7 @@ def _concat_same_type(
11111111
indices = []
11121112

11131113
for arr in to_concat:
1114-
int_idx = arr.sp_index.to_int_index().indices.copy()
1114+
int_idx = arr.sp_index.indices.copy()
11151115
int_idx += length # TODO: wraparound
11161116
length += arr.sp_index.length
11171117

@@ -1324,9 +1324,9 @@ def __setstate__(self, state):
13241324

13251325
def nonzero(self):
13261326
if self.fill_value == 0:
1327-
return (self.sp_index.to_int_index().indices,)
1327+
return (self.sp_index.indices,)
13281328
else:
1329-
return (self.sp_index.to_int_index().indices[self.sp_values != 0],)
1329+
return (self.sp_index.indices[self.sp_values != 0],)
13301330

13311331
# ------------------------------------------------------------------------
13321332
# Reductions

0 commit comments

Comments
 (0)