From 7b173bc2d66c604c99cb41e440958f9e876a7bda Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 27 Nov 2019 19:41:18 +0000 Subject: [PATCH 1/2] TYP: some types for pandas/core/arrays/sparse/array.py --- pandas/core/arrays/sparse/array.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 943dea4252499..16603a943885e 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -4,7 +4,7 @@ from collections import abc import numbers import operator -from typing import Any, Callable +from typing import Any, Callable, List, Type, TypeVar import warnings import numpy as np @@ -50,6 +50,8 @@ from .dtype import SparseDtype +_SparseArrayT = TypeVar("_SparseArrayT", bound="SparseArray") + # ---------------------------------------------------------------------------- # Array @@ -260,6 +262,7 @@ class SparseArray(PandasObject, ExtensionArray, ExtensionOpsMixin): _pandas_ftype = "sparse" _subtyp = "sparse_array" # register ABCSparseArray _deprecations = PandasObject._deprecations | frozenset(["get_values"]) + _sparse_index: SparseIndex def __init__( self, @@ -372,8 +375,11 @@ def __init__( @classmethod def _simple_new( - cls, sparse_array: np.ndarray, sparse_index: SparseIndex, dtype: SparseDtype - ) -> ABCSparseArray: + cls: Type[_SparseArrayT], + sparse_array: np.ndarray, + sparse_index: SparseIndex, + dtype: SparseDtype, + ) -> _SparseArrayT: new = cls([]) new._sparse_index = sparse_index new._sparse_values = sparse_array @@ -805,13 +811,14 @@ def _get_val_at(self, loc): else: return libindex.get_value_at(self.sp_values, sp_loc) - def take(self, indices, allow_fill=False, fill_value=None): + def take(self, indices, allow_fill: bool = False, fill_value=None): if is_scalar(indices): raise ValueError( "'indices' must be an array, not a scalar '{}'.".format(indices) ) indices = np.asarray(indices, dtype=np.int32) + result: List if indices.size == 0: result = [] kwargs = {"dtype": self.dtype} @@ -1392,8 +1399,10 @@ def __abs__(self): # ------------------------------------------------------------------------ @classmethod - def _create_unary_method(cls, op): - def sparse_unary_method(self): + def _create_unary_method( + cls: Type[_SparseArrayT], op + ) -> Callable[[_SparseArrayT], _SparseArrayT]: + def sparse_unary_method(self: _SparseArrayT) -> _SparseArrayT: fill_value = op(np.array(self.fill_value)).item() values = op(self.sp_values) dtype = SparseDtype(values.dtype, fill_value) From 8c3860f7e31aff3760c192034a8440103264ec8d Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 27 Nov 2019 20:53:21 +0000 Subject: [PATCH 2/2] update per comments --- pandas/core/arrays/sparse/array.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 16603a943885e..593ba7a643193 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -4,7 +4,7 @@ from collections import abc import numbers import operator -from typing import Any, Callable, List, Type, TypeVar +from typing import Any, Callable import warnings import numpy as np @@ -50,8 +50,6 @@ from .dtype import SparseDtype -_SparseArrayT = TypeVar("_SparseArrayT", bound="SparseArray") - # ---------------------------------------------------------------------------- # Array @@ -375,11 +373,8 @@ def __init__( @classmethod def _simple_new( - cls: Type[_SparseArrayT], - sparse_array: np.ndarray, - sparse_index: SparseIndex, - dtype: SparseDtype, - ) -> _SparseArrayT: + cls, sparse_array: np.ndarray, sparse_index: SparseIndex, dtype: SparseDtype, + ) -> "SparseArray": new = cls([]) new._sparse_index = sparse_index new._sparse_values = sparse_array @@ -811,14 +806,13 @@ def _get_val_at(self, loc): else: return libindex.get_value_at(self.sp_values, sp_loc) - def take(self, indices, allow_fill: bool = False, fill_value=None): + def take(self, indices, allow_fill=False, fill_value=None): if is_scalar(indices): raise ValueError( "'indices' must be an array, not a scalar '{}'.".format(indices) ) indices = np.asarray(indices, dtype=np.int32) - result: List if indices.size == 0: result = [] kwargs = {"dtype": self.dtype} @@ -1399,10 +1393,8 @@ def __abs__(self): # ------------------------------------------------------------------------ @classmethod - def _create_unary_method( - cls: Type[_SparseArrayT], op - ) -> Callable[[_SparseArrayT], _SparseArrayT]: - def sparse_unary_method(self: _SparseArrayT) -> _SparseArrayT: + def _create_unary_method(cls, op) -> Callable[["SparseArray"], "SparseArray"]: + def sparse_unary_method(self) -> "SparseArray": fill_value = op(np.array(self.fill_value)).item() values = op(self.sp_values) dtype = SparseDtype(values.dtype, fill_value)