3
3
"""
4
4
5
5
from datetime import date , datetime , timedelta
6
- from typing import TYPE_CHECKING , Any , List , Optional , Tuple , Type
6
+ from typing import TYPE_CHECKING , Any , List , Optional , Sequence , Set , Tuple , Type , Union
7
7
8
8
import numpy as np
9
9
18
18
ints_to_pydatetime ,
19
19
)
20
20
from pandas ._libs .tslibs .timezones import tz_compare
21
- from pandas ._typing import ArrayLike , Dtype , DtypeObj
21
+ from pandas ._typing import AnyArrayLike , ArrayLike , Dtype , DtypeObj , Scalar
22
22
from pandas .util ._validators import validate_bool_kwarg
23
23
24
24
from pandas .core .dtypes .common import (
@@ -118,7 +118,7 @@ def is_nested_object(obj) -> bool:
118
118
return False
119
119
120
120
121
- def maybe_downcast_to_dtype (result , dtype ):
121
+ def maybe_downcast_to_dtype (result , dtype : Dtype ):
122
122
"""
123
123
try to cast to the specified dtype (e.g. convert back to bool/int
124
124
or could be an astype of float64->float32
@@ -186,7 +186,7 @@ def maybe_downcast_to_dtype(result, dtype):
186
186
return result
187
187
188
188
189
- def maybe_downcast_numeric (result , dtype , do_round : bool = False ):
189
+ def maybe_downcast_numeric (result , dtype : Dtype , do_round : bool = False ):
190
190
"""
191
191
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
192
192
@@ -329,7 +329,9 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
329
329
return dtype
330
330
331
331
332
- def maybe_cast_to_extension_array (cls : Type ["ExtensionArray" ], obj , dtype = None ):
332
+ def maybe_cast_to_extension_array (
333
+ cls : Type ["ExtensionArray" ], obj , dtype : Dtype = None
334
+ ):
333
335
"""
334
336
Call to `_from_sequence` that returns the object unchanged on Exception.
335
337
@@ -362,7 +364,9 @@ def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
362
364
return result
363
365
364
366
365
- def maybe_upcast_putmask (result : np .ndarray , mask : np .ndarray , other ):
367
+ def maybe_upcast_putmask (
368
+ result : np .ndarray , mask : np .ndarray , other : Scalar
369
+ ) -> Tuple [np .ndarray , bool ]:
366
370
"""
367
371
A safe version of putmask that potentially upcasts the result.
368
372
@@ -660,7 +664,7 @@ def maybe_promote(dtype, fill_value=np.nan):
660
664
return dtype , fill_value
661
665
662
666
663
- def _ensure_dtype_type (value , dtype ):
667
+ def _ensure_dtype_type (value , dtype : DtypeObj ):
664
668
"""
665
669
Ensure that the given value is an instance of the given dtype.
666
670
@@ -787,7 +791,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
787
791
788
792
789
793
# TODO: try to make the Any in the return annotation more specific
790
- def infer_dtype_from_array (arr , pandas_dtype : bool = False ) -> Tuple [DtypeObj , Any ]:
794
+ def infer_dtype_from_array (
795
+ arr , pandas_dtype : bool = False
796
+ ) -> Tuple [DtypeObj , AnyArrayLike ]:
791
797
"""
792
798
Infer the dtype from an array.
793
799
@@ -875,7 +881,12 @@ def maybe_infer_dtype_type(element):
875
881
return tipo
876
882
877
883
878
- def maybe_upcast (values , fill_value = np .nan , dtype = None , copy : bool = False ):
884
+ def maybe_upcast (
885
+ values : ArrayLike ,
886
+ fill_value : Scalar = np .nan ,
887
+ dtype : Dtype = None ,
888
+ copy : bool = False ,
889
+ ) -> Tuple [ArrayLike , Scalar ]:
879
890
"""
880
891
Provide explicit type promotion and coercion.
881
892
@@ -887,6 +898,13 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
887
898
dtype : if None, then use the dtype of the values, else coerce to this type
888
899
copy : bool, default True
889
900
If True always make a copy even if no upcast is required.
901
+
902
+ Returns
903
+ -------
904
+ values: ndarray or ExtensionArray
905
+ the original array, possibly upcast
906
+ fill_value:
907
+ the fill value, possibly upcast
890
908
"""
891
909
if not is_scalar (fill_value ) and not is_object_dtype (values .dtype ):
892
910
# We allow arbitrary fill values for object dtype
@@ -907,7 +925,7 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
907
925
return values , fill_value
908
926
909
927
910
- def invalidate_string_dtypes (dtype_set ):
928
+ def invalidate_string_dtypes (dtype_set : Set ):
911
929
"""
912
930
Change string like dtypes to object for
913
931
``DataFrame.select_dtypes()``.
@@ -929,7 +947,7 @@ def coerce_indexer_dtype(indexer, categories):
929
947
return ensure_int64 (indexer )
930
948
931
949
932
- def coerce_to_dtypes (result , dtypes ) :
950
+ def coerce_to_dtypes (result : Sequence [ Scalar ] , dtypes : Sequence [ Dtype ]) -> List [ Scalar ] :
933
951
"""
934
952
given a dtypes and a result set, coerce the result elements to the
935
953
dtypes
@@ -959,7 +977,9 @@ def conv(r, dtype):
959
977
return [conv (r , dtype ) for r , dtype in zip (result , dtypes )]
960
978
961
979
962
- def astype_nansafe (arr , dtype , copy : bool = True , skipna : bool = False ):
980
+ def astype_nansafe (
981
+ arr , dtype : DtypeObj , copy : bool = True , skipna : bool = False
982
+ ) -> ArrayLike :
963
983
"""
964
984
Cast the elements of an array to a given dtype a nan-safe manner.
965
985
@@ -1063,7 +1083,9 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
1063
1083
return arr .view (dtype )
1064
1084
1065
1085
1066
- def maybe_convert_objects (values : np .ndarray , convert_numeric : bool = True ):
1086
+ def maybe_convert_objects (
1087
+ values : np .ndarray , convert_numeric : bool = True
1088
+ ) -> Union [np .ndarray , ABCDatetimeIndex ]:
1067
1089
"""
1068
1090
If we have an object dtype array, try to coerce dates and/or numbers.
1069
1091
@@ -1184,7 +1206,7 @@ def soft_convert_objects(
1184
1206
1185
1207
1186
1208
def convert_dtypes (
1187
- input_array ,
1209
+ input_array : AnyArrayLike ,
1188
1210
convert_string : bool = True ,
1189
1211
convert_integer : bool = True ,
1190
1212
convert_boolean : bool = True ,
@@ -1250,7 +1272,7 @@ def convert_dtypes(
1250
1272
return inferred_dtype
1251
1273
1252
1274
1253
- def maybe_castable (arr ) -> bool :
1275
+ def maybe_castable (arr : np . ndarray ) -> bool :
1254
1276
# return False to force a non-fastpath
1255
1277
1256
1278
# check datetime64[ns]/timedelta64[ns] are valid
0 commit comments