From a0b8c4cf205623a76f0e9e37bc20c73ee49e96af Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 20 Mar 2019 09:18:09 -0500 Subject: [PATCH 01/13] #25790 Updated type hints to Python3 style in files outside core/arrays/ --- pandas/core/base.py | 6 ++---- pandas/core/common.py | 3 +-- pandas/core/dtypes/base.py | 17 ++++++----------- pandas/core/frame.py | 14 ++++++++------ pandas/core/groupby/groupby.py | 14 ++++++-------- pandas/core/indexes/base.py | 3 +-- pandas/core/indexes/datetimelike.py | 5 ++--- pandas/core/indexes/period.py | 2 +- pandas/core/internals/blocks.py | 12 +++++++----- pandas/core/internals/managers.py | 4 ++-- 10 files changed, 36 insertions(+), 44 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index ce91c232bb92d..9951e9eacb1c6 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -786,8 +786,7 @@ def base(self): return self.values.base @property - def array(self): - # type: () -> ExtensionArray + def array(self) -> ExtensionArray: """ The ExtensionArray of the data backing this Series or Index. @@ -962,8 +961,7 @@ def to_numpy(self, dtype=None, copy=False): return result @property - def _ndarray_values(self): - # type: () -> np.ndarray + def _ndarray_values(self) -> np.ndarray: """ The data as an ndarray, possibly losing information. diff --git a/pandas/core/common.py b/pandas/core/common.py index 77b7b94e7a1f7..43c9aea0e0a0e 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -93,8 +93,7 @@ def maybe_box_datetimelike(value): values_from_object = lib.values_from_object -def is_bool_indexer(key): - # type: (Any) -> bool +def is_bool_indexer(key: Any) -> bool: """ Check whether `key` is a valid boolean indexer. diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 8269f8c88ffd3..7b06e20420a98 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -1,5 +1,5 @@ """Extend pandas with custom array types""" -from typing import List, Optional, Type +from typing import List, Optional import numpy as np @@ -65,8 +65,7 @@ def __ne__(self, other): return not self.__eq__(other) @property - def names(self): - # type: () -> Optional[List[str]] + def names(self) -> Optional[List[str]]: """Ordered list of field names, or None if there are no fields. This is for compatibility with NumPy arrays, and may be removed in the @@ -116,8 +115,7 @@ def is_dtype(cls, dtype): return False @property - def _is_numeric(self): - # type: () -> bool + def _is_numeric(self) -> bool: """ Whether columns with this dtype should be considered numeric. @@ -128,8 +126,7 @@ def _is_numeric(self): return False @property - def _is_boolean(self): - # type: () -> bool + def _is_boolean(self) -> bool: """ Whether this dtype should be considered boolean. @@ -212,8 +209,7 @@ def __str__(self): return self.name @property - def type(self): - # type: () -> Type + def type(self) -> bool: """ The scalar type for the array, e.g. ``int`` @@ -242,8 +238,7 @@ def kind(self): return 'O' @property - def name(self): - # type: () -> str + def name(self) -> str: """ A string identifying the data type. diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ec881c730c365..855846d722ef9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -19,7 +19,7 @@ import sys import warnings from textwrap import dedent -from typing import List, Union +from typing import List, TypeVar, Union import numpy as np import numpy.ma as ma @@ -284,6 +284,10 @@ Index(['value'], dtype='object') """ + +DF = TypeVar('DF', bound='DataFrame') + + # ----------------------------------------------------------------------- # DataFrame class @@ -6243,11 +6247,9 @@ def diff(self, periods=1, axis=0): # Function application def _gotitem(self, - key, # type: Union[str, List[str]] - ndim, # type: int - subset=None # type: Union[Series, DataFrame, None] - ): - # type: (...) -> Union[Series, DataFrame] + key: Union[str, List[str]], + ndim: int, + subset: Union[Series, DF, None] = None) -> Union[Series, DF]: """ Sub-classes to define. Return a sliced object. diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 96eafbfae2cdb..c2fad0af19b8f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1040,8 +1040,7 @@ def _bool_agg(self, val_test, skipna): Shared func to call any / all Cython GroupBy implementations. """ - def objs_to_bool(vals): - # type: (np.ndarray) -> (np.ndarray, Type) + def objs_to_bool(vals: np.ndarray) -> types.Tuple[np.ndarray, Type]: if is_object_dtype(vals): vals = np.array([bool(x) for x in vals]) else: @@ -1049,8 +1048,7 @@ def objs_to_bool(vals): return vals.view(np.uint8), np.bool - def result_to_bool(result, inference): - # type: (np.ndarray, Type) -> np.ndarray + def result_to_bool(result: np.ndarray, inference: Type) -> np.ndarray: return result.astype(inference, copy=False) return self._get_cythonized_result('group_any_all', self.grouper, @@ -1738,8 +1736,8 @@ def quantile(self, q=0.5, interpolation='linear'): b 3.0 """ - def pre_processor(vals): - # type: (np.ndarray) -> (np.ndarray, Optional[Type]) + def pre_processor(vals: np.ndarray) -> \ + types.Tuple[np.ndarray, Optional[Type]]: if is_object_dtype(vals): raise TypeError("'quantile' cannot be performed against " "'object' dtypes!") @@ -1753,8 +1751,8 @@ def pre_processor(vals): return vals, inference - def post_processor(vals, inference): - # type: (np.ndarray, Optional[Type]) -> np.ndarray + def post_processor(vals: np.ndarray, inference: Optional[Type]) -> \ + np.ndarray: if inference: # Check for edge case if not (is_integer_dtype(inference) and diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a3eed90ab5d80..ea475e335ce0d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3632,8 +3632,7 @@ def values(self): return self._data.view(np.ndarray) @property - def _values(self): - # type: () -> Union[ExtensionArray, Index, np.ndarray] + def _values(self) -> Union[ExtensionArray, Index, np.ndarray]: # TODO(EA): remove index types as they become extension arrays """ The best array representation. diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 830f234b85757..4575081f52b71 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -56,7 +56,7 @@ class DatetimeIndexOpsMixin(ExtensionOpsMixin): """ common ops mixin to support a unified interface datetimelike Index """ - _data = None # type: DatetimeLikeArrayMixin + _data: DatetimeLikeArrayMixin = None # DatetimeLikeArrayMixin assumes subclasses are mutable, so these are # properties there. They can be made into cache_readonly for Index @@ -129,8 +129,7 @@ def _ndarray_values(self): # Abstract data attributes @property - def values(self): - # type: () -> np.ndarray + def values(self) -> np.ndarray: # Note: PeriodArray overrides this to return an ndarray of objects. return self._data._data diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index a4bd7f9017eb4..ae18f63fe3ba4 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -170,7 +170,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index, PeriodDelegateMixin): _is_numeric_dtype = False _infer_as_myclass = True - _data = None # type: PeriodArray + _data: PeriodArray = None _engine_type = libindex.PeriodEngine diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a61bc30a126e6..c79cf90a9c447 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -3,7 +3,7 @@ import functools import inspect import re -from typing import Any, List +from typing import Any, List, Optional, TypeVar import warnings import numpy as np @@ -1646,6 +1646,9 @@ def _get_unstack_items(self, unstacker, new_columns): return new_placement, new_values, mask +EB = TypeVar('EB', bound='ExtensionBlock') + + class ExtensionBlock(NonConsolidatableMixIn, Block): """Block for holding extension types. @@ -1828,10 +1831,9 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None, placement=self.mgr_locs) def shift(self, - periods, # type: int - axis=0, # type: libinternals.BlockPlacement - fill_value=None): # type: Any - # type: (...) -> List[ExtensionBlock] + periods: int, + axis: libinternals.BlockPlacement = 0, + fill_value: Optional[Any] = None) -> List[EB]: """ Shift the block by `periods`. diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index b9d478f3f14eb..06dd0596a6183 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1863,8 +1863,8 @@ def _shape_compat(x): return stacked, placement -def _interleaved_dtype(blocks): - # type: (List[Block]) -> Optional[Union[np.dtype, ExtensionDtype]] +def _interleaved_dtype(blocks: List[Block]) \ + -> Optional[Union[np.dtype, ExtensionDtype]]: """Find the common dtype for `blocks`. Parameters From 46cc9090da5231c3032049fce408c54c8a938ac2 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 20 Mar 2019 09:22:19 -0500 Subject: [PATCH 02/13] #25790 Added TypeVar to hint in core/indexes/base.py --- pandas/core/indexes/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ea475e335ce0d..6ec94b995d284 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta import operator from textwrap import dedent -from typing import Union +from typing import TypeVar, Union import warnings import numpy as np @@ -164,6 +164,9 @@ def _new_Index(cls, d): return cls.__new__(cls, **d) +INDEXTYPE = TypeVar('INDEXTYPE', bound='Index') + + class Index(IndexOpsMixin, PandasObject): """ Immutable ndarray implementing an ordered, sliceable set. The basic object @@ -3632,7 +3635,7 @@ def values(self): return self._data.view(np.ndarray) @property - def _values(self) -> Union[ExtensionArray, Index, np.ndarray]: + def _values(self) -> Union[ExtensionArray, INDEXTYPE, np.ndarray]: # TODO(EA): remove index types as they become extension arrays """ The best array representation. From 7acb89aff3d409d97f740aa30f3f97c3c7021ada Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 20 Mar 2019 10:12:15 -0500 Subject: [PATCH 03/13] Fixed reference to types.Tuple that should have been to typing.Tuple --- pandas/core/groupby/groupby.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c2fad0af19b8f..25bbdd81d0a89 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -12,7 +12,7 @@ class providing the base-class of operations. import datetime from functools import partial, wraps import types -from typing import Optional, Type +from typing import Optional, Type, Tuple import warnings import numpy as np @@ -1040,7 +1040,7 @@ def _bool_agg(self, val_test, skipna): Shared func to call any / all Cython GroupBy implementations. """ - def objs_to_bool(vals: np.ndarray) -> types.Tuple[np.ndarray, Type]: + def objs_to_bool(vals: np.ndarray) -> Tuple[np.ndarray, Type]: if is_object_dtype(vals): vals = np.array([bool(x) for x in vals]) else: @@ -1737,7 +1737,7 @@ def quantile(self, q=0.5, interpolation='linear'): """ def pre_processor(vals: np.ndarray) -> \ - types.Tuple[np.ndarray, Optional[Type]]: + Tuple[np.ndarray, Optional[Type]]: if is_object_dtype(vals): raise TypeError("'quantile' cannot be performed against " "'object' dtypes!") From d9c53abbc44c97cfc76b247be6fbcce1db906ec9 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 20 Mar 2019 10:43:12 -0500 Subject: [PATCH 04/13] Fixed import sort --- pandas/core/groupby/groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 25bbdd81d0a89..c5a1a78093b2b 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -12,7 +12,7 @@ class providing the base-class of operations. import datetime from functools import partial, wraps import types -from typing import Optional, Type, Tuple +from typing import Optional, Tuple, Type import warnings import numpy as np From e73fe18eee729d2cac0aa7c9ad5aa17d945373d1 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 20 Mar 2019 12:19:16 -0500 Subject: [PATCH 05/13] #25802 Updated type hints to python3 syntax (array files) --- pandas/core/arrays/array_.py | 10 ++--- pandas/core/arrays/base.py | 69 +++++++++++++++--------------- pandas/core/arrays/datetimelike.py | 34 ++++++++------- pandas/core/arrays/datetimes.py | 5 +-- pandas/core/arrays/integer.py | 6 +-- pandas/core/arrays/period.py | 32 +++++++------- pandas/core/arrays/sparse.py | 25 +++++++---- 7 files changed, 95 insertions(+), 86 deletions(-) diff --git a/pandas/core/arrays/array_.py b/pandas/core/arrays/array_.py index 254ab876af1ac..ff58f848701d5 100644 --- a/pandas/core/arrays/array_.py +++ b/pandas/core/arrays/array_.py @@ -4,6 +4,7 @@ from pandas._libs import lib, tslibs +from pandas.core.arrays.base import ExtensionArray from pandas.core.dtypes.common import ( is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype) from pandas.core.dtypes.dtypes import ExtensionDtype, registry @@ -11,11 +12,10 @@ 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 +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..53656c4d3ba77 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: Optional[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..cb84a862bc936 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -275,7 +275,7 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin, # Constructors _attributes = ["freq", "tz"] - _dtype = None # type: Union[np.dtype, DatetimeTZDtype] + _dtype: Union[np.dtype, DatetimeTZDtype] = None _freq = None def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False): @@ -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..b05b5b450e4b1 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,11 @@ 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 +676,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 From 545d38d9b8f82ecc39a926f7aec418f3d1759b5a Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 21 Mar 2019 09:09:01 -0500 Subject: [PATCH 06/13] Removed instance variable type hint for 3.5 compatibility --- pandas/core/arrays/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index cb84a862bc936..a718cb1cdba65 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -275,7 +275,7 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin, # Constructors _attributes = ["freq", "tz"] - _dtype: Union[np.dtype, DatetimeTZDtype] = None + _dtype = None # type: Union[np.dtype, DatetimeTZDtype] _freq = None def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False): From abc09faf7d349d2e5b977d374909e6359fde06d5 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 21 Mar 2019 21:02:09 -0500 Subject: [PATCH 07/13] Cleaned up array/ type hints --- pandas/core/arrays/array_.py | 2 +- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/sparse.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/array_.py b/pandas/core/arrays/array_.py index ff58f848701d5..33cddb6bae615 100644 --- a/pandas/core/arrays/array_.py +++ b/pandas/core/arrays/array_.py @@ -13,7 +13,7 @@ def array(data: Sequence[object], - dtype: Optional[Union[str, np.dtype, ExtensionDtype]] = None, + dtype: Optional[str, np.dtype, ExtensionDtype] = None, copy: bool = True, ) -> ExtensionArray: """ diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 53656c4d3ba77..127cc43406c28 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -726,7 +726,7 @@ def take( self, indices: Sequence[int], allow_fill: bool = False, - fill_value: Optional[Any] = None + fill_value: Any = None ) -> 'ExtensionArray': """ Take elements from an array. diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index b05b5b450e4b1..f478a6e58367d 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -402,7 +402,8 @@ def _get_fill(arr: 'SparseArray') -> np.ndarray: def _sparse_array_op( left: 'SparseArray', right: 'SparseArray', - op: Callable, name: str + op: Callable, + name: str ) -> Any: """ Perform a binary operation between two arrays. From c37ef88d40da422131cd528b7c316e0107b345d3 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 21 Mar 2019 22:01:20 -0500 Subject: [PATCH 08/13] #25790 Updating type hints to Python3 syntax in pandas/core/array --- pandas/core/arrays/array_.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/array_.py b/pandas/core/arrays/array_.py index 33cddb6bae615..9ab703c863f31 100644 --- a/pandas/core/arrays/array_.py +++ b/pandas/core/arrays/array_.py @@ -1,10 +1,9 @@ -from typing import Optional, Sequence, Union +from typing import Optional, Sequence, TYPE_CHECKING, Union import numpy as np from pandas._libs import lib, tslibs -from pandas.core.arrays.base import ExtensionArray from pandas.core.dtypes.common import ( is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype) from pandas.core.dtypes.dtypes import ExtensionDtype, registry @@ -12,10 +11,14 @@ from pandas import compat +if TYPE_CHECKING: + from pandas.core.arrays.base import ExtensionArray + + def array(data: Sequence[object], - dtype: Optional[str, np.dtype, ExtensionDtype] = None, + dtype: Optional[Union[str, np.dtype, ExtensionDtype]] = None, copy: bool = True, - ) -> ExtensionArray: + ) -> 'ExtensionArray': """ Create an array. From ea9c2b3396458f80f2c590c01b430a0833893226 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 21 Mar 2019 22:45:38 -0500 Subject: [PATCH 09/13] #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 From 20b63e4e21da31d99ce43fca31a640573650c233 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Sat, 23 Mar 2019 22:15:39 -0500 Subject: [PATCH 10/13] Remove conditional import --- pandas/core/arrays/array_.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/array_.py b/pandas/core/arrays/array_.py index 9ab703c863f31..2404e1a71f5ad 100644 --- a/pandas/core/arrays/array_.py +++ b/pandas/core/arrays/array_.py @@ -1,4 +1,4 @@ -from typing import Optional, Sequence, TYPE_CHECKING, Union +from typing import Optional, Sequence, Union import numpy as np @@ -7,18 +7,15 @@ from pandas.core.dtypes.common import ( is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype) from pandas.core.dtypes.dtypes import ExtensionDtype, registry +from pandas.core.dtypes.generic import ABCExtensionArray from pandas import compat -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': + ) -> ABCExtensionArray: """ Create an array. From cf774d7d0f4244327c33967a0c65a9a9b3ee0d91 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 27 Mar 2019 22:57:53 -0500 Subject: [PATCH 11/13] Change forward refs to ABCs --- pandas/core/arrays/base.py | 4 ++-- pandas/core/arrays/datetimelike.py | 4 ++-- pandas/core/arrays/period.py | 4 ++-- pandas/core/arrays/sparse.py | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 127cc43406c28..aa186980c2646 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -17,7 +17,7 @@ from pandas.core.dtypes.common import is_list_like from pandas.core.dtypes.dtypes import ExtensionDtype -from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries +from pandas.core.dtypes.generic import ABCIndexClass, ABCExtensionArray, ABCSeries from pandas.core.dtypes.missing import isna from pandas.core import ops @@ -337,7 +337,7 @@ def astype(self, dtype, copy=True): """ return np.array(self, dtype=dtype, copy=copy) - def isna(self) -> Union['ExtensionArray', np.ndarray]: + def isna(self) -> Union[ABCExtensionArray, np.ndarray]: """ A 1-D array indicating if each value is missing. diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 9e6602daa92bb..d7d14d6bdb6e3 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from datetime import datetime, timedelta import operator -from typing import Any, Sequence, Tuple, Union +from typing import Any, Sequence, Tuple, Type, Union import warnings import numpy as np @@ -58,7 +58,7 @@ def _get_attributes_dict(self): return {k: getattr(self, k, None) for k in self._attributes} @property - def _scalar_type(self) -> Union[type, Tuple[type]]: + def _scalar_type(self) -> Union[Type, Tuple[Type]]: """The scalar associated with this datelike * PeriodArray : Period diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index ccc28ddec0714..b2166f6c7eea8 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -20,7 +20,7 @@ is_list_like, is_period_dtype, pandas_dtype) from pandas.core.dtypes.dtypes import PeriodDtype from pandas.core.dtypes.generic import ( - ABCDataFrame, ABCIndexClass, ABCPeriodIndex, ABCSeries) + ABCDataFrame, ABCIndexClass, ABCPeriodArray, ABCPeriodIndex, ABCSeries) from pandas.core.dtypes.missing import isna, notna import pandas.core.algorithms as algos @@ -188,7 +188,7 @@ def _from_sequence( scalars: Sequence[Optional[Period]], dtype: PeriodDtype = None, copy: bool = False, - ) -> 'PeriodArray': + ) -> ABCPeriodArray: if dtype: freq = dtype.freq else: diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index f478a6e58367d..7303573bfc13b 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -29,7 +29,7 @@ pandas_dtype) from pandas.core.dtypes.dtypes import register_extension_dtype from pandas.core.dtypes.generic import ( - ABCIndexClass, ABCSeries, ABCSparseSeries) + ABCIndexClass, ABCSeries, ABCSparseArray, ABCSparseSeries) from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna from pandas.core.accessor import PandasDelegate, delegate_names @@ -400,8 +400,8 @@ def _get_fill(arr: 'SparseArray') -> np.ndarray: def _sparse_array_op( - left: 'SparseArray', - right: 'SparseArray', + left: ABCSparseArray, + right: ABCSparseArray, op: Callable, name: str ) -> Any: From 33db1eef3c3361cebe61b439ba15af4569ce6ade Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 28 Mar 2019 00:21:23 -0500 Subject: [PATCH 12/13] Change remaining forward refs to ABCs --- pandas/core/arrays/base.py | 12 ++++++------ pandas/core/arrays/period.py | 2 +- pandas/core/arrays/sparse.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 387fef1443ef1..44662ce831b89 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -479,7 +479,7 @@ def shift( self, periods: int = 1, fill_value: object = None, - ) -> 'ExtensionArray': + ) -> ABCExtensionArray: """ Shift values by desired number. @@ -621,7 +621,7 @@ def _values_for_factorize(self) -> Tuple[np.ndarray, Any]: def factorize( self, na_sentinel: int = -1, - ) -> Tuple[np.ndarray, 'ExtensionArray']: + ) -> Tuple[np.ndarray, ABCExtensionArray]: """ Encode the extension array as an enumerated type. @@ -728,7 +728,7 @@ def take( indices: Sequence[int], allow_fill: bool = False, fill_value: Any = None - ) -> 'ExtensionArray': + ) -> ABCExtensionArray: """ Take elements from an array. @@ -817,7 +817,7 @@ def take(self, indices, allow_fill=False, fill_value=None): # pandas.api.extensions.take raise AbstractMethodError(self) - def copy(self, deep: bool = False) -> 'ExtensionArray': + def copy(self, deep: bool = False) -> ABCExtensionArray: """ Return a copy of the array. @@ -901,8 +901,8 @@ def _formatting_values(self) -> np.ndarray: @classmethod def _concat_same_type( cls, - to_concat: Sequence['ExtensionArray'] - ) -> 'ExtensionArray': + to_concat: Sequence[ABCExtensionArray] + ) -> ABCExtensionArray: """ Concatenate multiple array diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 44da1e52cdc50..70312f2e61445 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -544,7 +544,7 @@ def _addsub_int_array( self, other: Union[ExtensionArray, np.ndarray, ABCIndexClass], op: Callable[[Any], Any] - ) -> 'PeriodArray': + ) -> ABCPeriodArray: assert op in [operator.add, operator.sub] if op is operator.sub: other = -other diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 3da5a5f7bb62e..a4cfb38877326 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -373,7 +373,7 @@ def _subtype_with_str(self): _sparray_doc_kwargs = dict(klass='SparseArray') -def _get_fill(arr: 'SparseArray') -> np.ndarray: +def _get_fill(arr: ABCSparseArray) -> np.ndarray: """ Create a 0-dim ndarray containing the fill value @@ -680,7 +680,7 @@ def _simple_new( sparse_array: np.ndarray, sparse_index: SparseIndex, dtype: SparseDtype - ) -> 'SparseArray': + ) -> ABCSparseArray: new = cls([]) new._sparse_index = sparse_index new._sparse_values = sparse_array From 23aba8a5d19246b88c60c9fb0677036ad72488c3 Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Thu, 28 Mar 2019 08:24:12 -0500 Subject: [PATCH 13/13] type to Type --- pandas/core/arrays/sparse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index a4cfb38877326..287665b0165de 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -4,7 +4,7 @@ import numbers import operator import re -from typing import Any, Callable, Union +from typing import Any, Callable, Type, Union import warnings import numpy as np @@ -79,7 +79,7 @@ class SparseDtype(ExtensionDtype): def __init__( self, - dtype: Union[str, np.dtype, 'ExtensionDtype', type] = np.float64, + dtype: Union[str, np.dtype, ExtensionDtype, Type] = np.float64, fill_value: Any = None ) -> None: from pandas.core.dtypes.missing import na_value_for_dtype