15
15
from typing import (
16
16
TYPE_CHECKING ,
17
17
Any ,
18
- Dict ,
19
- List ,
20
- Optional ,
21
18
Sequence ,
22
- Set ,
23
19
Sized ,
24
- Tuple ,
25
- Type ,
26
- Union ,
27
20
cast ,
28
21
overload ,
29
22
)
125
118
126
119
127
120
def maybe_convert_platform (
128
- values : Union [ list , tuple , range , np .ndarray , ExtensionArray ]
121
+ values : list | tuple | range | np .ndarray | ExtensionArray ,
129
122
) -> ArrayLike :
130
123
""" try to do platform conversion, allow ndarray or list here """
131
124
if isinstance (values , (list , tuple , range )):
@@ -159,7 +152,7 @@ def is_nested_object(obj) -> bool:
159
152
)
160
153
161
154
162
- def maybe_box_datetimelike (value : Scalar , dtype : Optional [ Dtype ] = None ) -> Scalar :
155
+ def maybe_box_datetimelike (value : Scalar , dtype : Dtype | None = None ) -> Scalar :
163
156
"""
164
157
Cast scalar to Timestamp or Timedelta if scalar is datetime-like
165
158
and dtype is not object.
@@ -245,9 +238,7 @@ def _disallow_mismatched_datetimelike(value, dtype: DtypeObj):
245
238
raise TypeError (f"Cannot cast { repr (value )} to { dtype } " )
246
239
247
240
248
- def maybe_downcast_to_dtype (
249
- result : ArrayLike , dtype : Union [str , np .dtype ]
250
- ) -> ArrayLike :
241
+ def maybe_downcast_to_dtype (result : ArrayLike , dtype : str | np .dtype ) -> ArrayLike :
251
242
"""
252
243
try to cast to the specified dtype (e.g. convert back to bool/int
253
244
or could be an astype of float64->float32
@@ -460,7 +451,7 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
460
451
461
452
462
453
def maybe_cast_to_extension_array (
463
- cls : Type [ExtensionArray ], obj : ArrayLike , dtype : Optional [ ExtensionDtype ] = None
454
+ cls : type [ExtensionArray ], obj : ArrayLike , dtype : ExtensionDtype | None = None
464
455
) -> ArrayLike :
465
456
"""
466
457
Call to `_from_sequence` that returns the object unchanged on Exception.
@@ -727,7 +718,7 @@ def _ensure_dtype_type(value, dtype: np.dtype):
727
718
return dtype .type (value )
728
719
729
720
730
- def infer_dtype_from (val , pandas_dtype : bool = False ) -> Tuple [DtypeObj , Any ]:
721
+ def infer_dtype_from (val , pandas_dtype : bool = False ) -> tuple [DtypeObj , Any ]:
731
722
"""
732
723
Interpret the dtype from a scalar or array.
733
724
@@ -744,7 +735,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
744
735
return infer_dtype_from_array (val , pandas_dtype = pandas_dtype )
745
736
746
737
747
- def infer_dtype_from_scalar (val , pandas_dtype : bool = False ) -> Tuple [DtypeObj , Any ]:
738
+ def infer_dtype_from_scalar (val , pandas_dtype : bool = False ) -> tuple [DtypeObj , Any ]:
748
739
"""
749
740
Interpret the dtype from a scalar.
750
741
@@ -834,7 +825,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
834
825
return dtype , val
835
826
836
827
837
- def dict_compat (d : Dict [Scalar , Scalar ]) -> Dict [Scalar , Scalar ]:
828
+ def dict_compat (d : dict [Scalar , Scalar ]) -> dict [Scalar , Scalar ]:
838
829
"""
839
830
Convert datetimelike-keyed dicts to a Timestamp-keyed dict.
840
831
@@ -852,7 +843,7 @@ def dict_compat(d: Dict[Scalar, Scalar]) -> Dict[Scalar, Scalar]:
852
843
853
844
def infer_dtype_from_array (
854
845
arr , pandas_dtype : bool = False
855
- ) -> Tuple [DtypeObj , ArrayLike ]:
846
+ ) -> tuple [DtypeObj , ArrayLike ]:
856
847
"""
857
848
Infer the dtype from an array.
858
849
@@ -944,7 +935,7 @@ def maybe_upcast(
944
935
values : np .ndarray ,
945
936
fill_value : Scalar = np .nan ,
946
937
copy : bool = False ,
947
- ) -> Tuple [np .ndarray , Scalar ]:
938
+ ) -> tuple [np .ndarray , Scalar ]:
948
939
"""
949
940
Provide explicit type promotion and coercion.
950
941
@@ -970,7 +961,7 @@ def maybe_upcast(
970
961
return values , fill_value
971
962
972
963
973
- def invalidate_string_dtypes (dtype_set : Set [DtypeObj ]):
964
+ def invalidate_string_dtypes (dtype_set : set [DtypeObj ]):
974
965
"""
975
966
Change string like dtypes to object for
976
967
``DataFrame.select_dtypes()``.
@@ -1524,7 +1515,7 @@ def maybe_castable(dtype: np.dtype) -> bool:
1524
1515
return dtype .name not in POSSIBLY_CAST_DTYPES
1525
1516
1526
1517
1527
- def maybe_infer_to_datetimelike (value : Union [ np .ndarray , List ] ):
1518
+ def maybe_infer_to_datetimelike (value : np .ndarray | list ):
1528
1519
"""
1529
1520
we might have a array (or single object) that is datetime like,
1530
1521
and no dtype is passed don't change the value unless we find a
@@ -1619,8 +1610,8 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
1619
1610
1620
1611
1621
1612
def maybe_cast_to_datetime (
1622
- value : Union [ ExtensionArray , np .ndarray , list ] , dtype : Optional [ DtypeObj ]
1623
- ) -> Union [ ExtensionArray , np .ndarray , list ] :
1613
+ value : ExtensionArray | np .ndarray | list , dtype : DtypeObj | None
1614
+ ) -> ExtensionArray | np .ndarray | list :
1624
1615
"""
1625
1616
try to cast the array/value to a datetimelike dtype, converting float
1626
1617
nan to iNaT
@@ -1784,7 +1775,7 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
1784
1775
return dtype
1785
1776
1786
1777
1787
- def find_common_type (types : List [DtypeObj ]) -> DtypeObj :
1778
+ def find_common_type (types : list [DtypeObj ]) -> DtypeObj :
1788
1779
"""
1789
1780
Find a common data type among the given dtypes.
1790
1781
@@ -1873,7 +1864,7 @@ def construct_2d_arraylike_from_scalar(
1873
1864
1874
1865
1875
1866
def construct_1d_arraylike_from_scalar (
1876
- value : Scalar , length : int , dtype : Optional [ DtypeObj ]
1867
+ value : Scalar , length : int , dtype : DtypeObj | None
1877
1868
) -> ArrayLike :
1878
1869
"""
1879
1870
create a np.ndarray / pandas type of specified shape and dtype
@@ -1947,7 +1938,7 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
1947
1938
1948
1939
1949
1940
def construct_1d_ndarray_preserving_na (
1950
- values : Sequence , dtype : Optional [ DtypeObj ] = None , copy : bool = False
1941
+ values : Sequence , dtype : DtypeObj | None = None , copy : bool = False
1951
1942
) -> np .ndarray :
1952
1943
"""
1953
1944
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
@@ -1997,7 +1988,7 @@ def construct_1d_ndarray_preserving_na(
1997
1988
1998
1989
1999
1990
def maybe_cast_to_integer_array (
2000
- arr : Union [ list , np .ndarray ] , dtype : np .dtype , copy : bool = False
1991
+ arr : list | np .ndarray , dtype : np .dtype , copy : bool = False
2001
1992
):
2002
1993
"""
2003
1994
Takes any dtype and returns the casted version, raising for when data is
0 commit comments