Skip to content

Commit 85e4d6f

Browse files
TYP: core.arrays.integer (#31347)
1 parent 1aebc99 commit 85e4d6f

File tree

1 file changed

+82
-109
lines changed

1 file changed

+82
-109
lines changed

pandas/core/arrays/integer.py

+82-109
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numbers
2-
from typing import Any, Tuple, Type
2+
from typing import TYPE_CHECKING, Any, Dict, Tuple, Type, Union
33
import warnings
44

55
import numpy as np
@@ -31,6 +31,9 @@
3131

3232
from .masked import BaseMaskedArray
3333

34+
if TYPE_CHECKING:
35+
import pyarrow # noqa: F401
36+
3437

3538
class _IntegerDtype(ExtensionDtype):
3639
"""
@@ -52,33 +55,33 @@ def __repr__(self) -> str:
5255
return f"{sign}Int{8 * self.itemsize}Dtype()"
5356

5457
@cache_readonly
55-
def is_signed_integer(self):
58+
def is_signed_integer(self) -> bool:
5659
return self.kind == "i"
5760

5861
@cache_readonly
59-
def is_unsigned_integer(self):
62+
def is_unsigned_integer(self) -> bool:
6063
return self.kind == "u"
6164

6265
@property
63-
def _is_numeric(self):
66+
def _is_numeric(self) -> bool:
6467
return True
6568

6669
@cache_readonly
67-
def numpy_dtype(self):
70+
def numpy_dtype(self) -> np.dtype:
6871
""" Return an instance of our numpy dtype """
6972
return np.dtype(self.type)
7073

7174
@cache_readonly
72-
def kind(self):
75+
def kind(self) -> str:
7376
return self.numpy_dtype.kind
7477

7578
@cache_readonly
76-
def itemsize(self):
79+
def itemsize(self) -> int:
7780
""" Return the number of bytes in this dtype """
7881
return self.numpy_dtype.itemsize
7982

8083
@classmethod
81-
def construct_array_type(cls):
84+
def construct_array_type(cls) -> Type["IntegerArray"]:
8285
"""
8386
Return the array type associated with this dtype.
8487
@@ -88,9 +91,13 @@ def construct_array_type(cls):
8891
"""
8992
return IntegerArray
9093

91-
def __from_arrow__(self, array):
92-
"""Construct IntegerArray from passed pyarrow Array/ChunkedArray"""
93-
import pyarrow
94+
def __from_arrow__(
95+
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"]
96+
) -> "IntegerArray":
97+
"""
98+
Construct IntegerArray from pyarrow Array/ChunkedArray.
99+
"""
100+
import pyarrow # noqa: F811
94101
from pandas.core.arrays._arrow_utils import pyarrow_array_to_numpy_and_mask
95102

96103
if isinstance(array, pyarrow.Array):
@@ -108,7 +115,7 @@ def __from_arrow__(self, array):
108115
return IntegerArray._concat_same_type(results)
109116

110117

111-
def integer_array(values, dtype=None, copy=False):
118+
def integer_array(values, dtype=None, copy: bool = False,) -> "IntegerArray":
112119
"""
113120
Infer and return an integer array of the values.
114121
@@ -131,7 +138,7 @@ def integer_array(values, dtype=None, copy=False):
131138
return IntegerArray(values, mask)
132139

133140

134-
def safe_cast(values, dtype, copy):
141+
def safe_cast(values, dtype, copy: bool):
135142
"""
136143
Safely cast the values to the dtype if they
137144
are equivalent, meaning floats must be equivalent to the
@@ -152,7 +159,9 @@ def safe_cast(values, dtype, copy):
152159
)
153160

154161

155-
def coerce_to_array(values, dtype, mask=None, copy=False):
162+
def coerce_to_array(
163+
values, dtype, mask=None, copy: bool = False,
164+
) -> Tuple[np.ndarray, np.ndarray]:
156165
"""
157166
Coerce the input values array to numpy arrays with a mask
158167
@@ -322,10 +331,10 @@ class IntegerArray(BaseMaskedArray):
322331
_internal_fill_value = 1
323332

324333
@cache_readonly
325-
def dtype(self):
334+
def dtype(self) -> _IntegerDtype:
326335
return _dtypes[str(self._data.dtype)]
327336

328-
def __init__(self, values, mask, copy=False):
337+
def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
329338
if not (isinstance(values, np.ndarray) and is_integer_dtype(values.dtype)):
330339
raise TypeError(
331340
"values should be integer numpy array. Use "
@@ -345,21 +354,23 @@ def __init__(self, values, mask, copy=False):
345354
self._mask = mask
346355

347356
@classmethod
348-
def _from_sequence(cls, scalars, dtype=None, copy=False):
357+
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "IntegerArray":
349358
return integer_array(scalars, dtype=dtype, copy=copy)
350359

351360
@classmethod
352-
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
361+
def _from_sequence_of_strings(
362+
cls, strings, dtype=None, copy: bool = False
363+
) -> "IntegerArray":
353364
scalars = to_numeric(strings, errors="raise")
354365
return cls._from_sequence(scalars, dtype, copy)
355366

356367
@classmethod
357-
def _from_factorized(cls, values, original):
368+
def _from_factorized(cls, values, original) -> "IntegerArray":
358369
return integer_array(values, dtype=original.dtype)
359370

360371
_HANDLED_TYPES = (np.ndarray, numbers.Number)
361372

362-
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
373+
def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
363374
# For IntegerArray inputs, we apply the ufunc to ._data
364375
# and mask the result.
365376
if method == "reduce":
@@ -697,103 +708,65 @@ def integer_arithmetic_method(self, other):
697708
"""
698709

699710
# create the Dtype
700-
Int8Dtype = register_extension_dtype(
701-
type(
702-
"Int8Dtype",
703-
(_IntegerDtype,),
704-
{
705-
"type": np.int8,
706-
"name": "Int8",
707-
"__doc__": _dtype_docstring.format(dtype="int8"),
708-
},
709-
)
710-
)
711711

712-
Int16Dtype = register_extension_dtype(
713-
type(
714-
"Int16Dtype",
715-
(_IntegerDtype,),
716-
{
717-
"type": np.int16,
718-
"name": "Int16",
719-
"__doc__": _dtype_docstring.format(dtype="int16"),
720-
},
721-
)
722-
)
723712

724-
Int32Dtype = register_extension_dtype(
725-
type(
726-
"Int32Dtype",
727-
(_IntegerDtype,),
728-
{
729-
"type": np.int32,
730-
"name": "Int32",
731-
"__doc__": _dtype_docstring.format(dtype="int32"),
732-
},
733-
)
734-
)
713+
@register_extension_dtype
714+
class Int8Dtype(_IntegerDtype):
715+
type = np.int8
716+
name = "Int8"
717+
__doc__ = _dtype_docstring.format(dtype="int8")
735718

736-
Int64Dtype = register_extension_dtype(
737-
type(
738-
"Int64Dtype",
739-
(_IntegerDtype,),
740-
{
741-
"type": np.int64,
742-
"name": "Int64",
743-
"__doc__": _dtype_docstring.format(dtype="int64"),
744-
},
745-
)
746-
)
747719

748-
UInt8Dtype = register_extension_dtype(
749-
type(
750-
"UInt8Dtype",
751-
(_IntegerDtype,),
752-
{
753-
"type": np.uint8,
754-
"name": "UInt8",
755-
"__doc__": _dtype_docstring.format(dtype="uint8"),
756-
},
757-
)
758-
)
720+
@register_extension_dtype
721+
class Int16Dtype(_IntegerDtype):
722+
type = np.int16
723+
name = "Int16"
724+
__doc__ = _dtype_docstring.format(dtype="int16")
759725

760-
UInt16Dtype = register_extension_dtype(
761-
type(
762-
"UInt16Dtype",
763-
(_IntegerDtype,),
764-
{
765-
"type": np.uint16,
766-
"name": "UInt16",
767-
"__doc__": _dtype_docstring.format(dtype="uint16"),
768-
},
769-
)
770-
)
771726

772-
UInt32Dtype = register_extension_dtype(
773-
type(
774-
"UInt32Dtype",
775-
(_IntegerDtype,),
776-
{
777-
"type": np.uint32,
778-
"name": "UInt32",
779-
"__doc__": _dtype_docstring.format(dtype="uint32"),
780-
},
781-
)
782-
)
727+
@register_extension_dtype
728+
class Int32Dtype(_IntegerDtype):
729+
type = np.int32
730+
name = "Int32"
731+
__doc__ = _dtype_docstring.format(dtype="int32")
732+
733+
734+
@register_extension_dtype
735+
class Int64Dtype(_IntegerDtype):
736+
type = np.int64
737+
name = "Int64"
738+
__doc__ = _dtype_docstring.format(dtype="int64")
739+
740+
741+
@register_extension_dtype
742+
class UInt8Dtype(_IntegerDtype):
743+
type = np.uint8
744+
name = "UInt8"
745+
__doc__ = _dtype_docstring.format(dtype="uint8")
746+
747+
748+
@register_extension_dtype
749+
class UInt16Dtype(_IntegerDtype):
750+
type = np.uint16
751+
name = "UInt16"
752+
__doc__ = _dtype_docstring.format(dtype="uint16")
753+
754+
755+
@register_extension_dtype
756+
class UInt32Dtype(_IntegerDtype):
757+
type = np.uint32
758+
name = "UInt32"
759+
__doc__ = _dtype_docstring.format(dtype="uint32")
760+
761+
762+
@register_extension_dtype
763+
class UInt64Dtype(_IntegerDtype):
764+
type = np.uint64
765+
name = "UInt64"
766+
__doc__ = _dtype_docstring.format(dtype="uint64")
783767

784-
UInt64Dtype = register_extension_dtype(
785-
type(
786-
"UInt64Dtype",
787-
(_IntegerDtype,),
788-
{
789-
"type": np.uint64,
790-
"name": "UInt64",
791-
"__doc__": _dtype_docstring.format(dtype="uint64"),
792-
},
793-
)
794-
)
795768

796-
_dtypes = {
769+
_dtypes: Dict[str, _IntegerDtype] = {
797770
"int8": Int8Dtype(),
798771
"int16": Int16Dtype(),
799772
"int32": Int32Dtype(),

0 commit comments

Comments
 (0)