Skip to content

Commit 8706ee1

Browse files
committed
add type hints
1 parent c8aea2c commit 8706ee1

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

pandas/core/dtypes/cast.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from datetime import date, datetime, timedelta
6-
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type
6+
from typing import TYPE_CHECKING, Any, List, Optional, Sequence, Set, Tuple, Type, Union
77

88
import numpy as np
99

@@ -18,7 +18,7 @@
1818
ints_to_pydatetime,
1919
)
2020
from pandas._libs.tslibs.timezones import tz_compare
21-
from pandas._typing import ArrayLike, Dtype, DtypeObj
21+
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar
2222
from pandas.util._validators import validate_bool_kwarg
2323

2424
from pandas.core.dtypes.common import (
@@ -78,6 +78,7 @@
7878
if TYPE_CHECKING:
7979
from pandas import Series
8080
from pandas.core.arrays import ExtensionArray
81+
from pandas.core.indexes.base import Index
8182

8283
_int8_max = np.iinfo(np.int8).max
8384
_int16_max = np.iinfo(np.int16).max
@@ -113,7 +114,7 @@ def is_nested_object(obj) -> bool:
113114
return False
114115

115116

116-
def maybe_downcast_to_dtype(result, dtype):
117+
def maybe_downcast_to_dtype(result, dtype: Dtype):
117118
"""
118119
try to cast to the specified dtype (e.g. convert back to bool/int
119120
or could be an astype of float64->float32
@@ -181,7 +182,7 @@ def maybe_downcast_to_dtype(result, dtype):
181182
return result
182183

183184

184-
def maybe_downcast_numeric(result, dtype, do_round: bool = False):
185+
def maybe_downcast_numeric(result, dtype: Dtype, do_round: bool = False):
185186
"""
186187
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
187188
@@ -324,7 +325,9 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
324325
return dtype
325326

326327

327-
def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
328+
def maybe_cast_to_extension_array(
329+
cls: Type["ExtensionArray"], obj, dtype: Dtype = None
330+
):
328331
"""
329332
Call to `_from_sequence` that returns the object unchanged on Exception.
330333
@@ -357,7 +360,9 @@ def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
357360
return result
358361

359362

360-
def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
363+
def maybe_upcast_putmask(
364+
result: np.ndarray, mask: np.ndarray, other: Scalar
365+
) -> Tuple[np.ndarray, bool]:
361366
"""
362367
A safe version of putmask that potentially upcasts the result.
363368
@@ -439,7 +444,7 @@ def changeit():
439444
return result, False
440445

441446

442-
def maybe_promote(dtype, fill_value=np.nan):
447+
def maybe_promote(dtype, fill_value: Scalar = np.nan) -> Tuple[DtypeObj, Scalar]:
443448
"""
444449
Find the minimal dtype that can hold both the given dtype and fill_value.
445450
@@ -595,7 +600,7 @@ def maybe_promote(dtype, fill_value=np.nan):
595600
return dtype, fill_value
596601

597602

598-
def _ensure_dtype_type(value, dtype):
603+
def _ensure_dtype_type(value, dtype: DtypeObj):
599604
"""
600605
Ensure that the given value is an instance of the given dtype.
601606
@@ -722,7 +727,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
722727

723728

724729
# TODO: try to make the Any in the return annotation more specific
725-
def infer_dtype_from_array(arr, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
730+
def infer_dtype_from_array(
731+
arr, pandas_dtype: bool = False
732+
) -> Tuple[DtypeObj, AnyArrayLike]:
726733
"""
727734
Infer the dtype from an array.
728735
@@ -810,7 +817,12 @@ def maybe_infer_dtype_type(element):
810817
return tipo
811818

812819

813-
def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
820+
def maybe_upcast(
821+
values: ArrayLike,
822+
fill_value: Scalar = np.nan,
823+
dtype: Dtype = None,
824+
copy: bool = False,
825+
) -> Tuple[ArrayLike, Scalar]:
814826
"""
815827
Provide explicit type promotion and coercion.
816828
@@ -822,6 +834,13 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
822834
dtype : if None, then use the dtype of the values, else coerce to this type
823835
copy : bool, default True
824836
If True always make a copy even if no upcast is required.
837+
838+
Returns
839+
-------
840+
values: ndarray or ExtensionArray
841+
the original array, possibly upcast
842+
fill_value:
843+
the fill value, possibly upcast
825844
"""
826845
if not is_scalar(fill_value) and not is_object_dtype(values.dtype):
827846
# We allow arbitrary fill values for object dtype
@@ -842,7 +861,7 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
842861
return values, fill_value
843862

844863

845-
def invalidate_string_dtypes(dtype_set):
864+
def invalidate_string_dtypes(dtype_set: Set):
846865
"""
847866
Change string like dtypes to object for
848867
``DataFrame.select_dtypes()``.
@@ -852,7 +871,7 @@ def invalidate_string_dtypes(dtype_set):
852871
raise TypeError("string dtypes are not allowed, use 'object' instead")
853872

854873

855-
def coerce_indexer_dtype(indexer, categories):
874+
def coerce_indexer_dtype(indexer: Index, categories) -> Index:
856875
""" coerce the indexer input array to the smallest dtype possible """
857876
length = len(categories)
858877
if length < _int8_max:
@@ -864,7 +883,7 @@ def coerce_indexer_dtype(indexer, categories):
864883
return ensure_int64(indexer)
865884

866885

867-
def coerce_to_dtypes(result, dtypes):
886+
def coerce_to_dtypes(result: Sequence[Scalar], dtypes: Sequence[Dtype]) -> List[Scalar]:
868887
"""
869888
given a dtypes and a result set, coerce the result elements to the
870889
dtypes
@@ -894,7 +913,9 @@ def conv(r, dtype):
894913
return [conv(r, dtype) for r, dtype in zip(result, dtypes)]
895914

896915

897-
def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
916+
def astype_nansafe(
917+
arr, dtype: DtypeObj, copy: bool = True, skipna: bool = False
918+
) -> ArrayLike:
898919
"""
899920
Cast the elements of an array to a given dtype a nan-safe manner.
900921
@@ -996,7 +1017,9 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
9961017
return arr.view(dtype)
9971018

9981019

999-
def maybe_convert_objects(values: np.ndarray, convert_numeric: bool = True):
1020+
def maybe_convert_objects(
1021+
values: np.ndarray, convert_numeric: bool = True
1022+
) -> Union[np.ndarray, ABCDatetimeIndex]:
10001023
"""
10011024
If we have an object dtype array, try to coerce dates and/or numbers.
10021025
@@ -1117,7 +1140,7 @@ def soft_convert_objects(
11171140

11181141

11191142
def convert_dtypes(
1120-
input_array,
1143+
input_array: AnyArrayLike,
11211144
convert_string: bool = True,
11221145
convert_integer: bool = True,
11231146
convert_boolean: bool = True,
@@ -1183,7 +1206,7 @@ def convert_dtypes(
11831206
return inferred_dtype
11841207

11851208

1186-
def maybe_castable(arr) -> bool:
1209+
def maybe_castable(arr: np.ndarray) -> bool:
11871210
# return False to force a non-fastpath
11881211

11891212
# check datetime64[ns]/timedelta64[ns] are valid

0 commit comments

Comments
 (0)