Skip to content

TYP: SparseIndexKind #43536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class ellipsis(Enum):

Ellipsis = ellipsis.Ellipsis

SparseIndexKind = Literal["integer", "block"]

from pandas._typing import NumpySorter

from pandas import Series
Expand Down Expand Up @@ -334,7 +336,7 @@ def __init__(
sparse_index=None,
index=None,
fill_value=None,
kind="integer",
kind: SparseIndexKind = "integer",
dtype: Dtype | None = None,
copy: bool = False,
):
Expand Down Expand Up @@ -624,7 +626,7 @@ def fill_value(self, value):
self._dtype = SparseDtype(self.dtype.subtype, value)

@property
def kind(self) -> str:
def kind(self) -> SparseIndexKind:
"""
The kind of sparse index for this array. One of {'integer', 'block'}.
"""
Expand Down Expand Up @@ -1630,7 +1632,10 @@ def _formatter(self, boxed=False):


def make_sparse(
arr: np.ndarray, kind="block", fill_value=None, dtype: NpDtype | None = None
arr: np.ndarray,
kind: SparseIndexKind = "block",
fill_value=None,
dtype: NpDtype | None = None,
):
"""
Convert ndarray to sparse format
Expand Down Expand Up @@ -1689,8 +1694,17 @@ def make_sparse(
return sparsified_values, index, fill_value


def make_sparse_index(length, indices, kind) -> SparseIndex:
@overload
def make_sparse_index(length: int, indices, kind: Literal["block"]) -> BlockIndex:
...


@overload
def make_sparse_index(length: int, indices, kind: Literal["integer"]) -> IntIndex:
...


def make_sparse_index(length: int, indices, kind: SparseIndexKind) -> SparseIndex:
index: SparseIndex
if kind == "block" or isinstance(kind, BlockIndex):
locs, lens = splib.get_blocks(indices)
Expand Down