From 6982fa0ad2a5e08a06bb8ac186a8b77c35c22ff8 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Sat, 11 Sep 2021 11:23:30 -0400 Subject: [PATCH 1/2] TYP: SparseArray --- pandas/core/arrays/sparse/array.py | 32 ++++++++++++++++++------------ pandas/core/arrays/sparse/dtype.py | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 77142ef450487..688e90cce4da6 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -98,6 +98,8 @@ class ellipsis(Enum): from pandas._typing import NumpySorter + from pandas import Series + else: ellipsis = type(Ellipsis) @@ -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): @@ -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 @@ -606,7 +608,7 @@ def fill_value(self, value): self._dtype = SparseDtype(self.dtype.subtype, value) @property - def kind(self) -> str: + def kind(self): """ The kind of sparse index for this array. One of {'integer', 'block'}. """ @@ -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: @@ -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() @@ -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. @@ -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: @@ -1103,7 +1109,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. diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index a8f8f10e8716d..6684e559b6f88 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -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 From 07497651b4de85b0d71961ddd63559ff847d48d6 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Sat, 11 Sep 2021 11:25:47 -0400 Subject: [PATCH 2/2] Fix accidental removal --- pandas/core/arrays/sparse/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 688e90cce4da6..58adcc4e31c44 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -608,7 +608,7 @@ def fill_value(self, value): self._dtype = SparseDtype(self.dtype.subtype, value) @property - def kind(self): + def kind(self) -> str: """ The kind of sparse index for this array. One of {'integer', 'block'}. """