11import numbers
2- from typing import Union
2+ from typing import Optional , Tuple , Type , Union
33
44import numpy as np
55from numpy .lib .mixins import NDArrayOperatorsMixin
@@ -34,54 +34,66 @@ class PandasDtype(ExtensionDtype):
3434
3535 Parameters
3636 ----------
37- dtype : numpy.dtype
37+ dtype : object
38+ Object to be converted to a NumPy data type object.
39+
40+ See Also
41+ --------
42+ numpy.dtype
3843 """
3944
4045 _metadata = ("_dtype" ,)
4146
42- def __init__ (self , dtype ):
43- dtype = np .dtype (dtype )
44- self ._dtype = dtype
45- self ._type = dtype .type
47+ def __init__ (self , dtype : object ):
48+ self ._dtype = np .dtype (dtype )
4649
4750 def __repr__ (self ) -> str :
4851 return f"PandasDtype({ repr (self .name )} )"
4952
5053 @property
51- def numpy_dtype (self ):
52- """The NumPy dtype this PandasDtype wraps."""
54+ def numpy_dtype (self ) -> np .dtype :
55+ """
56+ The NumPy dtype this PandasDtype wraps.
57+ """
5358 return self ._dtype
5459
5560 @property
56- def name (self ):
61+ def name (self ) -> str :
62+ """
63+ A bit-width name for this data-type.
64+ """
5765 return self ._dtype .name
5866
5967 @property
60- def type (self ):
61- return self ._type
68+ def type (self ) -> Type [np .generic ]:
69+ """
70+ The type object used to instantiate a scalar of this NumPy data-type.
71+ """
72+ return self ._dtype .type
6273
6374 @property
64- def _is_numeric (self ):
75+ def _is_numeric (self ) -> bool :
6576 # exclude object, str, unicode, void.
6677 return self .kind in set ("biufc" )
6778
6879 @property
69- def _is_boolean (self ):
80+ def _is_boolean (self ) -> bool :
7081 return self .kind == "b"
7182
7283 @classmethod
73- def construct_from_string (cls , string ) :
84+ def construct_from_string (cls , string : str ) -> "PandasDtype" :
7485 try :
75- return cls ( np .dtype (string ) )
86+ dtype = np .dtype (string )
7687 except TypeError as err :
7788 if not isinstance (string , str ):
7889 msg = f"'construct_from_string' expects a string, got { type (string )} "
7990 else :
8091 msg = f"Cannot construct a 'PandasDtype' from '{ string } '"
8192 raise TypeError (msg ) from err
93+ return cls (dtype )
8294
8395 @classmethod
84- def construct_array_type (cls ):
96+ def construct_array_type (cls ) -> Type [ "PandasArray" ] :
8597 """
8698 Return the array type associated with this dtype.
8799
@@ -92,12 +104,17 @@ def construct_array_type(cls):
92104 return PandasArray
93105
94106 @property
95- def kind (self ):
107+ def kind (self ) -> str :
108+ """
109+ A character code (one of 'biufcmMOSUV') identifying the general kind of data.
110+ """
96111 return self ._dtype .kind
97112
98113 @property
99- def itemsize (self ):
100- """The element size of this data-type object."""
114+ def itemsize (self ) -> int :
115+ """
116+ The element size of this data-type object.
117+ """
101118 return self ._dtype .itemsize
102119
103120
@@ -155,7 +172,7 @@ def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False)
155172 self ._dtype = PandasDtype (values .dtype )
156173
157174 @classmethod
158- def _from_sequence (cls , scalars , dtype = None , copy = False ):
175+ def _from_sequence (cls , scalars , dtype = None , copy : bool = False ) -> "PandasArray" :
159176 if isinstance (dtype , PandasDtype ):
160177 dtype = dtype ._dtype
161178
@@ -165,18 +182,18 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
165182 return cls (result )
166183
167184 @classmethod
168- def _from_factorized (cls , values , original ):
185+ def _from_factorized (cls , values , original ) -> "PandasArray" :
169186 return cls (values )
170187
171188 @classmethod
172- def _concat_same_type (cls , to_concat ):
189+ def _concat_same_type (cls , to_concat ) -> "PandasArray" :
173190 return cls (np .concatenate (to_concat ))
174191
175192 # ------------------------------------------------------------------------
176193 # Data
177194
178195 @property
179- def dtype (self ):
196+ def dtype (self ) -> PandasDtype :
180197 return self ._dtype
181198
182199 # ------------------------------------------------------------------------
@@ -187,7 +204,7 @@ def __array__(self, dtype=None) -> np.ndarray:
187204
188205 _HANDLED_TYPES = (np .ndarray , numbers .Number )
189206
190- def __array_ufunc__ (self , ufunc , method , * inputs , ** kwargs ):
207+ def __array_ufunc__ (self , ufunc , method : str , * inputs , ** kwargs ):
191208 # Lightly modified version of
192209 # https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/\
193210 # numpy.lib.mixins.NDArrayOperatorsMixin.html
@@ -242,7 +259,7 @@ def __getitem__(self, item):
242259 result = type (self )(result )
243260 return result
244261
245- def __setitem__ (self , key , value ):
262+ def __setitem__ (self , key , value ) -> None :
246263 value = extract_array (value , extract_numpy = True )
247264
248265 scalar_key = lib .is_scalar (key )
@@ -263,10 +280,12 @@ def __len__(self) -> int:
263280 def nbytes (self ) -> int :
264281 return self ._ndarray .nbytes
265282
266- def isna (self ):
283+ def isna (self ) -> np . ndarray :
267284 return isna (self ._ndarray )
268285
269- def fillna (self , value = None , method = None , limit = None ):
286+ def fillna (
287+ self , value = None , method : Optional [str ] = None , limit : Optional [int ] = None ,
288+ ) -> "PandasArray" :
270289 # TODO(_values_for_fillna): remove this
271290 value , method = validate_fillna_kwargs (value , method )
272291
@@ -293,7 +312,7 @@ def fillna(self, value=None, method=None, limit=None):
293312 new_values = self .copy ()
294313 return new_values
295314
296- def take (self , indices , allow_fill = False , fill_value = None ):
315+ def take (self , indices , allow_fill = False , fill_value = None ) -> "PandasArray" :
297316 if fill_value is None :
298317 # Primarily for subclasses
299318 fill_value = self .dtype .na_value
@@ -302,16 +321,16 @@ def take(self, indices, allow_fill=False, fill_value=None):
302321 )
303322 return type (self )(result )
304323
305- def copy (self ):
324+ def copy (self ) -> "PandasArray" :
306325 return type (self )(self ._ndarray .copy ())
307326
308- def _values_for_argsort (self ):
327+ def _values_for_argsort (self ) -> np . ndarray :
309328 return self ._ndarray
310329
311- def _values_for_factorize (self ):
330+ def _values_for_factorize (self ) -> Tuple [ np . ndarray , int ] :
312331 return self ._ndarray , - 1
313332
314- def unique (self ):
333+ def unique (self ) -> "PandasArray" :
315334 return type (self )(unique (self ._ndarray ))
316335
317336 # ------------------------------------------------------------------------
0 commit comments