1
1
import numbers
2
- from typing import Any , Tuple , Type
2
+ from typing import TYPE_CHECKING , Any , Dict , Tuple , Type , Union
3
3
import warnings
4
4
5
5
import numpy as np
31
31
32
32
from .masked import BaseMaskedArray
33
33
34
+ if TYPE_CHECKING :
35
+ import pyarrow # noqa: F401
36
+
34
37
35
38
class _IntegerDtype (ExtensionDtype ):
36
39
"""
@@ -52,33 +55,33 @@ def __repr__(self) -> str:
52
55
return f"{ sign } Int{ 8 * self .itemsize } Dtype()"
53
56
54
57
@cache_readonly
55
- def is_signed_integer (self ):
58
+ def is_signed_integer (self ) -> bool :
56
59
return self .kind == "i"
57
60
58
61
@cache_readonly
59
- def is_unsigned_integer (self ):
62
+ def is_unsigned_integer (self ) -> bool :
60
63
return self .kind == "u"
61
64
62
65
@property
63
- def _is_numeric (self ):
66
+ def _is_numeric (self ) -> bool :
64
67
return True
65
68
66
69
@cache_readonly
67
- def numpy_dtype (self ):
70
+ def numpy_dtype (self ) -> np . dtype :
68
71
""" Return an instance of our numpy dtype """
69
72
return np .dtype (self .type )
70
73
71
74
@cache_readonly
72
- def kind (self ):
75
+ def kind (self ) -> str :
73
76
return self .numpy_dtype .kind
74
77
75
78
@cache_readonly
76
- def itemsize (self ):
79
+ def itemsize (self ) -> int :
77
80
""" Return the number of bytes in this dtype """
78
81
return self .numpy_dtype .itemsize
79
82
80
83
@classmethod
81
- def construct_array_type (cls ):
84
+ def construct_array_type (cls ) -> Type [ "IntegerArray" ] :
82
85
"""
83
86
Return the array type associated with this dtype.
84
87
@@ -88,9 +91,13 @@ def construct_array_type(cls):
88
91
"""
89
92
return IntegerArray
90
93
91
- def __from_arrow__ (self , array ):
92
- """Construct IntegerArray from passed pyarrow Array/ChunkedArray"""
93
- import pyarrow
94
+ def __from_arrow__ (
95
+ self , array : Union ["pyarrow.Array" , "pyarrow.ChunkedArray" ]
96
+ ) -> "IntegerArray" :
97
+ """
98
+ Construct IntegerArray from pyarrow Array/ChunkedArray.
99
+ """
100
+ import pyarrow # noqa: F811
94
101
from pandas .core .arrays ._arrow_utils import pyarrow_array_to_numpy_and_mask
95
102
96
103
if isinstance (array , pyarrow .Array ):
@@ -108,7 +115,7 @@ def __from_arrow__(self, array):
108
115
return IntegerArray ._concat_same_type (results )
109
116
110
117
111
- def integer_array (values , dtype = None , copy = False ) :
118
+ def integer_array (values , dtype = None , copy : bool = False ,) -> "IntegerArray" :
112
119
"""
113
120
Infer and return an integer array of the values.
114
121
@@ -131,7 +138,7 @@ def integer_array(values, dtype=None, copy=False):
131
138
return IntegerArray (values , mask )
132
139
133
140
134
- def safe_cast (values , dtype , copy ):
141
+ def safe_cast (values , dtype , copy : bool ):
135
142
"""
136
143
Safely cast the values to the dtype if they
137
144
are equivalent, meaning floats must be equivalent to the
@@ -152,7 +159,9 @@ def safe_cast(values, dtype, copy):
152
159
)
153
160
154
161
155
- def coerce_to_array (values , dtype , mask = None , copy = False ):
162
+ def coerce_to_array (
163
+ values , dtype , mask = None , copy : bool = False ,
164
+ ) -> Tuple [np .ndarray , np .ndarray ]:
156
165
"""
157
166
Coerce the input values array to numpy arrays with a mask
158
167
@@ -322,10 +331,10 @@ class IntegerArray(BaseMaskedArray):
322
331
_internal_fill_value = 1
323
332
324
333
@cache_readonly
325
- def dtype (self ):
334
+ def dtype (self ) -> _IntegerDtype :
326
335
return _dtypes [str (self ._data .dtype )]
327
336
328
- def __init__ (self , values , mask , copy = False ):
337
+ def __init__ (self , values : np . ndarray , mask : np . ndarray , copy : bool = False ):
329
338
if not (isinstance (values , np .ndarray ) and is_integer_dtype (values .dtype )):
330
339
raise TypeError (
331
340
"values should be integer numpy array. Use "
@@ -345,21 +354,23 @@ def __init__(self, values, mask, copy=False):
345
354
self ._mask = mask
346
355
347
356
@classmethod
348
- def _from_sequence (cls , scalars , dtype = None , copy = False ):
357
+ def _from_sequence (cls , scalars , dtype = None , copy : bool = False ) -> "IntegerArray" :
349
358
return integer_array (scalars , dtype = dtype , copy = copy )
350
359
351
360
@classmethod
352
- def _from_sequence_of_strings (cls , strings , dtype = None , copy = False ):
361
+ def _from_sequence_of_strings (
362
+ cls , strings , dtype = None , copy : bool = False
363
+ ) -> "IntegerArray" :
353
364
scalars = to_numeric (strings , errors = "raise" )
354
365
return cls ._from_sequence (scalars , dtype , copy )
355
366
356
367
@classmethod
357
- def _from_factorized (cls , values , original ):
368
+ def _from_factorized (cls , values , original ) -> "IntegerArray" :
358
369
return integer_array (values , dtype = original .dtype )
359
370
360
371
_HANDLED_TYPES = (np .ndarray , numbers .Number )
361
372
362
- def __array_ufunc__ (self , ufunc , method , * inputs , ** kwargs ):
373
+ def __array_ufunc__ (self , ufunc , method : str , * inputs , ** kwargs ):
363
374
# For IntegerArray inputs, we apply the ufunc to ._data
364
375
# and mask the result.
365
376
if method == "reduce" :
@@ -697,103 +708,65 @@ def integer_arithmetic_method(self, other):
697
708
"""
698
709
699
710
# create the Dtype
700
- Int8Dtype = register_extension_dtype (
701
- type (
702
- "Int8Dtype" ,
703
- (_IntegerDtype ,),
704
- {
705
- "type" : np .int8 ,
706
- "name" : "Int8" ,
707
- "__doc__" : _dtype_docstring .format (dtype = "int8" ),
708
- },
709
- )
710
- )
711
711
712
- Int16Dtype = register_extension_dtype (
713
- type (
714
- "Int16Dtype" ,
715
- (_IntegerDtype ,),
716
- {
717
- "type" : np .int16 ,
718
- "name" : "Int16" ,
719
- "__doc__" : _dtype_docstring .format (dtype = "int16" ),
720
- },
721
- )
722
- )
723
712
724
- Int32Dtype = register_extension_dtype (
725
- type (
726
- "Int32Dtype" ,
727
- (_IntegerDtype ,),
728
- {
729
- "type" : np .int32 ,
730
- "name" : "Int32" ,
731
- "__doc__" : _dtype_docstring .format (dtype = "int32" ),
732
- },
733
- )
734
- )
713
+ @register_extension_dtype
714
+ class Int8Dtype (_IntegerDtype ):
715
+ type = np .int8
716
+ name = "Int8"
717
+ __doc__ = _dtype_docstring .format (dtype = "int8" )
735
718
736
- Int64Dtype = register_extension_dtype (
737
- type (
738
- "Int64Dtype" ,
739
- (_IntegerDtype ,),
740
- {
741
- "type" : np .int64 ,
742
- "name" : "Int64" ,
743
- "__doc__" : _dtype_docstring .format (dtype = "int64" ),
744
- },
745
- )
746
- )
747
719
748
- UInt8Dtype = register_extension_dtype (
749
- type (
750
- "UInt8Dtype" ,
751
- (_IntegerDtype ,),
752
- {
753
- "type" : np .uint8 ,
754
- "name" : "UInt8" ,
755
- "__doc__" : _dtype_docstring .format (dtype = "uint8" ),
756
- },
757
- )
758
- )
720
+ @register_extension_dtype
721
+ class Int16Dtype (_IntegerDtype ):
722
+ type = np .int16
723
+ name = "Int16"
724
+ __doc__ = _dtype_docstring .format (dtype = "int16" )
759
725
760
- UInt16Dtype = register_extension_dtype (
761
- type (
762
- "UInt16Dtype" ,
763
- (_IntegerDtype ,),
764
- {
765
- "type" : np .uint16 ,
766
- "name" : "UInt16" ,
767
- "__doc__" : _dtype_docstring .format (dtype = "uint16" ),
768
- },
769
- )
770
- )
771
726
772
- UInt32Dtype = register_extension_dtype (
773
- type (
774
- "UInt32Dtype" ,
775
- (_IntegerDtype ,),
776
- {
777
- "type" : np .uint32 ,
778
- "name" : "UInt32" ,
779
- "__doc__" : _dtype_docstring .format (dtype = "uint32" ),
780
- },
781
- )
782
- )
727
+ @register_extension_dtype
728
+ class Int32Dtype (_IntegerDtype ):
729
+ type = np .int32
730
+ name = "Int32"
731
+ __doc__ = _dtype_docstring .format (dtype = "int32" )
732
+
733
+
734
+ @register_extension_dtype
735
+ class Int64Dtype (_IntegerDtype ):
736
+ type = np .int64
737
+ name = "Int64"
738
+ __doc__ = _dtype_docstring .format (dtype = "int64" )
739
+
740
+
741
+ @register_extension_dtype
742
+ class UInt8Dtype (_IntegerDtype ):
743
+ type = np .uint8
744
+ name = "UInt8"
745
+ __doc__ = _dtype_docstring .format (dtype = "uint8" )
746
+
747
+
748
+ @register_extension_dtype
749
+ class UInt16Dtype (_IntegerDtype ):
750
+ type = np .uint16
751
+ name = "UInt16"
752
+ __doc__ = _dtype_docstring .format (dtype = "uint16" )
753
+
754
+
755
+ @register_extension_dtype
756
+ class UInt32Dtype (_IntegerDtype ):
757
+ type = np .uint32
758
+ name = "UInt32"
759
+ __doc__ = _dtype_docstring .format (dtype = "uint32" )
760
+
761
+
762
+ @register_extension_dtype
763
+ class UInt64Dtype (_IntegerDtype ):
764
+ type = np .uint64
765
+ name = "UInt64"
766
+ __doc__ = _dtype_docstring .format (dtype = "uint64" )
783
767
784
- UInt64Dtype = register_extension_dtype (
785
- type (
786
- "UInt64Dtype" ,
787
- (_IntegerDtype ,),
788
- {
789
- "type" : np .uint64 ,
790
- "name" : "UInt64" ,
791
- "__doc__" : _dtype_docstring .format (dtype = "uint64" ),
792
- },
793
- )
794
- )
795
768
796
- _dtypes = {
769
+ _dtypes : Dict [ str , _IntegerDtype ] = {
797
770
"int8" : Int8Dtype (),
798
771
"int16" : Int16Dtype (),
799
772
"int32" : Int32Dtype (),
0 commit comments