From ea9c2b3396458f80f2c590c01b430a0833893226 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 21 Mar 2019 22:45:38 -0500 Subject: [PATCH] #25790 Updating type hints to Python3 syntax in pandas/core/array --- pandas/core/arrays/array_.py | 15 ++++--- pandas/core/arrays/base.py | 69 +++++++++++++++--------------- pandas/core/arrays/datetimelike.py | 34 ++++++++------- pandas/core/arrays/datetimes.py | 3 +- pandas/core/arrays/integer.py | 6 +-- pandas/core/arrays/period.py | 32 +++++++------- pandas/core/arrays/sparse.py | 26 +++++++---- 7 files changed, 99 insertions(+), 86 deletions(-) diff --git a/pandas/core/arrays/array_.py b/pandas/core/arrays/array_.py index 254ab876af1ac..9ab703c863f31 100644 --- a/pandas/core/arrays/array_.py +++ b/pandas/core/arrays/array_.py @@ -1,4 +1,4 @@ -from typing import Optional, Sequence, Union +from typing import Optional, Sequence, TYPE_CHECKING, Union import numpy as np @@ -11,11 +11,14 @@ from pandas import compat -def array(data, # type: Sequence[object] - dtype=None, # type: Optional[Union[str, np.dtype, ExtensionDtype]] - copy=True, # type: bool - ): - # type: (...) -> ExtensionArray +if TYPE_CHECKING: + from pandas.core.arrays.base import ExtensionArray + + +def array(data: Sequence[object], + dtype: Optional[Union[str, np.dtype, ExtensionDtype]] = None, + copy: bool = True, + ) -> 'ExtensionArray': """ Create an array. diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index f7d427ce26e6a..127cc43406c28 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -214,8 +214,7 @@ def __getitem__(self, item): """ raise AbstractMethodError(self) - def __setitem__(self, key, value): - # type: (Union[int, np.ndarray], Any) -> None + def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None: """ Set one or more values inplace. @@ -262,8 +261,7 @@ def __setitem__(self, key, value): type(self), '__setitem__') ) - def __len__(self): - # type: () -> int + def __len__(self) -> int: """ Length of this array @@ -287,32 +285,28 @@ def __iter__(self): # Required attributes # ------------------------------------------------------------------------ @property - def dtype(self): - # type: () -> ExtensionDtype + def dtype(self) -> ExtensionDtype: """ An instance of 'ExtensionDtype'. """ raise AbstractMethodError(self) @property - def shape(self): - # type: () -> Tuple[int, ...] + def shape(self) -> Tuple[int, ...]: """ Return a tuple of the array dimensions. """ return (len(self),) @property - def ndim(self): - # type: () -> int + def ndim(self) -> int: """ Extension Arrays are only allowed to be 1-dimensional. """ return 1 @property - def nbytes(self): - # type: () -> int + def nbytes(self) -> int: """ The number of bytes needed to store this object in memory. """ @@ -343,8 +337,7 @@ def astype(self, dtype, copy=True): """ return np.array(self, dtype=dtype, copy=copy) - def isna(self): - # type: () -> Union[ExtensionArray, np.ndarray] + def isna(self) -> Union['ExtensionArray', np.ndarray]: """ A 1-D array indicating if each value is missing. @@ -366,8 +359,7 @@ def isna(self): """ raise AbstractMethodError(self) - def _values_for_argsort(self): - # type: () -> np.ndarray + def _values_for_argsort(self) -> np.ndarray: """ Return values for sorting. @@ -482,8 +474,11 @@ def dropna(self): """ return self[~self.isna()] - def shift(self, periods=1, fill_value=None): - # type: (int, object) -> ExtensionArray + def shift( + self, + periods: int = 1, + fill_value: object = None, + ) -> 'ExtensionArray': """ Shift values by desired number. @@ -598,8 +593,7 @@ def searchsorted(self, value, side="left", sorter=None): arr = self.astype(object) return arr.searchsorted(value, side=side, sorter=sorter) - def _values_for_factorize(self): - # type: () -> Tuple[np.ndarray, Any] + def _values_for_factorize(self) -> Tuple[np.ndarray, Any]: """ Return an array and missing value suitable for factorization. @@ -623,8 +617,10 @@ def _values_for_factorize(self): """ return self.astype(object), np.nan - def factorize(self, na_sentinel=-1): - # type: (int) -> Tuple[np.ndarray, ExtensionArray] + def factorize( + self, + na_sentinel: int = -1, + ) -> Tuple[np.ndarray, 'ExtensionArray']: """ Encode the extension array as an enumerated type. @@ -726,8 +722,12 @@ def repeat(self, repeats, axis=None): # Indexing methods # ------------------------------------------------------------------------ - def take(self, indices, allow_fill=False, fill_value=None): - # type: (Sequence[int], bool, Optional[Any]) -> ExtensionArray + def take( + self, + indices: Sequence[int], + allow_fill: bool = False, + fill_value: Any = None + ) -> 'ExtensionArray': """ Take elements from an array. @@ -816,8 +816,7 @@ def take(self, indices, allow_fill=False, fill_value=None): # pandas.api.extensions.take raise AbstractMethodError(self) - def copy(self, deep=False): - # type: (bool) -> ExtensionArray + def copy(self, deep: bool = False) -> 'ExtensionArray': """ Return a copy of the array. @@ -853,8 +852,10 @@ def __repr__(self): length=len(self), dtype=self.dtype) - def _formatter(self, boxed=False): - # type: (bool) -> Callable[[Any], Optional[str]] + def _formatter( + self, + boxed: bool = False, + ) -> Callable[[Any], Optional[str]]: """Formatting function for scalar values. This is used in the default '__repr__'. The returned formatting @@ -881,8 +882,7 @@ def _formatter(self, boxed=False): return str return repr - def _formatting_values(self): - # type: () -> np.ndarray + def _formatting_values(self) -> np.ndarray: # At the moment, this has to be an array since we use result.dtype """ An array of values to be printed in, e.g. the Series repr @@ -898,8 +898,10 @@ def _formatting_values(self): # ------------------------------------------------------------------------ @classmethod - def _concat_same_type(cls, to_concat): - # type: (Sequence[ExtensionArray]) -> ExtensionArray + def _concat_same_type( + cls, + to_concat: Sequence['ExtensionArray'] + ) -> 'ExtensionArray': """ Concatenate multiple array @@ -921,8 +923,7 @@ def _concat_same_type(cls, to_concat): _can_hold_na = True @property - def _ndarray_values(self): - # type: () -> np.ndarray + def _ndarray_values(self) -> np.ndarray: """ Internal pandas method for lossy conversion to a NumPy ndarray. diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 27d7d4f888550..9e6602daa92bb 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -58,8 +58,7 @@ def _get_attributes_dict(self): return {k: getattr(self, k, None) for k in self._attributes} @property - def _scalar_type(self): - # type: () -> Union[type, Tuple[type]] + def _scalar_type(self) -> Union[type, Tuple[type]]: """The scalar associated with this datelike * PeriodArray : Period @@ -68,8 +67,10 @@ def _scalar_type(self): """ raise AbstractMethodError(self) - def _scalar_from_string(self, value): - # type: (str) -> Union[Period, Timestamp, Timedelta, NaTType] + def _scalar_from_string( + self, + value: str, + ) -> Union[Period, Timestamp, Timedelta, NaTType]: """ Construct a scalar type from a string. @@ -89,8 +90,10 @@ def _scalar_from_string(self, value): """ raise AbstractMethodError(self) - def _unbox_scalar(self, value): - # type: (Union[Period, Timestamp, Timedelta, NaTType]) -> int + def _unbox_scalar( + self, + value: Union[Period, Timestamp, Timedelta, NaTType], + ) -> int: """ Unbox the integer value of a scalar `value`. @@ -109,8 +112,10 @@ def _unbox_scalar(self, value): """ raise AbstractMethodError(self) - def _check_compatible_with(self, other): - # type: (Union[Period, Timestamp, Timedelta, NaTType]) -> None + def _check_compatible_with( + self, + other: Union[Period, Timestamp, Timedelta, NaTType], + ) -> None: """ Verify that `self` and `other` are compatible. @@ -350,8 +355,7 @@ def __iter__(self): return (self._box_func(v) for v in self.asi8) @property - def asi8(self): - # type: () -> np.ndarray + def asi8(self) -> np.ndarray: """ Integer representation of the values. @@ -402,8 +406,7 @@ def shape(self): return (len(self),) @property - def size(self): - # type: () -> int + def size(self) -> int: """The number of elements in this array.""" return np.prod(self.shape) @@ -461,10 +464,9 @@ def __getitem__(self, key): def __setitem__( self, - key, # type: Union[int, Sequence[int], Sequence[bool], slice] - value, # type: Union[NaTType, Any, Sequence[Any]] - ): - # type: (...) -> None + key: Union[int, Sequence[int], Sequence[bool], slice], + value: Union[NaTType, Any, Sequence[Any]] + ) -> None: # I'm fudging the types a bit here. "Any" above really depends # on type(self). For PeriodArray, it's Period (or stuff coercible # to a period in from_sequence). For DatetimeArray, it's Timestamp... diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 33e6674389e7c..a718cb1cdba65 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -514,8 +514,7 @@ def _box_func(self): return lambda x: Timestamp(x, freq=self.freq, tz=self.tz) @property - def dtype(self): - # type: () -> Union[np.dtype, DatetimeTZDtype] + def dtype(self) -> Union[np.dtype, DatetimeTZDtype]: """ The dtype for the DatetimeArray. diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 0144d04c6e197..f192ff22aedf6 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -452,8 +452,7 @@ def astype(self, dtype, copy=True): return astype_nansafe(data, dtype, copy=None) @property - def _ndarray_values(self): - # type: () -> np.ndarray + def _ndarray_values(self) -> np.ndarray: """Internal pandas method for lossy conversion to a NumPy ndarray. This method is not part of the pandas interface. @@ -509,8 +508,7 @@ def value_counts(self, dropna=True): return Series(array, index=index) - def _values_for_argsort(self): - # type: () -> np.ndarray + def _values_for_argsort(self) -> np.ndarray: """Return values for sorting. Returns diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 55d12d444fb45..ccc28ddec0714 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -183,8 +183,12 @@ def _simple_new(cls, values, freq=None, **kwargs): return cls(values, freq=freq, **kwargs) @classmethod - def _from_sequence(cls, scalars, dtype=None, copy=False): - # type: (Sequence[Optional[Period]], PeriodDtype, bool) -> PeriodArray + def _from_sequence( + cls, + scalars: Sequence[Optional[Period]], + dtype: PeriodDtype = None, + copy: bool = False, + ) -> 'PeriodArray': if dtype: freq = dtype.freq else: @@ -246,8 +250,7 @@ def _generate_range(cls, start, end, periods, freq, fields): # ----------------------------------------------------------------- # DatetimeLike Interface - def _unbox_scalar(self, value): - # type: (Union[Period, NaTType]) -> int + def _unbox_scalar(self, value: Union[Period, NaTType]) -> int: if value is NaT: return value.value elif isinstance(value, self._scalar_type): @@ -258,8 +261,7 @@ def _unbox_scalar(self, value): raise ValueError("'value' should be a Period. Got '{val}' instead." .format(val=value)) - def _scalar_from_string(self, value): - # type: (str) -> Period + def _scalar_from_string(self, value: str) -> Period: return Period(value, freq=self.freq) def _check_compatible_with(self, other): @@ -540,14 +542,9 @@ def _sub_period(self, other): @Appender(dtl.DatetimeLikeArrayMixin._addsub_int_array.__doc__) def _addsub_int_array( self, - other, # type: Union[ExtensionArray, np.ndarray[int]] - op # type: Callable[Any, Any] - ): - # type: (...) -> PeriodArray - - # TODO: ABCIndexClass is a valid type for other but had to be excluded - # due to length of Py2 compatability comment; add back in once migrated - # to Py3 syntax + other: Union[ExtensionArray, np.ndarray, ABCIndexClass], + op: Callable[[Any], Any] + ) -> 'PeriodArray': assert op in [operator.add, operator.sub] if op is operator.sub: other = -other @@ -716,8 +713,11 @@ def _raise_on_incompatible(left, right): # ------------------------------------------------------------------- # Constructor Helpers -def period_array(data, freq=None, copy=False): - # type: (Sequence[Optional[Period]], Optional[Tick], bool) -> PeriodArray +def period_array( + data: Sequence[Optional[Period]], + freq: Optional[Tick] = None, + copy: bool = False, +) -> PeriodArray: """ Construct a new PeriodArray from a sequence of Period scalars. diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 8a4422120b7a3..f478a6e58367d 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -79,8 +79,11 @@ class SparseDtype(ExtensionDtype): # hash(nan) is (sometimes?) 0. _metadata = ('_dtype', '_fill_value', '_is_na_fill_value') - def __init__(self, dtype=np.float64, fill_value=None): - # type: (Union[str, np.dtype, 'ExtensionDtype', type], Any) -> None + def __init__( + self, + dtype: Union[str, np.dtype, 'ExtensionDtype', type] = np.float64, + fill_value: Any = None + ) -> None: from pandas.core.dtypes.missing import na_value_for_dtype from pandas.core.dtypes.common import ( pandas_dtype, is_string_dtype, is_scalar @@ -372,8 +375,7 @@ def _subtype_with_str(self): _sparray_doc_kwargs = dict(klass='SparseArray') -def _get_fill(arr): - # type: (SparseArray) -> np.ndarray +def _get_fill(arr: 'SparseArray') -> np.ndarray: """ Create a 0-dim ndarray containing the fill value @@ -397,8 +399,12 @@ def _get_fill(arr): return np.asarray(arr.fill_value) -def _sparse_array_op(left, right, op, name): - # type: (SparseArray, SparseArray, Callable, str) -> Any +def _sparse_array_op( + left: 'SparseArray', + right: 'SparseArray', + op: Callable, + name: str +) -> Any: """ Perform a binary operation between two arrays. @@ -671,8 +677,12 @@ def __init__(self, data, sparse_index=None, index=None, fill_value=None, self._dtype = SparseDtype(sparse_values.dtype, fill_value) @classmethod - def _simple_new(cls, sparse_array, sparse_index, dtype): - # type: (np.ndarray, SparseIndex, SparseDtype) -> 'SparseArray' + def _simple_new( + cls, + sparse_array: np.ndarray, + sparse_index: SparseIndex, + dtype: SparseDtype + ) -> 'SparseArray': new = cls([]) new._sparse_index = sparse_index new._sparse_values = sparse_array