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

34 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.common import is_float_dtype 

6from pandas.core.dtypes.dtypes import register_extension_dtype 

7 

8from pandas.core.arrays.numeric import ( 

9 NumericArray, 

10 NumericDtype, 

11) 

12 

13 

14class FloatingDtype(NumericDtype): 

15 """ 

16 An ExtensionDtype to hold a single size of floating dtype. 

17 

18 These specific implementations are subclasses of the non-public 

19 FloatingDtype. For example we have Float32Dtype to represent float32. 

20 

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

22 """ 

23 

24 _default_np_dtype = np.dtype(np.float64) 

25 _checker = is_float_dtype 

26 

27 @classmethod 

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

29 """ 

30 Return the array type associated with this dtype. 

31 

32 Returns 

33 ------- 

34 type 

35 """ 

36 return FloatingArray 

37 

38 @classmethod 

39 def _str_to_dtype_mapping(cls): 

40 return FLOAT_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. 

48 """ 

49 # This is really only here for compatibility with IntegerDtype 

50 # Here for compat with IntegerDtype 

51 return values.astype(dtype, copy=copy) 

52 

53 

54class FloatingArray(NumericArray): 

55 """ 

56 Array of floating (optional missing) values. 

57 

58 .. versionadded:: 1.2.0 

59 

60 .. warning:: 

61 

62 FloatingArray is currently experimental, and its API or internal 

63 implementation may change without warning. Especially the behaviour 

64 regarding NaN (distinct from NA missing values) is subject to change. 

65 

66 We represent a FloatingArray with 2 numpy arrays: 

67 

68 - data: contains a numpy float array of the appropriate dtype 

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

70 

71 To construct an FloatingArray from generic array-like input, use 

72 :func:`pandas.array` with one of the float dtypes (see examples). 

73 

74 See :ref:`integer_na` for more. 

75 

76 Parameters 

77 ---------- 

78 values : numpy.ndarray 

79 A 1-d float-dtype array. 

80 mask : numpy.ndarray 

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

82 copy : bool, default False 

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

84 

85 Attributes 

86 ---------- 

87 None 

88 

89 Methods 

90 ------- 

91 None 

92 

93 Returns 

94 ------- 

95 FloatingArray 

96 

97 Examples 

98 -------- 

99 Create an FloatingArray with :func:`pandas.array`: 

100 

101 >>> pd.array([0.1, None, 0.3], dtype=pd.Float32Dtype()) 

102 <FloatingArray> 

103 [0.1, <NA>, 0.3] 

104 Length: 3, dtype: Float32 

105 

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

107 

108 >>> pd.array([0.1, None, 0.3], dtype="Float32") 

109 <FloatingArray> 

110 [0.1, <NA>, 0.3] 

111 Length: 3, dtype: Float32 

112 """ 

113 

114 _dtype_cls = FloatingDtype 

115 

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

117 _internal_fill_value = np.nan 

118 # Fill values used for any/all 

119 _truthy_value = 1.0 

120 _falsey_value = 0.0 

121 

122 

123_dtype_docstring = """ 

124An ExtensionDtype for {dtype} data. 

125 

126This dtype uses ``pd.NA`` as missing value indicator. 

127 

128Attributes 

129---------- 

130None 

131 

132Methods 

133------- 

134None 

135""" 

136 

137# create the Dtype 

138 

139 

140@register_extension_dtype 

141class Float32Dtype(FloatingDtype): 

142 type = np.float32 

143 name = "Float32" 

144 __doc__ = _dtype_docstring.format(dtype="float32") 

145 

146 

147@register_extension_dtype 

148class Float64Dtype(FloatingDtype): 

149 type = np.float64 

150 name = "Float64" 

151 __doc__ = _dtype_docstring.format(dtype="float64") 

152 

153 

154FLOAT_STR_TO_DTYPE = { 

155 "float32": Float32Dtype(), 

156 "float64": Float64Dtype(), 

157}