Skip to content

Commit be67919

Browse files
committed
more type hints
1 parent 81ffd4a commit be67919

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

@@ -1220,7 +1231,9 @@ def maybe_castable(arr: np.ndarray) -> bool:
12201231
return arr.dtype.name not in POSSIBLY_CAST_DTYPES
12211232

12221233

1223-
def maybe_infer_to_datetimelike(value, convert_dates: bool = False):
1234+
def maybe_infer_to_datetimelike(
1235+
value: Union[ArrayLike, Scalar], convert_dates: bool = False
1236+
):
12241237
"""
12251238
we might have a array (or single object) that is datetime like,
12261239
and no dtype is passed don't change the value unless we find a
@@ -1329,7 +1342,7 @@ def try_timedelta(v):
13291342
return value
13301343

13311344

1332-
def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
1345+
def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"):
13331346
"""
13341347
try to cast the array/value to a datetimelike dtype, converting float
13351348
nan to iNaT
@@ -1522,7 +1535,9 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
15221535
return np.find_common_type(types, [])
15231536

15241537

1525-
def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.ndarray:
1538+
def cast_scalar_to_array(
1539+
shape: Tuple, value: Scalar, dtype: Optional[DtypeObj] = None
1540+
) -> np.ndarray:
15261541
"""
15271542
Create np.ndarray of specified shape and dtype, filled with values.
15281543
@@ -1550,7 +1565,7 @@ def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.n
15501565

15511566

15521567
def construct_1d_arraylike_from_scalar(
1553-
value, length: int, dtype: DtypeObj
1568+
value: Scalar, length: int, dtype: DtypeObj
15541569
) -> ArrayLike:
15551570
"""
15561571
create a np.ndarray / pandas type of specified shape and dtype
@@ -1594,7 +1609,7 @@ def construct_1d_arraylike_from_scalar(
15941609
return subarr
15951610

15961611

1597-
def construct_1d_object_array_from_listlike(values) -> np.ndarray:
1612+
def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
15981613
"""
15991614
Transform any list-like object in a 1-dimensional numpy array of object
16001615
dtype.
@@ -1620,7 +1635,7 @@ def construct_1d_object_array_from_listlike(values) -> np.ndarray:
16201635

16211636

16221637
def construct_1d_ndarray_preserving_na(
1623-
values, dtype: Optional[DtypeObj] = None, copy: bool = False
1638+
values: Sequence, dtype: Optional[DtypeObj] = None, copy: bool = False
16241639
) -> np.ndarray:
16251640
"""
16261641
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
@@ -1654,7 +1669,7 @@ def construct_1d_ndarray_preserving_na(
16541669
return subarr
16551670

16561671

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

17261741

1727-
def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
1742+
def convert_scalar_for_putitemlike(scalar: Scalar, dtype: np.dtype) -> Scalar:
17281743
"""
17291744
Convert datetimelike scalar if we are setting into a datetime64
17301745
or timedelta64 ndarray.
@@ -1755,7 +1770,7 @@ def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
17551770
return scalar
17561771

17571772

1758-
def validate_numeric_casting(dtype: np.dtype, value):
1773+
def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
17591774
"""
17601775
Check that we can losslessly insert the given value into an array
17611776
with the given dtype.

0 commit comments

Comments
 (0)