Skip to content

Commit 4d13d23

Browse files
committed
more type hints
1 parent b3cd956 commit 4d13d23

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

@@ -1284,7 +1295,9 @@ def maybe_castable(arr: np.ndarray) -> bool:
12841295
return arr.dtype.name not in POSSIBLY_CAST_DTYPES
12851296

12861297

1287-
def maybe_infer_to_datetimelike(value, convert_dates: bool = False):
1298+
def maybe_infer_to_datetimelike(
1299+
value: Union[ArrayLike, Scalar], convert_dates: bool = False
1300+
):
12881301
"""
12891302
we might have a array (or single object) that is datetime like,
12901303
and no dtype is passed don't change the value unless we find a
@@ -1393,7 +1406,7 @@ def try_timedelta(v):
13931406
return value
13941407

13951408

1396-
def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
1409+
def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"):
13971410
"""
13981411
try to cast the array/value to a datetimelike dtype, converting float
13991412
nan to iNaT
@@ -1586,7 +1599,9 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
15861599
return np.find_common_type(types, [])
15871600

15881601

1589-
def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.ndarray:
1602+
def cast_scalar_to_array(
1603+
shape: Tuple, value: Scalar, dtype: Optional[DtypeObj] = None
1604+
) -> np.ndarray:
15901605
"""
15911606
Create np.ndarray of specified shape and dtype, filled with values.
15921607
@@ -1614,7 +1629,7 @@ def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.n
16141629

16151630

16161631
def construct_1d_arraylike_from_scalar(
1617-
value, length: int, dtype: DtypeObj
1632+
value: Scalar, length: int, dtype: DtypeObj
16181633
) -> ArrayLike:
16191634
"""
16201635
create a np.ndarray / pandas type of specified shape and dtype
@@ -1658,7 +1673,7 @@ def construct_1d_arraylike_from_scalar(
16581673
return subarr
16591674

16601675

1661-
def construct_1d_object_array_from_listlike(values) -> np.ndarray:
1676+
def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
16621677
"""
16631678
Transform any list-like object in a 1-dimensional numpy array of object
16641679
dtype.
@@ -1684,7 +1699,7 @@ def construct_1d_object_array_from_listlike(values) -> np.ndarray:
16841699

16851700

16861701
def construct_1d_ndarray_preserving_na(
1687-
values, dtype: Optional[DtypeObj] = None, copy: bool = False
1702+
values: Sequence, dtype: Optional[DtypeObj] = None, copy: bool = False
16881703
) -> np.ndarray:
16891704
"""
16901705
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
@@ -1718,7 +1733,7 @@ def construct_1d_ndarray_preserving_na(
17181733
return subarr
17191734

17201735

1721-
def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
1736+
def maybe_cast_to_integer_array(arr, dtype: Union[str, np.dtype], copy: bool = False):
17221737
"""
17231738
Takes any dtype and returns the casted version, raising for when data is
17241739
incompatible with integer/unsigned integer dtypes.
@@ -1788,7 +1803,7 @@ def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
17881803
raise ValueError("Trying to coerce float values to integers")
17891804

17901805

1791-
def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
1806+
def convert_scalar_for_putitemlike(scalar: Scalar, dtype: np.dtype) -> Scalar:
17921807
"""
17931808
Convert datetimelike scalar if we are setting into a datetime64
17941809
or timedelta64 ndarray.
@@ -1819,7 +1834,7 @@ def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
18191834
return scalar
18201835

18211836

1822-
def validate_numeric_casting(dtype: np.dtype, value):
1837+
def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
18231838
"""
18241839
Check that we can losslessly insert the given value into an array
18251840
with the given dtype.

0 commit comments

Comments
 (0)