Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/core/arrays/integer.py: 88%

70 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import annotations 

2 

3import numpy as np 

4 

5from pandas.core.dtypes.base import register_extension_dtype 

6from pandas.core.dtypes.common import is_integer_dtype 

7 

8from pandas.core.arrays.numeric import ( 

9 NumericArray, 

10 NumericDtype, 

11) 

12 

13 

14class IntegerDtype(NumericDtype): 

15 """ 

16 An ExtensionDtype to hold a single size & kind of integer dtype. 

17 

18 These specific implementations are subclasses of the non-public 

19 IntegerDtype. For example we have Int8Dtype to represent signed int 8s. 

20 

21 The attributes name & type are set when these subclasses are created. 

22 """ 

23 

24 _default_np_dtype = np.dtype(np.int64) 

25 _checker = is_integer_dtype 

26 

27 @classmethod 

28 def construct_array_type(cls) -> type[IntegerArray]: 

29 """ 

30 Return the array type associated with this dtype. 

31 

32 Returns 

33 ------- 

34 type 

35 """ 

36 return IntegerArray 

37 

38 @classmethod 

39 def _str_to_dtype_mapping(cls): 

40 return INT_STR_TO_DTYPE 

41 

42 @classmethod 

43 def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarray: 

44 """ 

45 Safely cast the values to the given dtype. 

46 

47 "safe" in this context means the casting is lossless. e.g. if 'values' 

48 has a floating dtype, each value must be an integer. 

49 """ 

50 try: 

51 return values.astype(dtype, casting="safe", copy=copy) 

52 except TypeError as err: 

53 casted = values.astype(dtype, copy=copy) 

54 if (casted == values).all(): 

55 return casted 

56 

57 raise TypeError( 

58 f"cannot safely cast non-equivalent {values.dtype} to {np.dtype(dtype)}" 

59 ) from err 

60 

61 

62class IntegerArray(NumericArray): 

63 """ 

64 Array of integer (optional missing) values. 

65 

66 .. versionchanged:: 1.0.0 

67 

68 Now uses :attr:`pandas.NA` as the missing value rather 

69 than :attr:`numpy.nan`. 

70 

71 .. warning:: 

72 

73 IntegerArray is currently experimental, and its API or internal 

74 implementation may change without warning. 

75 

76 We represent an IntegerArray with 2 numpy arrays: 

77 

78 - data: contains a numpy integer array of the appropriate dtype 

79 - mask: a boolean array holding a mask on the data, True is missing 

80 

81 To construct an IntegerArray from generic array-like input, use 

82 :func:`pandas.array` with one of the integer dtypes (see examples). 

83 

84 See :ref:`integer_na` for more. 

85 

86 Parameters 

87 ---------- 

88 values : numpy.ndarray 

89 A 1-d integer-dtype array. 

90 mask : numpy.ndarray 

91 A 1-d boolean-dtype array indicating missing values. 

92 copy : bool, default False 

93 Whether to copy the `values` and `mask`. 

94 

95 Attributes 

96 ---------- 

97 None 

98 

99 Methods 

100 ------- 

101 None 

102 

103 Returns 

104 ------- 

105 IntegerArray 

106 

107 Examples 

108 -------- 

109 Create an IntegerArray with :func:`pandas.array`. 

110 

111 >>> int_array = pd.array([1, None, 3], dtype=pd.Int32Dtype()) 

112 >>> int_array 

113 <IntegerArray> 

114 [1, <NA>, 3] 

115 Length: 3, dtype: Int32 

116 

117 String aliases for the dtypes are also available. They are capitalized. 

118 

119 >>> pd.array([1, None, 3], dtype='Int32') 

120 <IntegerArray> 

121 [1, <NA>, 3] 

122 Length: 3, dtype: Int32 

123 

124 >>> pd.array([1, None, 3], dtype='UInt16') 

125 <IntegerArray> 

126 [1, <NA>, 3] 

127 Length: 3, dtype: UInt16 

128 """ 

129 

130 _dtype_cls = IntegerDtype 

131 

132 # The value used to fill '_data' to avoid upcasting 

133 _internal_fill_value = 1 

134 # Fill values used for any/all 

135 _truthy_value = 1 

136 _falsey_value = 0 

137 

138 

139_dtype_docstring = """ 

140An ExtensionDtype for {dtype} integer data. 

141 

142.. versionchanged:: 1.0.0 

143 

144 Now uses :attr:`pandas.NA` as its missing value, 

145 rather than :attr:`numpy.nan`. 

146 

147Attributes 

148---------- 

149None 

150 

151Methods 

152------- 

153None 

154""" 

155 

156# create the Dtype 

157 

158 

159@register_extension_dtype 

160class Int8Dtype(IntegerDtype): 

161 type = np.int8 

162 name = "Int8" 

163 __doc__ = _dtype_docstring.format(dtype="int8") 

164 

165 

166@register_extension_dtype 

167class Int16Dtype(IntegerDtype): 

168 type = np.int16 

169 name = "Int16" 

170 __doc__ = _dtype_docstring.format(dtype="int16") 

171 

172 

173@register_extension_dtype 

174class Int32Dtype(IntegerDtype): 

175 type = np.int32 

176 name = "Int32" 

177 __doc__ = _dtype_docstring.format(dtype="int32") 

178 

179 

180@register_extension_dtype 

181class Int64Dtype(IntegerDtype): 

182 type = np.int64 

183 name = "Int64" 

184 __doc__ = _dtype_docstring.format(dtype="int64") 

185 

186 

187@register_extension_dtype 

188class UInt8Dtype(IntegerDtype): 

189 type = np.uint8 

190 name = "UInt8" 

191 __doc__ = _dtype_docstring.format(dtype="uint8") 

192 

193 

194@register_extension_dtype 

195class UInt16Dtype(IntegerDtype): 

196 type = np.uint16 

197 name = "UInt16" 

198 __doc__ = _dtype_docstring.format(dtype="uint16") 

199 

200 

201@register_extension_dtype 

202class UInt32Dtype(IntegerDtype): 

203 type = np.uint32 

204 name = "UInt32" 

205 __doc__ = _dtype_docstring.format(dtype="uint32") 

206 

207 

208@register_extension_dtype 

209class UInt64Dtype(IntegerDtype): 

210 type = np.uint64 

211 name = "UInt64" 

212 __doc__ = _dtype_docstring.format(dtype="uint64") 

213 

214 

215INT_STR_TO_DTYPE: dict[str, IntegerDtype] = { 

216 "int8": Int8Dtype(), 

217 "int16": Int16Dtype(), 

218 "int32": Int32Dtype(), 

219 "int64": Int64Dtype(), 

220 "uint8": UInt8Dtype(), 

221 "uint16": UInt16Dtype(), 

222 "uint32": UInt32Dtype(), 

223 "uint64": UInt64Dtype(), 

224}