33"""
44
55from datetime import date , datetime , timedelta
6- from typing import TYPE_CHECKING , Type
6+ from typing import TYPE_CHECKING , Any , Optional , Tuple , Type
77
88import numpy as np
99
1717 iNaT ,
1818)
1919from pandas ._libs .tslibs .timezones import tz_compare
20- from pandas ._typing import Dtype , DtypeObj
20+ from pandas ._typing import ArrayLike , Dtype , DtypeObj
2121from pandas .util ._validators import validate_bool_kwarg
2222
2323from pandas .core .dtypes .common import (
@@ -613,7 +613,7 @@ def _ensure_dtype_type(value, dtype):
613613 return dtype .type (value )
614614
615615
616- def infer_dtype_from (val , pandas_dtype : bool = False ):
616+ def infer_dtype_from (val , pandas_dtype : bool = False ) -> Tuple [ DtypeObj , Any ] :
617617 """
618618 Interpret the dtype from a scalar or array.
619619
@@ -630,7 +630,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False):
630630 return infer_dtype_from_array (val , pandas_dtype = pandas_dtype )
631631
632632
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 ] :
634634 """
635635 Interpret the dtype from a scalar.
636636
@@ -641,7 +641,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
641641 If False, scalar belongs to pandas extension types is inferred as
642642 object
643643 """
644- dtype = np .object_
644+ dtype = np .dtype ( object )
645645
646646 # a 1-element ndarray
647647 if isinstance (val , np .ndarray ):
@@ -660,7 +660,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
660660 # instead of np.empty (but then you still don't want things
661661 # coming out as np.str_!
662662
663- dtype = np .object_
663+ dtype = np .dtype ( object )
664664
665665 elif isinstance (val , (np .datetime64 , datetime )):
666666 val = tslibs .Timestamp (val )
@@ -671,30 +671,30 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
671671 dtype = DatetimeTZDtype (unit = "ns" , tz = val .tz )
672672 else :
673673 # return datetimetz as object
674- return np .object_ , val
674+ return np .dtype ( object ) , val
675675 val = val .value
676676
677677 elif isinstance (val , (np .timedelta64 , timedelta )):
678678 val = tslibs .Timedelta (val ).value
679679 dtype = np .dtype ("m8[ns]" )
680680
681681 elif is_bool (val ):
682- dtype = np .bool_
682+ dtype = np .dtype ( np . bool_ )
683683
684684 elif is_integer (val ):
685685 if isinstance (val , np .integer ):
686- dtype = type (val )
686+ dtype = np . dtype ( type (val ) )
687687 else :
688- dtype = np .int64
688+ dtype = np .dtype ( np . int64 )
689689
690690 elif is_float (val ):
691691 if isinstance (val , np .floating ):
692- dtype = type (val )
692+ dtype = np . dtype ( type (val ) )
693693 else :
694- dtype = np .float64
694+ dtype = np .dtype ( np . float64 )
695695
696696 elif is_complex (val ):
697- dtype = np .complex_
697+ dtype = np .dtype ( np . complex_ )
698698
699699 elif pandas_dtype :
700700 if lib .is_period (val ):
@@ -707,7 +707,8 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
707707 return dtype , val
708708
709709
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 ]:
711712 """
712713 Infer the dtype from an array.
713714
@@ -738,7 +739,7 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):
738739 array(['1', '1'], dtype='<U21')
739740
740741 >>> infer_dtype_from_array([1, '1'])
741- (<class 'numpy.object_'> , [1, '1'])
742+ (dtype('O') , [1, '1'])
742743 """
743744 if isinstance (arr , np .ndarray ):
744745 return arr .dtype , arr
@@ -755,7 +756,7 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):
755756 # don't force numpy coerce with nan's
756757 inferred = lib .infer_dtype (arr , skipna = False )
757758 if inferred in ["string" , "bytes" , "mixed" , "mixed-integer" ]:
758- return (np .object_ , arr )
759+ return (np .dtype ( np . object_ ) , arr )
759760
760761 arr = np .asarray (arr )
761762 return arr .dtype , arr
@@ -1469,7 +1470,7 @@ def find_common_type(types):
14691470 return np .find_common_type (types , [])
14701471
14711472
1472- def cast_scalar_to_array (shape , value , dtype = None ):
1473+ def cast_scalar_to_array (shape , value , dtype : Optional [ DtypeObj ] = None ) -> np . ndarray :
14731474 """
14741475 Create np.ndarray of specified shape and dtype, filled with values.
14751476
@@ -1496,7 +1497,9 @@ def cast_scalar_to_array(shape, value, dtype=None):
14961497 return values
14971498
14981499
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 :
15001503 """
15011504 create a np.ndarray / pandas type of specified shape and dtype
15021505 filled with values
@@ -1505,7 +1508,7 @@ def construct_1d_arraylike_from_scalar(value, length: int, dtype):
15051508 ----------
15061509 value : scalar value
15071510 length : int
1508- dtype : pandas_dtype / np.dtype
1511+ dtype : pandas_dtype or np.dtype
15091512
15101513 Returns
15111514 -------
@@ -1517,8 +1520,6 @@ def construct_1d_arraylike_from_scalar(value, length: int, dtype):
15171520 subarr = cls ._from_sequence ([value ] * length , dtype = dtype )
15181521
15191522 else :
1520- if not isinstance (dtype , (np .dtype , type (np .dtype ))):
1521- dtype = dtype .dtype
15221523
15231524 if length and is_integer_dtype (dtype ) and isna (value ):
15241525 # coerce if we have nan for an integer dtype
@@ -1536,7 +1537,7 @@ def construct_1d_arraylike_from_scalar(value, length: int, dtype):
15361537 return subarr
15371538
15381539
1539- def construct_1d_object_array_from_listlike (values ):
1540+ def construct_1d_object_array_from_listlike (values ) -> np . ndarray :
15401541 """
15411542 Transform any list-like object in a 1-dimensional numpy array of object
15421543 dtype.
@@ -1561,7 +1562,9 @@ def construct_1d_object_array_from_listlike(values):
15611562 return result
15621563
15631564
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 :
15651568 """
15661569 Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
15671570
0 commit comments