Skip to content

Commit ca45cb5

Browse files
mzeitlin11AlexeyGy
authored andcommitted
TYP: SparseIndexKind (pandas-dev#43536)
1 parent c7b7080 commit ca45cb5

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pandas/core/arrays/sparse/array.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class ellipsis(Enum):
9797

9898
Ellipsis = ellipsis.Ellipsis
9999

100+
SparseIndexKind = Literal["integer", "block"]
101+
100102
from pandas._typing import NumpySorter
101103

102104
from pandas import Series
@@ -334,7 +336,7 @@ def __init__(
334336
sparse_index=None,
335337
index=None,
336338
fill_value=None,
337-
kind="integer",
339+
kind: SparseIndexKind = "integer",
338340
dtype: Dtype | None = None,
339341
copy: bool = False,
340342
):
@@ -624,7 +626,7 @@ def fill_value(self, value):
624626
self._dtype = SparseDtype(self.dtype.subtype, value)
625627

626628
@property
627-
def kind(self) -> str:
629+
def kind(self) -> SparseIndexKind:
628630
"""
629631
The kind of sparse index for this array. One of {'integer', 'block'}.
630632
"""
@@ -1676,7 +1678,10 @@ def _formatter(self, boxed=False):
16761678

16771679

16781680
def make_sparse(
1679-
arr: np.ndarray, kind="block", fill_value=None, dtype: NpDtype | None = None
1681+
arr: np.ndarray,
1682+
kind: SparseIndexKind = "block",
1683+
fill_value=None,
1684+
dtype: NpDtype | None = None,
16801685
):
16811686
"""
16821687
Convert ndarray to sparse format
@@ -1735,8 +1740,17 @@ def make_sparse(
17351740
return sparsified_values, index, fill_value
17361741

17371742

1738-
def make_sparse_index(length, indices, kind) -> SparseIndex:
1743+
@overload
1744+
def make_sparse_index(length: int, indices, kind: Literal["block"]) -> BlockIndex:
1745+
...
1746+
1747+
1748+
@overload
1749+
def make_sparse_index(length: int, indices, kind: Literal["integer"]) -> IntIndex:
1750+
...
1751+
17391752

1753+
def make_sparse_index(length: int, indices, kind: SparseIndexKind) -> SparseIndex:
17401754
index: SparseIndex
17411755
if kind == "block" or isinstance(kind, BlockIndex):
17421756
locs, lens = splib.get_blocks(indices)

0 commit comments

Comments
 (0)