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 (
@@ -113,7 +113,7 @@ def is_nested_object(obj) -> bool:
113
113
return False
114
114
115
115
116
- def maybe_downcast_to_dtype (result , dtype ):
116
+ def maybe_downcast_to_dtype (result , dtype : Dtype ):
117
117
"""
118
118
try to cast to the specified dtype (e.g. convert back to bool/int
119
119
or could be an astype of float64->float32
@@ -181,7 +181,7 @@ def maybe_downcast_to_dtype(result, dtype):
181
181
return result
182
182
183
183
184
- def maybe_downcast_numeric (result , dtype , do_round : bool = False ):
184
+ def maybe_downcast_numeric (result , dtype : Dtype , do_round : bool = False ):
185
185
"""
186
186
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
187
187
@@ -324,7 +324,9 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
324
324
return dtype
325
325
326
326
327
- def maybe_cast_to_extension_array (cls : Type ["ExtensionArray" ], obj , dtype = None ):
327
+ def maybe_cast_to_extension_array (
328
+ cls : Type ["ExtensionArray" ], obj , dtype : Dtype = None
329
+ ):
328
330
"""
329
331
Call to `_from_sequence` that returns the object unchanged on Exception.
330
332
@@ -357,7 +359,9 @@ def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
357
359
return result
358
360
359
361
360
- def maybe_upcast_putmask (result : np .ndarray , mask : np .ndarray , other ):
362
+ def maybe_upcast_putmask (
363
+ result : np .ndarray , mask : np .ndarray , other : Scalar
364
+ ) -> Tuple [np .ndarray , bool ]:
361
365
"""
362
366
A safe version of putmask that potentially upcasts the result.
363
367
@@ -439,7 +443,7 @@ def changeit():
439
443
return result , False
440
444
441
445
442
- def maybe_promote (dtype , fill_value = np .nan ):
446
+ def maybe_promote (dtype , fill_value : Scalar = np .nan ) -> Tuple [ DtypeObj , Scalar ] :
443
447
"""
444
448
Find the minimal dtype that can hold both the given dtype and fill_value.
445
449
@@ -595,7 +599,7 @@ def maybe_promote(dtype, fill_value=np.nan):
595
599
return dtype , fill_value
596
600
597
601
598
- def _ensure_dtype_type (value , dtype ):
602
+ def _ensure_dtype_type (value , dtype : DtypeObj ):
599
603
"""
600
604
Ensure that the given value is an instance of the given dtype.
601
605
@@ -722,7 +726,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
722
726
723
727
724
728
# TODO: try to make the Any in the return annotation more specific
725
- def infer_dtype_from_array (arr , pandas_dtype : bool = False ) -> Tuple [DtypeObj , Any ]:
729
+ def infer_dtype_from_array (
730
+ arr , pandas_dtype : bool = False
731
+ ) -> Tuple [DtypeObj , AnyArrayLike ]:
726
732
"""
727
733
Infer the dtype from an array.
728
734
@@ -810,7 +816,12 @@ def maybe_infer_dtype_type(element):
810
816
return tipo
811
817
812
818
813
- def maybe_upcast (values , fill_value = np .nan , dtype = None , copy : bool = False ):
819
+ def maybe_upcast (
820
+ values : ArrayLike ,
821
+ fill_value : Scalar = np .nan ,
822
+ dtype : Dtype = None ,
823
+ copy : bool = False ,
824
+ ) -> Tuple [ArrayLike , Scalar ]:
814
825
"""
815
826
Provide explicit type promotion and coercion.
816
827
@@ -822,6 +833,13 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
822
833
dtype : if None, then use the dtype of the values, else coerce to this type
823
834
copy : bool, default True
824
835
If True always make a copy even if no upcast is required.
836
+
837
+ Returns
838
+ -------
839
+ values: ndarray or ExtensionArray
840
+ the original array, possibly upcast
841
+ fill_value:
842
+ the fill value, possibly upcast
825
843
"""
826
844
if not is_scalar (fill_value ) and not is_object_dtype (values .dtype ):
827
845
# We allow arbitrary fill values for object dtype
@@ -842,7 +860,7 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
842
860
return values , fill_value
843
861
844
862
845
- def invalidate_string_dtypes (dtype_set ):
863
+ def invalidate_string_dtypes (dtype_set : Set ):
846
864
"""
847
865
Change string like dtypes to object for
848
866
``DataFrame.select_dtypes()``.
@@ -864,7 +882,7 @@ def coerce_indexer_dtype(indexer, categories):
864
882
return ensure_int64 (indexer )
865
883
866
884
867
- def coerce_to_dtypes (result , dtypes ) :
885
+ def coerce_to_dtypes (result : Sequence [ Scalar ] , dtypes : Sequence [ Dtype ]) -> List [ Scalar ] :
868
886
"""
869
887
given a dtypes and a result set, coerce the result elements to the
870
888
dtypes
@@ -894,7 +912,9 @@ def conv(r, dtype):
894
912
return [conv (r , dtype ) for r , dtype in zip (result , dtypes )]
895
913
896
914
897
- def astype_nansafe (arr , dtype , copy : bool = True , skipna : bool = False ):
915
+ def astype_nansafe (
916
+ arr , dtype : DtypeObj , copy : bool = True , skipna : bool = False
917
+ ) -> ArrayLike :
898
918
"""
899
919
Cast the elements of an array to a given dtype a nan-safe manner.
900
920
@@ -996,7 +1016,9 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
996
1016
return arr .view (dtype )
997
1017
998
1018
999
- def maybe_convert_objects (values : np .ndarray , convert_numeric : bool = True ):
1019
+ def maybe_convert_objects (
1020
+ values : np .ndarray , convert_numeric : bool = True
1021
+ ) -> Union [np .ndarray , ABCDatetimeIndex ]:
1000
1022
"""
1001
1023
If we have an object dtype array, try to coerce dates and/or numbers.
1002
1024
@@ -1117,7 +1139,7 @@ def soft_convert_objects(
1117
1139
1118
1140
1119
1141
def convert_dtypes (
1120
- input_array ,
1142
+ input_array : AnyArrayLike ,
1121
1143
convert_string : bool = True ,
1122
1144
convert_integer : bool = True ,
1123
1145
convert_boolean : bool = True ,
@@ -1183,7 +1205,7 @@ def convert_dtypes(
1183
1205
return inferred_dtype
1184
1206
1185
1207
1186
- def maybe_castable (arr ) -> bool :
1208
+ def maybe_castable (arr : np . ndarray ) -> bool :
1187
1209
# return False to force a non-fastpath
1188
1210
1189
1211
# check datetime64[ns]/timedelta64[ns] are valid
0 commit comments