@@ -603,7 +603,9 @@ def to_datetime(
603
603
cache : bool, default True
604
604
If True, use a cache of unique, converted dates to apply the datetime
605
605
conversion. May produce significant speed-up when parsing duplicate
606
- date strings, especially ones with timezone offsets.
606
+ date strings, especially ones with timezone offsets. The cache is only
607
+ used when there are at least 50 values. The presence of out-of-bounds
608
+ values will render the cache unusable and may slow down parsing.
607
609
608
610
.. versionadded:: 0.23.0
609
611
@@ -735,7 +737,17 @@ def to_datetime(
735
737
convert_listlike = partial (convert_listlike , name = arg .name )
736
738
result = convert_listlike (arg , format )
737
739
elif is_list_like (arg ):
738
- cache_array = _maybe_cache (arg , format , cache , convert_listlike )
740
+ try :
741
+ cache_array = _maybe_cache (arg , format , cache , convert_listlike )
742
+ except tslibs .OutOfBoundsDatetime :
743
+ # caching attempts to create a DatetimeIndex, which may raise
744
+ # an OOB. If that's the desired behavior, then just reraise...
745
+ if errors == "raise" :
746
+ raise
747
+ # ... otherwise, continue without the cache.
748
+ from pandas import Series
749
+
750
+ cache_array = Series ([], dtype = object ) # just an empty array
739
751
if not cache_array .empty :
740
752
result = _convert_and_box_cache (arg , cache_array )
741
753
else :
0 commit comments