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_float_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 # Incompatible types in assignment (expression has type "float", base class
120 # "BaseMaskedArray" defined the type as "<typing special form>")
121 _truthy_value = 1.0 # type: ignore[assignment]
122 _falsey_value = 0.0 # type: ignore[assignment]
123
124
125_dtype_docstring = """
126An ExtensionDtype for {dtype} data.
127
128This dtype uses ``pd.NA`` as missing value indicator.
129
130Attributes
131----------
132None
133
134Methods
135-------
136None
137"""
138
139# create the Dtype
140
141
142@register_extension_dtype
143class Float32Dtype(FloatingDtype):
144 type = np.float32
145 name = "Float32"
146 __doc__ = _dtype_docstring.format(dtype="float32")
147
148
149@register_extension_dtype
150class Float64Dtype(FloatingDtype):
151 type = np.float64
152 name = "Float64"
153 __doc__ = _dtype_docstring.format(dtype="float64")
154
155
156FLOAT_STR_TO_DTYPE = {
157 "float32": Float32Dtype(),
158 "float64": Float64Dtype(),
159}