3
3
"""
4
4
5
5
from datetime import date , datetime , timedelta
6
- from typing import TYPE_CHECKING , Type
6
+ from typing import TYPE_CHECKING , Any , Optional , Tuple , Type
7
7
8
8
import numpy as np
9
9
17
17
iNaT ,
18
18
)
19
19
from pandas ._libs .tslibs .timezones import tz_compare
20
- from pandas ._typing import Dtype , DtypeObj
20
+ from pandas ._typing import ArrayLike , Dtype , DtypeObj
21
21
from pandas .util ._validators import validate_bool_kwarg
22
22
23
23
from pandas .core .dtypes .common import (
@@ -613,7 +613,7 @@ def _ensure_dtype_type(value, dtype):
613
613
return dtype .type (value )
614
614
615
615
616
- def infer_dtype_from (val , pandas_dtype : bool = False ):
616
+ def infer_dtype_from (val , pandas_dtype : bool = False ) -> Tuple [ DtypeObj , Any ] :
617
617
"""
618
618
Interpret the dtype from a scalar or array.
619
619
@@ -630,7 +630,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False):
630
630
return infer_dtype_from_array (val , pandas_dtype = pandas_dtype )
631
631
632
632
633
- def infer_dtype_from_scalar (val , pandas_dtype : bool = False ):
633
+ def infer_dtype_from_scalar (val , pandas_dtype : bool = False ) -> Tuple [ DtypeObj , Any ] :
634
634
"""
635
635
Interpret the dtype from a scalar.
636
636
@@ -641,7 +641,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
641
641
If False, scalar belongs to pandas extension types is inferred as
642
642
object
643
643
"""
644
- dtype = np .object_
644
+ dtype = np .dtype ( object )
645
645
646
646
# a 1-element ndarray
647
647
if isinstance (val , np .ndarray ):
@@ -660,7 +660,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
660
660
# instead of np.empty (but then you still don't want things
661
661
# coming out as np.str_!
662
662
663
- dtype = np .object_
663
+ dtype = np .dtype ( object )
664
664
665
665
elif isinstance (val , (np .datetime64 , datetime )):
666
666
val = tslibs .Timestamp (val )
@@ -671,30 +671,30 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
671
671
dtype = DatetimeTZDtype (unit = "ns" , tz = val .tz )
672
672
else :
673
673
# return datetimetz as object
674
- return np .object_ , val
674
+ return np .dtype ( object ) , val
675
675
val = val .value
676
676
677
677
elif isinstance (val , (np .timedelta64 , timedelta )):
678
678
val = tslibs .Timedelta (val ).value
679
679
dtype = np .dtype ("m8[ns]" )
680
680
681
681
elif is_bool (val ):
682
- dtype = np .bool_
682
+ dtype = np .dtype ( np . bool_ )
683
683
684
684
elif is_integer (val ):
685
685
if isinstance (val , np .integer ):
686
- dtype = type (val )
686
+ dtype = np . dtype ( type (val ) )
687
687
else :
688
- dtype = np .int64
688
+ dtype = np .dtype ( np . int64 )
689
689
690
690
elif is_float (val ):
691
691
if isinstance (val , np .floating ):
692
- dtype = type (val )
692
+ dtype = np . dtype ( type (val ) )
693
693
else :
694
- dtype = np .float64
694
+ dtype = np .dtype ( np . float64 )
695
695
696
696
elif is_complex (val ):
697
- dtype = np .complex_
697
+ dtype = np .dtype ( np . complex_ )
698
698
699
699
elif pandas_dtype :
700
700
if lib .is_period (val ):
@@ -707,7 +707,8 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
707
707
return dtype , val
708
708
709
709
710
- def infer_dtype_from_array (arr , pandas_dtype : bool = False ):
710
+ # TODO: try to make the Any in the return annotation more specific
711
+ def infer_dtype_from_array (arr , pandas_dtype : bool = False ) -> Tuple [DtypeObj , Any ]:
711
712
"""
712
713
Infer the dtype from an array.
713
714
@@ -738,7 +739,7 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):
738
739
array(['1', '1'], dtype='<U21')
739
740
740
741
>>> infer_dtype_from_array([1, '1'])
741
- (<class 'numpy.object_'> , [1, '1'])
742
+ (dtype('O') , [1, '1'])
742
743
"""
743
744
if isinstance (arr , np .ndarray ):
744
745
return arr .dtype , arr
@@ -755,7 +756,7 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):
755
756
# don't force numpy coerce with nan's
756
757
inferred = lib .infer_dtype (arr , skipna = False )
757
758
if inferred in ["string" , "bytes" , "mixed" , "mixed-integer" ]:
758
- return (np .object_ , arr )
759
+ return (np .dtype ( np . object_ ) , arr )
759
760
760
761
arr = np .asarray (arr )
761
762
return arr .dtype , arr
@@ -1469,7 +1470,7 @@ def find_common_type(types):
1469
1470
return np .find_common_type (types , [])
1470
1471
1471
1472
1472
- def cast_scalar_to_array (shape , value , dtype = None ):
1473
+ def cast_scalar_to_array (shape , value , dtype : Optional [ DtypeObj ] = None ) -> np . ndarray :
1473
1474
"""
1474
1475
Create np.ndarray of specified shape and dtype, filled with values.
1475
1476
@@ -1496,7 +1497,9 @@ def cast_scalar_to_array(shape, value, dtype=None):
1496
1497
return values
1497
1498
1498
1499
1499
- def construct_1d_arraylike_from_scalar (value , length : int , dtype ):
1500
+ def construct_1d_arraylike_from_scalar (
1501
+ value , length : int , dtype : DtypeObj
1502
+ ) -> ArrayLike :
1500
1503
"""
1501
1504
create a np.ndarray / pandas type of specified shape and dtype
1502
1505
filled with values
@@ -1505,7 +1508,7 @@ def construct_1d_arraylike_from_scalar(value, length: int, dtype):
1505
1508
----------
1506
1509
value : scalar value
1507
1510
length : int
1508
- dtype : pandas_dtype / np.dtype
1511
+ dtype : pandas_dtype or np.dtype
1509
1512
1510
1513
Returns
1511
1514
-------
@@ -1517,8 +1520,6 @@ def construct_1d_arraylike_from_scalar(value, length: int, dtype):
1517
1520
subarr = cls ._from_sequence ([value ] * length , dtype = dtype )
1518
1521
1519
1522
else :
1520
- if not isinstance (dtype , (np .dtype , type (np .dtype ))):
1521
- dtype = dtype .dtype
1522
1523
1523
1524
if length and is_integer_dtype (dtype ) and isna (value ):
1524
1525
# coerce if we have nan for an integer dtype
@@ -1536,7 +1537,7 @@ def construct_1d_arraylike_from_scalar(value, length: int, dtype):
1536
1537
return subarr
1537
1538
1538
1539
1539
- def construct_1d_object_array_from_listlike (values ):
1540
+ def construct_1d_object_array_from_listlike (values ) -> np . ndarray :
1540
1541
"""
1541
1542
Transform any list-like object in a 1-dimensional numpy array of object
1542
1543
dtype.
@@ -1561,7 +1562,9 @@ def construct_1d_object_array_from_listlike(values):
1561
1562
return result
1562
1563
1563
1564
1564
- def construct_1d_ndarray_preserving_na (values , dtype = None , copy : bool = False ):
1565
+ def construct_1d_ndarray_preserving_na (
1566
+ values , dtype : Optional [DtypeObj ] = None , copy : bool = False
1567
+ ) -> np .ndarray :
1565
1568
"""
1566
1569
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
1567
1570
0 commit comments