@@ -162,6 +162,7 @@ def _maybe_cache(
162
162
format : str | None ,
163
163
cache : bool ,
164
164
convert_listlike : Callable ,
165
+ errors : str = "raise" ,
165
166
) -> Series :
166
167
"""
167
168
Create a cache of unique dates from an array of dates
@@ -175,6 +176,10 @@ def _maybe_cache(
175
176
True attempts to create a cache of converted values
176
177
convert_listlike : function
177
178
Conversion function to apply on dates
179
+ errors : {'ignore', 'raise', 'coerce'}, default 'raise'
180
+ - If 'raise', then invalid parsing will raise an exception.
181
+ - If 'coerce', then invalid parsing will be set as NaT.
182
+ - If 'ignore', then invalid parsing will return the input.
178
183
179
184
Returns
180
185
-------
@@ -193,7 +198,16 @@ def _maybe_cache(
193
198
unique_dates = unique (arg )
194
199
if len (unique_dates ) < len (arg ):
195
200
cache_dates = convert_listlike (unique_dates , format )
196
- cache_array = Series (cache_dates , index = unique_dates )
201
+ try :
202
+ cache_array = Series (cache_dates , index = unique_dates )
203
+ except OutOfBoundsDatetime :
204
+ # caching attempts to create a DatetimeIndex, which may raise
205
+ # an OOB. If that's the desired behavior, then just reraise...
206
+ if errors == "raise" :
207
+ raise
208
+ # ... otherwise, continue without the cache.
209
+ return cache_array
210
+
197
211
# GH#39882 and GH#35888 in case of None and NaT we get duplicates
198
212
if not cache_array .index .is_unique :
199
213
cache_array = cache_array [~ cache_array .index .duplicated ()]
@@ -891,32 +905,25 @@ def to_datetime(
891
905
# error: Too many arguments for "tz_localize" of "NaTType"
892
906
result = result .tz_localize (tz ) # type: ignore[call-arg]
893
907
elif isinstance (arg , ABCSeries ):
894
- cache_array = _maybe_cache (arg , format , cache , convert_listlike )
908
+ cache_array = _maybe_cache (arg , format , cache , convert_listlike , errors )
909
+
895
910
if not cache_array .empty :
896
911
result = arg .map (cache_array )
897
912
else :
898
- values = convert_listlike (arg ._values , format )
913
+ values = convert_listlike (arg ._values , format , errors )
899
914
result = arg ._constructor (values , index = arg .index , name = arg .name )
900
915
elif isinstance (arg , (ABCDataFrame , abc .MutableMapping )):
901
916
result = _assemble_from_unit_mappings (arg , errors , tz )
902
917
elif isinstance (arg , Index ):
903
- cache_array = _maybe_cache (arg , format , cache , convert_listlike )
918
+ cache_array = _maybe_cache (arg , format , cache , convert_listlike , errors )
919
+
904
920
if not cache_array .empty :
905
921
result = _convert_and_box_cache (arg , cache_array , name = arg .name )
906
922
else :
907
923
result = convert_listlike (arg , format , name = arg .name )
908
924
elif is_list_like (arg ):
909
- try :
910
- cache_array = _maybe_cache (arg , format , cache , convert_listlike )
911
- except OutOfBoundsDatetime :
912
- # caching attempts to create a DatetimeIndex, which may raise
913
- # an OOB. If that's the desired behavior, then just reraise...
914
- if errors == "raise" :
915
- raise
916
- # ... otherwise, continue without the cache.
917
- from pandas import Series
925
+ cache_array = _maybe_cache (arg , format , cache , convert_listlike , errors )
918
926
919
- cache_array = Series ([], dtype = object ) # just an empty array
920
927
if not cache_array .empty :
921
928
result = _convert_and_box_cache (arg , cache_array )
922
929
else :
0 commit comments