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
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from __future__ import annotations
3import numpy as np
5from pandas.core.dtypes.base import register_extension_dtype
6from pandas.core.dtypes.common import is_integer_dtype
8from pandas.core.arrays.numeric import (
9 NumericArray,
10 NumericDtype,
11)
14class IntegerDtype(NumericDtype):
15 """
16 An ExtensionDtype to hold a single size & kind of integer dtype.
18 These specific implementations are subclasses of the non-public
19 IntegerDtype. For example we have Int8Dtype to represent signed int 8s.
21 The attributes name & type are set when these subclasses are created.
22 """
24 _default_np_dtype = np.dtype(np.int64)
25 _checker = is_integer_dtype
27 @classmethod
28 def construct_array_type(cls) -> type[IntegerArray]:
29 """
30 Return the array type associated with this dtype.
32 Returns
33 -------
34 type
35 """
36 return IntegerArray
38 @classmethod
39 def _str_to_dtype_mapping(cls):
40 return INT_STR_TO_DTYPE
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.
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
57 raise TypeError(
58 f"cannot safely cast non-equivalent {values.dtype} to {np.dtype(dtype)}"
59 ) from err
62class IntegerArray(NumericArray):
63 """
64 Array of integer (optional missing) values.
66 .. versionchanged:: 1.0.0
68 Now uses :attr:`pandas.NA` as the missing value rather
69 than :attr:`numpy.nan`.
71 .. warning::
73 IntegerArray is currently experimental, and its API or internal
74 implementation may change without warning.
76 We represent an IntegerArray with 2 numpy arrays:
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
81 To construct an IntegerArray from generic array-like input, use
82 :func:`pandas.array` with one of the integer dtypes (see examples).
84 See :ref:`integer_na` for more.
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`.
95 Attributes
96 ----------
97 None
99 Methods
100 -------
101 None
103 Returns
104 -------
105 IntegerArray
107 Examples
108 --------
109 Create an IntegerArray with :func:`pandas.array`.
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
117 String aliases for the dtypes are also available. They are capitalized.
119 >>> pd.array([1, None, 3], dtype='Int32')
120 <IntegerArray>
121 [1, <NA>, 3]
122 Length: 3, dtype: Int32
124 >>> pd.array([1, None, 3], dtype='UInt16')
125 <IntegerArray>
126 [1, <NA>, 3]
127 Length: 3, dtype: UInt16
128 """
130 _dtype_cls = IntegerDtype
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
139_dtype_docstring = """
140An ExtensionDtype for {dtype} integer data.
142.. versionchanged:: 1.0.0
144 Now uses :attr:`pandas.NA` as its missing value,
145 rather than :attr:`numpy.nan`.
147Attributes
148----------
149None
151Methods
152-------
153None
154"""
156# create the Dtype
159@register_extension_dtype
160class Int8Dtype(IntegerDtype):
161 type = np.int8
162 name = "Int8"
163 __doc__ = _dtype_docstring.format(dtype="int8")
166@register_extension_dtype
167class Int16Dtype(IntegerDtype):
168 type = np.int16
169 name = "Int16"
170 __doc__ = _dtype_docstring.format(dtype="int16")
173@register_extension_dtype
174class Int32Dtype(IntegerDtype):
175 type = np.int32
176 name = "Int32"
177 __doc__ = _dtype_docstring.format(dtype="int32")
180@register_extension_dtype
181class Int64Dtype(IntegerDtype):
182 type = np.int64
183 name = "Int64"
184 __doc__ = _dtype_docstring.format(dtype="int64")
187@register_extension_dtype
188class UInt8Dtype(IntegerDtype):
189 type = np.uint8
190 name = "UInt8"
191 __doc__ = _dtype_docstring.format(dtype="uint8")
194@register_extension_dtype
195class UInt16Dtype(IntegerDtype):
196 type = np.uint16
197 name = "UInt16"
198 __doc__ = _dtype_docstring.format(dtype="uint16")
201@register_extension_dtype
202class UInt32Dtype(IntegerDtype):
203 type = np.uint32
204 name = "UInt32"
205 __doc__ = _dtype_docstring.format(dtype="uint32")
208@register_extension_dtype
209class UInt64Dtype(IntegerDtype):
210 type = np.uint64
211 name = "UInt64"
212 __doc__ = _dtype_docstring.format(dtype="uint64")
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}