10
10
Hashable ,
11
11
List ,
12
12
Tuple ,
13
+ TypedDict ,
13
14
Union ,
14
15
cast ,
15
16
overload ,
79
80
if TYPE_CHECKING :
80
81
from pandas ._libs .tslibs .nattype import NaTType
81
82
82
- from pandas import Series
83
+ from pandas import (
84
+ DataFrame ,
85
+ Series ,
86
+ )
83
87
84
88
# ---------------------------------------------------------------------
85
89
# types used in annotations
86
90
87
- ArrayConvertible = Union [List , Tuple , AnyArrayLike , "Series" ]
91
+ ArrayConvertible = Union [List , Tuple , AnyArrayLike ]
88
92
Scalar = Union [int , float , str ]
89
93
DatetimeScalar = Union [Scalar , datetime ]
90
94
91
95
DatetimeScalarOrArrayConvertible = Union [DatetimeScalar , ArrayConvertible ]
96
+
97
+ DatetimeDictArg = Union [List [Scalar ], Tuple [Scalar , ...], AnyArrayLike ]
98
+
99
+
100
+ class YearMonthDayDict (TypedDict , total = True ):
101
+ year : DatetimeDictArg
102
+ month : DatetimeDictArg
103
+ day : DatetimeDictArg
104
+
105
+
106
+ class FulldatetimeDict (YearMonthDayDict , total = False ):
107
+ hour : DatetimeDictArg
108
+ hours : DatetimeDictArg
109
+ minute : DatetimeDictArg
110
+ minutes : DatetimeDictArg
111
+ second : DatetimeDictArg
112
+ seconds : DatetimeDictArg
113
+ ms : DatetimeDictArg
114
+ us : DatetimeDictArg
115
+ ns : DatetimeDictArg
116
+
117
+
118
+ DictConvertible = Union [FulldatetimeDict , "DataFrame" ]
92
119
start_caching_at = 50
93
120
94
121
@@ -640,13 +667,13 @@ def to_datetime(
640
667
infer_datetime_format : bool = ...,
641
668
origin = ...,
642
669
cache : bool = ...,
643
- ) -> Timestamp | NaTType :
670
+ ) -> Timestamp :
644
671
...
645
672
646
673
647
674
@overload
648
675
def to_datetime (
649
- arg : Series ,
676
+ arg : Series | DictConvertible ,
650
677
errors : str = ...,
651
678
dayfirst : bool = ...,
652
679
yearfirst : bool = ...,
@@ -663,7 +690,7 @@ def to_datetime(
663
690
664
691
@overload
665
692
def to_datetime (
666
- arg : list | tuple | np . ndarray ,
693
+ arg : list | tuple | Index | ArrayLike ,
667
694
errors : str = ...,
668
695
dayfirst : bool = ...,
669
696
yearfirst : bool = ...,
@@ -679,7 +706,7 @@ def to_datetime(
679
706
680
707
681
708
def to_datetime (
682
- arg : DatetimeScalarOrArrayConvertible ,
709
+ arg : DatetimeScalarOrArrayConvertible | DictConvertible ,
683
710
errors : str = "raise" ,
684
711
dayfirst : bool = False ,
685
712
yearfirst : bool = False ,
@@ -1067,10 +1094,10 @@ def to_datetime(
1067
1094
# "Union[float, str, datetime, List[Any], Tuple[Any, ...], ExtensionArray,
1068
1095
# ndarray[Any, Any], Series]"; expected "Union[List[Any], Tuple[Any, ...],
1069
1096
# Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series], Series]"
1070
- arg = cast (
1097
+ argc = cast (
1071
1098
Union [list , tuple , ExtensionArray , np .ndarray , "Series" , Index ], arg
1072
1099
)
1073
- cache_array = _maybe_cache (arg , format , cache , convert_listlike )
1100
+ cache_array = _maybe_cache (argc , format , cache , convert_listlike )
1074
1101
except OutOfBoundsDatetime :
1075
1102
# caching attempts to create a DatetimeIndex, which may raise
1076
1103
# an OOB. If that's the desired behavior, then just reraise...
@@ -1081,9 +1108,9 @@ def to_datetime(
1081
1108
1082
1109
cache_array = Series ([], dtype = object ) # just an empty array
1083
1110
if not cache_array .empty :
1084
- result = _convert_and_box_cache (arg , cache_array )
1111
+ result = _convert_and_box_cache (argc , cache_array )
1085
1112
else :
1086
- result = convert_listlike (arg , format )
1113
+ result = convert_listlike (argc , format )
1087
1114
else :
1088
1115
result = convert_listlike (np .array ([arg ]), format )[0 ]
1089
1116
if isinstance (arg , bool ) and isinstance (result , np .bool_ ):
0 commit comments