Skip to content

Commit 88d793e

Browse files
committed
add type hints
1 parent c8aea2c commit 88d793e

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

pandas/core/dtypes/cast.py

Lines changed: 38 additions & 16 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 (
@@ -113,7 +113,7 @@ def is_nested_object(obj) -> bool:
113113
return False
114114

115115

116-
def maybe_downcast_to_dtype(result, dtype):
116+
def maybe_downcast_to_dtype(result, dtype: Dtype):
117117
"""
118118
try to cast to the specified dtype (e.g. convert back to bool/int
119119
or could be an astype of float64->float32
@@ -181,7 +181,7 @@ def maybe_downcast_to_dtype(result, dtype):
181181
return result
182182

183183

184-
def maybe_downcast_numeric(result, dtype, do_round: bool = False):
184+
def maybe_downcast_numeric(result, dtype: Dtype, do_round: bool = False):
185185
"""
186186
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
187187
@@ -324,7 +324,9 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
324324
return dtype
325325

326326

327-
def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
327+
def maybe_cast_to_extension_array(
328+
cls: Type["ExtensionArray"], obj, dtype: Dtype = None
329+
):
328330
"""
329331
Call to `_from_sequence` that returns the object unchanged on Exception.
330332
@@ -357,7 +359,9 @@ def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
357359
return result
358360

359361

360-
def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
362+
def maybe_upcast_putmask(
363+
result: np.ndarray, mask: np.ndarray, other: Scalar
364+
) -> Tuple[np.ndarray, bool]:
361365
"""
362366
A safe version of putmask that potentially upcasts the result.
363367
@@ -439,7 +443,7 @@ def changeit():
439443
return result, False
440444

441445

442-
def maybe_promote(dtype, fill_value=np.nan):
446+
def maybe_promote(dtype, fill_value: Scalar = np.nan) -> Tuple[DtypeObj, Scalar]:
443447
"""
444448
Find the minimal dtype that can hold both the given dtype and fill_value.
445449
@@ -595,7 +599,7 @@ def maybe_promote(dtype, fill_value=np.nan):
595599
return dtype, fill_value
596600

597601

598-
def _ensure_dtype_type(value, dtype):
602+
def _ensure_dtype_type(value, dtype: DtypeObj):
599603
"""
600604
Ensure that the given value is an instance of the given dtype.
601605
@@ -722,7 +726,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
722726

723727

724728
# 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]:
729+
def infer_dtype_from_array(
730+
arr, pandas_dtype: bool = False
731+
) -> Tuple[DtypeObj, AnyArrayLike]:
726732
"""
727733
Infer the dtype from an array.
728734
@@ -810,7 +816,12 @@ def maybe_infer_dtype_type(element):
810816
return tipo
811817

812818

813-
def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
819+
def maybe_upcast(
820+
values: ArrayLike,
821+
fill_value: Scalar = np.nan,
822+
dtype: Dtype = None,
823+
copy: bool = False,
824+
) -> Tuple[ArrayLike, Scalar]:
814825
"""
815826
Provide explicit type promotion and coercion.
816827
@@ -822,6 +833,13 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
822833
dtype : if None, then use the dtype of the values, else coerce to this type
823834
copy : bool, default True
824835
If True always make a copy even if no upcast is required.
836+
837+
Returns
838+
-------
839+
values: ndarray or ExtensionArray
840+
the original array, possibly upcast
841+
fill_value:
842+
the fill value, possibly upcast
825843
"""
826844
if not is_scalar(fill_value) and not is_object_dtype(values.dtype):
827845
# We allow arbitrary fill values for object dtype
@@ -842,7 +860,7 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
842860
return values, fill_value
843861

844862

845-
def invalidate_string_dtypes(dtype_set):
863+
def invalidate_string_dtypes(dtype_set: Set):
846864
"""
847865
Change string like dtypes to object for
848866
``DataFrame.select_dtypes()``.
@@ -864,7 +882,7 @@ def coerce_indexer_dtype(indexer, categories):
864882
return ensure_int64(indexer)
865883

866884

867-
def coerce_to_dtypes(result, dtypes):
885+
def coerce_to_dtypes(result: Sequence[Scalar], dtypes: Sequence[Dtype]) -> List[Scalar]:
868886
"""
869887
given a dtypes and a result set, coerce the result elements to the
870888
dtypes
@@ -894,7 +912,9 @@ def conv(r, dtype):
894912
return [conv(r, dtype) for r, dtype in zip(result, dtypes)]
895913

896914

897-
def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
915+
def astype_nansafe(
916+
arr, dtype: DtypeObj, copy: bool = True, skipna: bool = False
917+
) -> ArrayLike:
898918
"""
899919
Cast the elements of an array to a given dtype a nan-safe manner.
900920
@@ -996,7 +1016,9 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
9961016
return arr.view(dtype)
9971017

9981018

999-
def maybe_convert_objects(values: np.ndarray, convert_numeric: bool = True):
1019+
def maybe_convert_objects(
1020+
values: np.ndarray, convert_numeric: bool = True
1021+
) -> Union[np.ndarray, ABCDatetimeIndex]:
10001022
"""
10011023
If we have an object dtype array, try to coerce dates and/or numbers.
10021024
@@ -1117,7 +1139,7 @@ def soft_convert_objects(
11171139

11181140

11191141
def convert_dtypes(
1120-
input_array,
1142+
input_array: AnyArrayLike,
11211143
convert_string: bool = True,
11221144
convert_integer: bool = True,
11231145
convert_boolean: bool = True,
@@ -1183,7 +1205,7 @@ def convert_dtypes(
11831205
return inferred_dtype
11841206

11851207

1186-
def maybe_castable(arr) -> bool:
1208+
def maybe_castable(arr: np.ndarray) -> bool:
11871209
# return False to force a non-fastpath
11881210

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

0 commit comments

Comments
 (0)