Skip to content

Commit 1c6c767

Browse files
authored
TYP: SparseArray (#43513)
1 parent 3b55364 commit 1c6c767

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

pandas/core/arrays/sparse/array.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class ellipsis(Enum):
9898

9999
from pandas._typing import NumpySorter
100100

101+
from pandas import Series
102+
101103
else:
102104
ellipsis = type(Ellipsis)
103105

@@ -328,7 +330,7 @@ def __init__(
328330
fill_value=None,
329331
kind="integer",
330332
dtype: Dtype | None = None,
331-
copy=False,
333+
copy: bool = False,
332334
):
333335

334336
if fill_value is None and isinstance(dtype, SparseDtype):
@@ -558,7 +560,7 @@ def __setitem__(self, key, value):
558560
raise TypeError(msg)
559561

560562
@classmethod
561-
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
563+
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = False):
562564
return cls(scalars, dtype=dtype)
563565

564566
@classmethod
@@ -625,10 +627,10 @@ def __len__(self) -> int:
625627
return self.sp_index.length
626628

627629
@property
628-
def _null_fill_value(self):
630+
def _null_fill_value(self) -> bool:
629631
return self._dtype._is_na_fill_value
630632

631-
def _fill_value_matches(self, fill_value):
633+
def _fill_value_matches(self, fill_value) -> bool:
632634
if self._null_fill_value:
633635
return isna(fill_value)
634636
else:
@@ -725,7 +727,7 @@ def fillna(self, value=None, method=None, limit=None):
725727

726728
return self._simple_new(new_values, self._sparse_index, new_dtype)
727729

728-
def shift(self, periods=1, fill_value=None):
730+
def shift(self, periods: int = 1, fill_value=None):
729731

730732
if not len(self) or periods == 0:
731733
return self.copy()
@@ -794,7 +796,7 @@ def factorize(self, na_sentinel=-1):
794796
uniques = SparseArray(uniques, dtype=self.dtype) # type: ignore[assignment]
795797
return codes, uniques
796798

797-
def value_counts(self, dropna: bool = True):
799+
def value_counts(self, dropna: bool = True) -> Series:
798800
"""
799801
Returns a Series containing counts of unique values.
800802
@@ -909,24 +911,28 @@ def _get_val_at(self, loc):
909911
val = maybe_box_datetimelike(val, self.sp_values.dtype)
910912
return val
911913

912-
def take(self, indices, *, allow_fill=False, fill_value=None) -> SparseArray:
914+
def take(
915+
self, indices, *, allow_fill: bool = False, fill_value=None
916+
) -> SparseArray:
913917
if is_scalar(indices):
914918
raise ValueError(f"'indices' must be an array, not a scalar '{indices}'.")
915919
indices = np.asarray(indices, dtype=np.int32)
916920

921+
dtype = None
917922
if indices.size == 0:
918923
result = np.array([], dtype="object")
919-
kwargs = {"dtype": self.dtype}
924+
dtype = self.dtype
920925
elif allow_fill:
921926
result = self._take_with_fill(indices, fill_value=fill_value)
922-
kwargs = {}
923927
else:
924928
# error: Incompatible types in assignment (expression has type
925929
# "Union[ndarray, SparseArray]", variable has type "ndarray")
926930
result = self._take_without_fill(indices) # type: ignore[assignment]
927-
kwargs = {"dtype": self.dtype}
931+
dtype = self.dtype
928932

929-
return type(self)(result, fill_value=self.fill_value, kind=self.kind, **kwargs)
933+
return type(self)(
934+
result, fill_value=self.fill_value, kind=self.kind, dtype=dtype
935+
)
930936

931937
def _take_with_fill(self, indices, fill_value=None) -> np.ndarray:
932938
if fill_value is None:
@@ -1104,7 +1110,7 @@ def _concat_same_type(
11041110

11051111
return cls(data, sparse_index=sp_index, fill_value=fill_value)
11061112

1107-
def astype(self, dtype: AstypeArg | None = None, copy=True):
1113+
def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
11081114
"""
11091115
Change the dtype of a SparseArray.
11101116

pandas/core/arrays/sparse/dtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def fill_value(self):
149149
return self._fill_value
150150

151151
@property
152-
def _is_na_fill_value(self):
152+
def _is_na_fill_value(self) -> bool:
153153
return isna(self.fill_value)
154154

155155
@property

0 commit comments

Comments
 (0)