Skip to content

Commit 4ae79be

Browse files
committed
more type hints
1 parent 19c76fc commit 4ae79be

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

pandas/core/dtypes/cast.py

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

55
from datetime import date, datetime, timedelta
6-
from typing import TYPE_CHECKING, Any, List, Optional, Sequence, Set, Tuple, Type, Union
6+
from typing import (
7+
TYPE_CHECKING,
8+
Any,
9+
List,
10+
Optional,
11+
Sequence,
12+
Set,
13+
Sized,
14+
Tuple,
15+
Type,
16+
Union,
17+
)
718

819
import numpy as np
920

@@ -1287,7 +1298,9 @@ def maybe_castable(arr: np.ndarray) -> bool:
12871298
return arr.dtype.name not in POSSIBLY_CAST_DTYPES
12881299

12891300

1290-
def maybe_infer_to_datetimelike(value, convert_dates: bool = False):
1301+
def maybe_infer_to_datetimelike(
1302+
value: Union[ArrayLike, Scalar], convert_dates: bool = False
1303+
):
12911304
"""
12921305
we might have a array (or single object) that is datetime like,
12931306
and no dtype is passed don't change the value unless we find a
@@ -1396,7 +1409,7 @@ def try_timedelta(v):
13961409
return value
13971410

13981411

1399-
def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
1412+
def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"):
14001413
"""
14011414
try to cast the array/value to a datetimelike dtype, converting float
14021415
nan to iNaT
@@ -1589,7 +1602,9 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
15891602
return np.find_common_type(types, [])
15901603

15911604

1592-
def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.ndarray:
1605+
def cast_scalar_to_array(
1606+
shape: Tuple, value: Scalar, dtype: Optional[DtypeObj] = None
1607+
) -> np.ndarray:
15931608
"""
15941609
Create np.ndarray of specified shape and dtype, filled with values.
15951610
@@ -1617,7 +1632,7 @@ def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.n
16171632

16181633

16191634
def construct_1d_arraylike_from_scalar(
1620-
value, length: int, dtype: DtypeObj
1635+
value: Scalar, length: int, dtype: DtypeObj
16211636
) -> ArrayLike:
16221637
"""
16231638
create a np.ndarray / pandas type of specified shape and dtype
@@ -1661,7 +1676,7 @@ def construct_1d_arraylike_from_scalar(
16611676
return subarr
16621677

16631678

1664-
def construct_1d_object_array_from_listlike(values) -> np.ndarray:
1679+
def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
16651680
"""
16661681
Transform any list-like object in a 1-dimensional numpy array of object
16671682
dtype.
@@ -1687,7 +1702,7 @@ def construct_1d_object_array_from_listlike(values) -> np.ndarray:
16871702

16881703

16891704
def construct_1d_ndarray_preserving_na(
1690-
values, dtype: Optional[DtypeObj] = None, copy: bool = False
1705+
values: Sequence, dtype: Optional[DtypeObj] = None, copy: bool = False
16911706
) -> np.ndarray:
16921707
"""
16931708
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
@@ -1721,7 +1736,7 @@ def construct_1d_ndarray_preserving_na(
17211736
return subarr
17221737

17231738

1724-
def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
1739+
def maybe_cast_to_integer_array(arr, dtype: Union[str, np.dtype], copy: bool = False):
17251740
"""
17261741
Takes any dtype and returns the casted version, raising for when data is
17271742
incompatible with integer/unsigned integer dtypes.
@@ -1791,7 +1806,7 @@ def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
17911806
raise ValueError("Trying to coerce float values to integers")
17921807

17931808

1794-
def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
1809+
def convert_scalar_for_putitemlike(scalar: Scalar, dtype: np.dtype) -> Scalar:
17951810
"""
17961811
Convert datetimelike scalar if we are setting into a datetime64
17971812
or timedelta64 ndarray.
@@ -1822,7 +1837,7 @@ def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
18221837
return scalar
18231838

18241839

1825-
def validate_numeric_casting(dtype: np.dtype, value):
1840+
def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
18261841
"""
18271842
Check that we can losslessly insert the given value into an array
18281843
with the given dtype.

0 commit comments

Comments
 (0)