@@ -603,7 +603,9 @@ def to_datetime(
603603 cache : bool, default True
604604 If True, use a cache of unique, converted dates to apply the datetime
605605 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.
607609
608610 .. versionadded:: 0.23.0
609611
@@ -735,7 +737,17 @@ def to_datetime(
735737 convert_listlike = partial (convert_listlike , name = arg .name )
736738 result = convert_listlike (arg , format )
737739 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
739751 if not cache_array .empty :
740752 result = _convert_and_box_cache (arg , cache_array )
741753 else :
0 commit comments