3
3
"""
4
4
5
5
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
+ )
7
18
8
19
import numpy as np
9
20
@@ -1284,7 +1295,9 @@ def maybe_castable(arr: np.ndarray) -> bool:
1284
1295
return arr .dtype .name not in POSSIBLY_CAST_DTYPES
1285
1296
1286
1297
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
+ ):
1288
1301
"""
1289
1302
we might have a array (or single object) that is datetime like,
1290
1303
and no dtype is passed don't change the value unless we find a
@@ -1393,7 +1406,7 @@ def try_timedelta(v):
1393
1406
return value
1394
1407
1395
1408
1396
- def maybe_cast_to_datetime (value , dtype , errors : str = "raise" ):
1409
+ def maybe_cast_to_datetime (value , dtype : DtypeObj , errors : str = "raise" ):
1397
1410
"""
1398
1411
try to cast the array/value to a datetimelike dtype, converting float
1399
1412
nan to iNaT
@@ -1586,7 +1599,9 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
1586
1599
return np .find_common_type (types , [])
1587
1600
1588
1601
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 :
1590
1605
"""
1591
1606
Create np.ndarray of specified shape and dtype, filled with values.
1592
1607
@@ -1614,7 +1629,7 @@ def cast_scalar_to_array(shape, value, dtype: Optional[DtypeObj] = None) -> np.n
1614
1629
1615
1630
1616
1631
def construct_1d_arraylike_from_scalar (
1617
- value , length : int , dtype : DtypeObj
1632
+ value : Scalar , length : int , dtype : DtypeObj
1618
1633
) -> ArrayLike :
1619
1634
"""
1620
1635
create a np.ndarray / pandas type of specified shape and dtype
@@ -1658,7 +1673,7 @@ def construct_1d_arraylike_from_scalar(
1658
1673
return subarr
1659
1674
1660
1675
1661
- def construct_1d_object_array_from_listlike (values ) -> np .ndarray :
1676
+ def construct_1d_object_array_from_listlike (values : Sized ) -> np .ndarray :
1662
1677
"""
1663
1678
Transform any list-like object in a 1-dimensional numpy array of object
1664
1679
dtype.
@@ -1684,7 +1699,7 @@ def construct_1d_object_array_from_listlike(values) -> np.ndarray:
1684
1699
1685
1700
1686
1701
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
1688
1703
) -> np .ndarray :
1689
1704
"""
1690
1705
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
@@ -1718,7 +1733,7 @@ def construct_1d_ndarray_preserving_na(
1718
1733
return subarr
1719
1734
1720
1735
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 ):
1722
1737
"""
1723
1738
Takes any dtype and returns the casted version, raising for when data is
1724
1739
incompatible with integer/unsigned integer dtypes.
@@ -1788,7 +1803,7 @@ def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
1788
1803
raise ValueError ("Trying to coerce float values to integers" )
1789
1804
1790
1805
1791
- def convert_scalar_for_putitemlike (scalar , dtype : np .dtype ):
1806
+ def convert_scalar_for_putitemlike (scalar : Scalar , dtype : np .dtype ) -> Scalar :
1792
1807
"""
1793
1808
Convert datetimelike scalar if we are setting into a datetime64
1794
1809
or timedelta64 ndarray.
@@ -1819,7 +1834,7 @@ def convert_scalar_for_putitemlike(scalar, dtype: np.dtype):
1819
1834
return scalar
1820
1835
1821
1836
1822
- def validate_numeric_casting (dtype : np .dtype , value ) :
1837
+ def validate_numeric_casting (dtype : np .dtype , value : Scalar ) -> None :
1823
1838
"""
1824
1839
Check that we can losslessly insert the given value into an array
1825
1840
with the given dtype.
0 commit comments