Skip to content

Commit

Permalink
TYP: SparseArray (#43513)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeitlin11 authored Sep 12, 2021
1 parent 3b55364 commit 1c6c767
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class ellipsis(Enum):

from pandas._typing import NumpySorter

from pandas import Series

else:
ellipsis = type(Ellipsis)

Expand Down Expand Up @@ -328,7 +330,7 @@ def __init__(
fill_value=None,
kind="integer",
dtype: Dtype | None = None,
copy=False,
copy: bool = False,
):

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

@classmethod
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = False):
return cls(scalars, dtype=dtype)

@classmethod
Expand Down Expand Up @@ -625,10 +627,10 @@ def __len__(self) -> int:
return self.sp_index.length

@property
def _null_fill_value(self):
def _null_fill_value(self) -> bool:
return self._dtype._is_na_fill_value

def _fill_value_matches(self, fill_value):
def _fill_value_matches(self, fill_value) -> bool:
if self._null_fill_value:
return isna(fill_value)
else:
Expand Down Expand Up @@ -725,7 +727,7 @@ def fillna(self, value=None, method=None, limit=None):

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

def shift(self, periods=1, fill_value=None):
def shift(self, periods: int = 1, fill_value=None):

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

def value_counts(self, dropna: bool = True):
def value_counts(self, dropna: bool = True) -> Series:
"""
Returns a Series containing counts of unique values.
Expand Down Expand Up @@ -909,24 +911,28 @@ def _get_val_at(self, loc):
val = maybe_box_datetimelike(val, self.sp_values.dtype)
return val

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

dtype = None
if indices.size == 0:
result = np.array([], dtype="object")
kwargs = {"dtype": self.dtype}
dtype = self.dtype
elif allow_fill:
result = self._take_with_fill(indices, fill_value=fill_value)
kwargs = {}
else:
# error: Incompatible types in assignment (expression has type
# "Union[ndarray, SparseArray]", variable has type "ndarray")
result = self._take_without_fill(indices) # type: ignore[assignment]
kwargs = {"dtype": self.dtype}
dtype = self.dtype

return type(self)(result, fill_value=self.fill_value, kind=self.kind, **kwargs)
return type(self)(
result, fill_value=self.fill_value, kind=self.kind, dtype=dtype
)

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

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

def astype(self, dtype: AstypeArg | None = None, copy=True):
def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
"""
Change the dtype of a SparseArray.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def fill_value(self):
return self._fill_value

@property
def _is_na_fill_value(self):
def _is_na_fill_value(self) -> bool:
return isna(self.fill_value)

@property
Expand Down

0 comments on commit 1c6c767

Please sign in to comment.