Skip to content

Commit 0354147

Browse files
committed
TYP: Typing fixes and improvements related to scipy.sparse
1 parent 7bfef3b commit 0354147

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

pandas/_libs/sparse.pyi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from collections.abc import Sequence
21
from typing import Self
32

43
import numpy as np
54

6-
from pandas._typing import npt
5+
from pandas._typing import (
6+
TakeIndexer,
7+
npt,
8+
)
79

810
class SparseIndex:
911
length: int
@@ -26,7 +28,7 @@ class SparseIndex:
2628
class IntIndex(SparseIndex):
2729
indices: npt.NDArray[np.int32]
2830
def __init__(
29-
self, length: int, indices: Sequence[int], check_integrity: bool = ...
31+
self, length: int, indices: TakeIndexer, check_integrity: bool = ...
3032
) -> None: ...
3133

3234
class BlockIndex(SparseIndex):

pandas/core/arrays/sparse/accessor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,10 @@ def to_coo(self) -> spmatrix:
440440
rows.append(row)
441441
data.append(sp_arr.sp_values.astype(dtype, copy=False))
442442

443-
cols = np.concatenate(cols)
444-
rows = np.concatenate(rows)
445-
data = np.concatenate(data)
446-
return coo_matrix((data, (rows, cols)), shape=self._parent.shape)
443+
cols_arr = np.concatenate(cols)
444+
rows_arr = np.concatenate(rows)
445+
data_arr = np.concatenate(data)
446+
return coo_matrix((data_arr, (rows_arr, cols_arr)), shape=self._parent.shape)
447447

448448
@property
449449
def density(self) -> float:

pandas/core/arrays/sparse/array.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,26 @@
9292
Sequence,
9393
)
9494
from enum import Enum
95+
from typing import (
96+
Protocol,
97+
type_check_only,
98+
)
9599

96100
class ellipsis(Enum):
97101
Ellipsis = "..."
98102

99103
Ellipsis = ellipsis.Ellipsis
100104

101-
from scipy.sparse import spmatrix
105+
from scipy.sparse import (
106+
csc_array,
107+
csc_matrix,
108+
)
109+
110+
@type_check_only
111+
class _SparseMatrixLike(Protocol):
112+
@property
113+
def shape(self, /) -> tuple[int, int]: ...
114+
def tocsc(self, /) -> csc_array | csc_matrix: ...
102115

103116
from pandas._typing import NumpySorter
104117

@@ -120,6 +133,7 @@ class ellipsis(Enum):
120133

121134
from pandas import Series
122135

136+
123137
else:
124138
ellipsis = type(Ellipsis)
125139

@@ -511,7 +525,7 @@ def _simple_new(
511525
return new
512526

513527
@classmethod
514-
def from_spmatrix(cls, data: spmatrix) -> Self:
528+
def from_spmatrix(cls, data: _SparseMatrixLike) -> Self:
515529
"""
516530
Create a SparseArray from a scipy.sparse matrix.
517531
@@ -543,10 +557,10 @@ def from_spmatrix(cls, data: spmatrix) -> Self:
543557

544558
# our sparse index classes require that the positions be strictly
545559
# increasing. So we need to sort loc, and arr accordingly.
546-
data = data.tocsc()
547-
data.sort_indices()
548-
arr = data.data
549-
idx = data.indices
560+
data_csc = data.tocsc()
561+
data_csc.sort_indices()
562+
arr = data_csc.data
563+
idx = data_csc.indices
550564

551565
zero = np.array(0, dtype=arr.dtype).item()
552566
dtype = SparseDtype(arr.dtype, zero)
@@ -1217,10 +1231,7 @@ def _concat_same_type(cls, to_concat: Sequence[Self]) -> Self:
12171231

12181232
data = np.concatenate(values)
12191233
indices_arr = np.concatenate(indices)
1220-
# error: Argument 2 to "IntIndex" has incompatible type
1221-
# "ndarray[Any, dtype[signedinteger[_32Bit]]]";
1222-
# expected "Sequence[int]"
1223-
sp_index = IntIndex(length, indices_arr) # type: ignore[arg-type]
1234+
sp_index = IntIndex(length, indices_arr)
12241235

12251236
else:
12261237
# when concatenating block indices, we don't claim that you'll

pandas/tests/arrays/sparse/test_libsparse.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,10 @@ def test_intersect_empty(self):
209209
@pytest.mark.parametrize(
210210
"case",
211211
[
212-
# Argument 2 to "IntIndex" has incompatible type "ndarray[Any,
213-
# dtype[signedinteger[_32Bit]]]"; expected "Sequence[int]"
214-
IntIndex(5, np.array([1, 2], dtype=np.int32)), # type: ignore[arg-type]
215-
IntIndex(5, np.array([0, 2, 4], dtype=np.int32)), # type: ignore[arg-type]
216-
IntIndex(0, np.array([], dtype=np.int32)), # type: ignore[arg-type]
217-
IntIndex(5, np.array([], dtype=np.int32)), # type: ignore[arg-type]
212+
IntIndex(5, np.array([1, 2], dtype=np.int32)),
213+
IntIndex(5, np.array([0, 2, 4], dtype=np.int32)),
214+
IntIndex(0, np.array([], dtype=np.int32)),
215+
IntIndex(5, np.array([], dtype=np.int32)),
218216
],
219217
)
220218
def test_intersect_identical(self, case):

0 commit comments

Comments
 (0)