floating.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from __future__ import annotations
  2. import numpy as np
  3. from pandas.core.dtypes.base import register_extension_dtype
  4. from pandas.core.dtypes.common import is_float_dtype
  5. from pandas.core.arrays.numeric import (
  6. NumericArray,
  7. NumericDtype,
  8. )
  9. class FloatingDtype(NumericDtype):
  10. """
  11. An ExtensionDtype to hold a single size of floating dtype.
  12. These specific implementations are subclasses of the non-public
  13. FloatingDtype. For example we have Float32Dtype to represent float32.
  14. The attributes name & type are set when these subclasses are created.
  15. """
  16. _default_np_dtype = np.dtype(np.float64)
  17. _checker = is_float_dtype
  18. @classmethod
  19. def construct_array_type(cls) -> type[FloatingArray]:
  20. """
  21. Return the array type associated with this dtype.
  22. Returns
  23. -------
  24. type
  25. """
  26. return FloatingArray
  27. @classmethod
  28. def _str_to_dtype_mapping(cls):
  29. return FLOAT_STR_TO_DTYPE
  30. @classmethod
  31. def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarray:
  32. """
  33. Safely cast the values to the given dtype.
  34. "safe" in this context means the casting is lossless.
  35. """
  36. # This is really only here for compatibility with IntegerDtype
  37. # Here for compat with IntegerDtype
  38. return values.astype(dtype, copy=copy)
  39. class FloatingArray(NumericArray):
  40. """
  41. Array of floating (optional missing) values.
  42. .. versionadded:: 1.2.0
  43. .. warning::
  44. FloatingArray is currently experimental, and its API or internal
  45. implementation may change without warning. Especially the behaviour
  46. regarding NaN (distinct from NA missing values) is subject to change.
  47. We represent a FloatingArray with 2 numpy arrays:
  48. - data: contains a numpy float array of the appropriate dtype
  49. - mask: a boolean array holding a mask on the data, True is missing
  50. To construct an FloatingArray from generic array-like input, use
  51. :func:`pandas.array` with one of the float dtypes (see examples).
  52. See :ref:`integer_na` for more.
  53. Parameters
  54. ----------
  55. values : numpy.ndarray
  56. A 1-d float-dtype array.
  57. mask : numpy.ndarray
  58. A 1-d boolean-dtype array indicating missing values.
  59. copy : bool, default False
  60. Whether to copy the `values` and `mask`.
  61. Attributes
  62. ----------
  63. None
  64. Methods
  65. -------
  66. None
  67. Returns
  68. -------
  69. FloatingArray
  70. Examples
  71. --------
  72. Create an FloatingArray with :func:`pandas.array`:
  73. >>> pd.array([0.1, None, 0.3], dtype=pd.Float32Dtype())
  74. <FloatingArray>
  75. [0.1, <NA>, 0.3]
  76. Length: 3, dtype: Float32
  77. String aliases for the dtypes are also available. They are capitalized.
  78. >>> pd.array([0.1, None, 0.3], dtype="Float32")
  79. <FloatingArray>
  80. [0.1, <NA>, 0.3]
  81. Length: 3, dtype: Float32
  82. """
  83. _dtype_cls = FloatingDtype
  84. # The value used to fill '_data' to avoid upcasting
  85. _internal_fill_value = np.nan
  86. # Fill values used for any/all
  87. # Incompatible types in assignment (expression has type "float", base class
  88. # "BaseMaskedArray" defined the type as "<typing special form>")
  89. _truthy_value = 1.0 # type: ignore[assignment]
  90. _falsey_value = 0.0 # type: ignore[assignment]
  91. _dtype_docstring = """
  92. An ExtensionDtype for {dtype} data.
  93. This dtype uses ``pd.NA`` as missing value indicator.
  94. Attributes
  95. ----------
  96. None
  97. Methods
  98. -------
  99. None
  100. """
  101. # create the Dtype
  102. @register_extension_dtype
  103. class Float32Dtype(FloatingDtype):
  104. type = np.float32
  105. name = "Float32"
  106. __doc__ = _dtype_docstring.format(dtype="float32")
  107. @register_extension_dtype
  108. class Float64Dtype(FloatingDtype):
  109. type = np.float64
  110. name = "Float64"
  111. __doc__ = _dtype_docstring.format(dtype="float64")
  112. FLOAT_STR_TO_DTYPE = {
  113. "float32": Float32Dtype(),
  114. "float64": Float64Dtype(),
  115. }