1
1
import numbers
2
- from typing import Union
2
+ from typing import Optional , Tuple , Type , Union
3
3
4
4
import numpy as np
5
5
from numpy .lib .mixins import NDArrayOperatorsMixin
@@ -34,54 +34,66 @@ class PandasDtype(ExtensionDtype):
34
34
35
35
Parameters
36
36
----------
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
38
43
"""
39
44
40
45
_metadata = ("_dtype" ,)
41
46
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 )
46
49
47
50
def __repr__ (self ) -> str :
48
51
return f"PandasDtype({ repr (self .name )} )"
49
52
50
53
@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
+ """
53
58
return self ._dtype
54
59
55
60
@property
56
- def name (self ):
61
+ def name (self ) -> str :
62
+ """
63
+ A bit-width name for this data-type.
64
+ """
57
65
return self ._dtype .name
58
66
59
67
@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
62
73
63
74
@property
64
- def _is_numeric (self ):
75
+ def _is_numeric (self ) -> bool :
65
76
# exclude object, str, unicode, void.
66
77
return self .kind in set ("biufc" )
67
78
68
79
@property
69
- def _is_boolean (self ):
80
+ def _is_boolean (self ) -> bool :
70
81
return self .kind == "b"
71
82
72
83
@classmethod
73
- def construct_from_string (cls , string ) :
84
+ def construct_from_string (cls , string : str ) -> "PandasDtype" :
74
85
try :
75
- return cls ( np .dtype (string ) )
86
+ dtype = np .dtype (string )
76
87
except TypeError as err :
77
88
if not isinstance (string , str ):
78
89
msg = f"'construct_from_string' expects a string, got { type (string )} "
79
90
else :
80
91
msg = f"Cannot construct a 'PandasDtype' from '{ string } '"
81
92
raise TypeError (msg ) from err
93
+ return cls (dtype )
82
94
83
95
@classmethod
84
- def construct_array_type (cls ):
96
+ def construct_array_type (cls ) -> Type [ "PandasArray" ] :
85
97
"""
86
98
Return the array type associated with this dtype.
87
99
@@ -92,12 +104,17 @@ def construct_array_type(cls):
92
104
return PandasArray
93
105
94
106
@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
+ """
96
111
return self ._dtype .kind
97
112
98
113
@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
+ """
101
118
return self ._dtype .itemsize
102
119
103
120
@@ -155,7 +172,7 @@ def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False)
155
172
self ._dtype = PandasDtype (values .dtype )
156
173
157
174
@classmethod
158
- def _from_sequence (cls , scalars , dtype = None , copy = False ):
175
+ def _from_sequence (cls , scalars , dtype = None , copy : bool = False ) -> "PandasArray" :
159
176
if isinstance (dtype , PandasDtype ):
160
177
dtype = dtype ._dtype
161
178
@@ -165,18 +182,18 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
165
182
return cls (result )
166
183
167
184
@classmethod
168
- def _from_factorized (cls , values , original ):
185
+ def _from_factorized (cls , values , original ) -> "PandasArray" :
169
186
return cls (values )
170
187
171
188
@classmethod
172
- def _concat_same_type (cls , to_concat ):
189
+ def _concat_same_type (cls , to_concat ) -> "PandasArray" :
173
190
return cls (np .concatenate (to_concat ))
174
191
175
192
# ------------------------------------------------------------------------
176
193
# Data
177
194
178
195
@property
179
- def dtype (self ):
196
+ def dtype (self ) -> PandasDtype :
180
197
return self ._dtype
181
198
182
199
# ------------------------------------------------------------------------
@@ -187,7 +204,7 @@ def __array__(self, dtype=None) -> np.ndarray:
187
204
188
205
_HANDLED_TYPES = (np .ndarray , numbers .Number )
189
206
190
- def __array_ufunc__ (self , ufunc , method , * inputs , ** kwargs ):
207
+ def __array_ufunc__ (self , ufunc , method : str , * inputs , ** kwargs ):
191
208
# Lightly modified version of
192
209
# https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/\
193
210
# numpy.lib.mixins.NDArrayOperatorsMixin.html
@@ -242,7 +259,7 @@ def __getitem__(self, item):
242
259
result = type (self )(result )
243
260
return result
244
261
245
- def __setitem__ (self , key , value ):
262
+ def __setitem__ (self , key , value ) -> None :
246
263
value = extract_array (value , extract_numpy = True )
247
264
248
265
scalar_key = lib .is_scalar (key )
@@ -263,10 +280,12 @@ def __len__(self) -> int:
263
280
def nbytes (self ) -> int :
264
281
return self ._ndarray .nbytes
265
282
266
- def isna (self ):
283
+ def isna (self ) -> np . ndarray :
267
284
return isna (self ._ndarray )
268
285
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" :
270
289
# TODO(_values_for_fillna): remove this
271
290
value , method = validate_fillna_kwargs (value , method )
272
291
@@ -293,7 +312,7 @@ def fillna(self, value=None, method=None, limit=None):
293
312
new_values = self .copy ()
294
313
return new_values
295
314
296
- def take (self , indices , allow_fill = False , fill_value = None ):
315
+ def take (self , indices , allow_fill = False , fill_value = None ) -> "PandasArray" :
297
316
if fill_value is None :
298
317
# Primarily for subclasses
299
318
fill_value = self .dtype .na_value
@@ -302,16 +321,16 @@ def take(self, indices, allow_fill=False, fill_value=None):
302
321
)
303
322
return type (self )(result )
304
323
305
- def copy (self ):
324
+ def copy (self ) -> "PandasArray" :
306
325
return type (self )(self ._ndarray .copy ())
307
326
308
- def _values_for_argsort (self ):
327
+ def _values_for_argsort (self ) -> np . ndarray :
309
328
return self ._ndarray
310
329
311
- def _values_for_factorize (self ):
330
+ def _values_for_factorize (self ) -> Tuple [ np . ndarray , int ] :
312
331
return self ._ndarray , - 1
313
332
314
- def unique (self ):
333
+ def unique (self ) -> "PandasArray" :
315
334
return type (self )(unique (self ._ndarray ))
316
335
317
336
# ------------------------------------------------------------------------
0 commit comments