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