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
@@ -657,7 +661,7 @@ def maybe_promote(dtype, fill_value=np.nan):
657
661
return dtype , fill_value
658
662
659
663
660
- def _ensure_dtype_type (value , dtype ):
664
+ def _ensure_dtype_type (value , dtype : DtypeObj ):
661
665
"""
662
666
Ensure that the given value is an instance of the given dtype.
663
667
@@ -784,7 +788,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
784
788
785
789
786
790
# TODO: try to make the Any in the return annotation more specific
787
- def infer_dtype_from_array (arr , pandas_dtype : bool = False ) -> Tuple [DtypeObj , Any ]:
791
+ def infer_dtype_from_array (
792
+ arr , pandas_dtype : bool = False
793
+ ) -> Tuple [DtypeObj , AnyArrayLike ]:
788
794
"""
789
795
Infer the dtype from an array.
790
796
@@ -872,7 +878,12 @@ def maybe_infer_dtype_type(element):
872
878
return tipo
873
879
874
880
875
- def maybe_upcast (values , fill_value = np .nan , dtype = None , copy : bool = False ):
881
+ def maybe_upcast (
882
+ values : ArrayLike ,
883
+ fill_value : Scalar = np .nan ,
884
+ dtype : Dtype = None ,
885
+ copy : bool = False ,
886
+ ) -> Tuple [ArrayLike , Scalar ]:
876
887
"""
877
888
Provide explicit type promotion and coercion.
878
889
@@ -884,6 +895,13 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
884
895
dtype : if None, then use the dtype of the values, else coerce to this type
885
896
copy : bool, default True
886
897
If True always make a copy even if no upcast is required.
898
+
899
+ Returns
900
+ -------
901
+ values: ndarray or ExtensionArray
902
+ the original array, possibly upcast
903
+ fill_value:
904
+ the fill value, possibly upcast
887
905
"""
888
906
if not is_scalar (fill_value ) and not is_object_dtype (values .dtype ):
889
907
# We allow arbitrary fill values for object dtype
@@ -904,7 +922,7 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
904
922
return values , fill_value
905
923
906
924
907
- def invalidate_string_dtypes (dtype_set ):
925
+ def invalidate_string_dtypes (dtype_set : Set ):
908
926
"""
909
927
Change string like dtypes to object for
910
928
``DataFrame.select_dtypes()``.
@@ -926,7 +944,7 @@ def coerce_indexer_dtype(indexer, categories):
926
944
return ensure_int64 (indexer )
927
945
928
946
929
- def coerce_to_dtypes (result , dtypes ) :
947
+ def coerce_to_dtypes (result : Sequence [ Scalar ] , dtypes : Sequence [ Dtype ]) -> List [ Scalar ] :
930
948
"""
931
949
given a dtypes and a result set, coerce the result elements to the
932
950
dtypes
@@ -956,7 +974,9 @@ def conv(r, dtype):
956
974
return [conv (r , dtype ) for r , dtype in zip (result , dtypes )]
957
975
958
976
959
- def astype_nansafe (arr , dtype , copy : bool = True , skipna : bool = False ):
977
+ def astype_nansafe (
978
+ arr , dtype : DtypeObj , copy : bool = True , skipna : bool = False
979
+ ) -> ArrayLike :
960
980
"""
961
981
Cast the elements of an array to a given dtype a nan-safe manner.
962
982
@@ -1060,7 +1080,9 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
1060
1080
return arr .view (dtype )
1061
1081
1062
1082
1063
- def maybe_convert_objects (values : np .ndarray , convert_numeric : bool = True ):
1083
+ def maybe_convert_objects (
1084
+ values : np .ndarray , convert_numeric : bool = True
1085
+ ) -> Union [np .ndarray , ABCDatetimeIndex ]:
1064
1086
"""
1065
1087
If we have an object dtype array, try to coerce dates and/or numbers.
1066
1088
@@ -1181,7 +1203,7 @@ def soft_convert_objects(
1181
1203
1182
1204
1183
1205
def convert_dtypes (
1184
- input_array ,
1206
+ input_array : AnyArrayLike ,
1185
1207
convert_string : bool = True ,
1186
1208
convert_integer : bool = True ,
1187
1209
convert_boolean : bool = True ,
@@ -1247,7 +1269,7 @@ def convert_dtypes(
1247
1269
return inferred_dtype
1248
1270
1249
1271
1250
- def maybe_castable (arr ) -> bool :
1272
+ def maybe_castable (arr : np . ndarray ) -> bool :
1251
1273
# return False to force a non-fastpath
1252
1274
1253
1275
# check datetime64[ns]/timedelta64[ns] are valid
0 commit comments