@@ -116,7 +116,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
116
116
If True, use a cache of unique, converted dates to apply the datetime
117
117
conversion. Produces signficant speed-ups when parsing duplicate date.
118
118
119
- .. versionadded: 0.20.2
119
+ .. versionadded: 0.21.0
120
120
Returns
121
121
-------
122
122
ret : datetime if parsing succeeded.
@@ -310,6 +310,28 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
310
310
except (ValueError , TypeError ):
311
311
raise e
312
312
313
+ def _maybe_convert_cache (arg , cache ):
314
+ """Try to convert the datetimelike arg using
315
+ a cache of converted dates.
316
+
317
+ arg: datetimelike arg from to_datetime
318
+ cache: bool whether to convert using a cache
319
+
320
+ Result:
321
+ Series of converted datetime arg or
322
+ None if the conversion failed
323
+ """
324
+ if cache and is_list_like (arg ) and len (arg ) >= 1000 :
325
+ unique_dates = algorithms .unique (arg )
326
+ if len (unique_dates ) != len (arg ):
327
+ from pandas import Series
328
+ cache_dates = _convert_listlike (unique_dates , False , format )
329
+ convert_cache = Series (cache_dates , index = unique_dates )
330
+ if not isinstance (arg , Series ):
331
+ arg = Series (arg )
332
+ return arg .map (convert_cache )
333
+ return None
334
+
313
335
if arg is None :
314
336
return None
315
337
@@ -371,41 +393,32 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
371
393
arg = np .asarray (arg )
372
394
arg = arg + offset
373
395
374
- convert_cache = None
375
- if cache and is_list_like (arg ) and len (arg ) >= 1000 :
376
- unique_dates = algorithms .unique (arg )
377
- if len (unique_dates ) != len (arg ):
378
- from pandas import Series
379
- cache_dates = _convert_listlike (unique_dates , False , format )
380
- convert_cache = Series (cache_dates , index = unique_dates )
381
-
382
396
if isinstance (arg , tslib .Timestamp ):
383
397
result = arg
384
398
elif isinstance (arg , ABCSeries ):
385
- if convert_cache is not None :
386
- result = arg .map (convert_cache )
387
- else :
399
+ result = _maybe_convert_cache (arg , cache )
400
+ if result is None :
388
401
from pandas import Series
389
402
values = _convert_listlike (arg ._values , True , format )
390
403
result = Series (values , index = arg .index , name = arg .name )
391
404
elif isinstance (arg , (ABCDataFrame , MutableMapping )):
392
405
result = _assemble_from_unit_mappings (arg , errors = errors )
393
406
elif isinstance (arg , ABCIndexClass ):
394
- if convert_cache is not None :
395
- from pandas import Series
396
- result = Series (arg ).map (convert_cache ).values
407
+ result = _maybe_convert_cache (arg , cache )
408
+ if result is None :
409
+ result = _convert_listlike (arg , box , format , name = arg .name )
410
+ else :
411
+ result = result .values
397
412
if box :
398
413
result = DatetimeIndex (result , tz = tz , name = arg .name )
399
- else :
400
- result = _convert_listlike (arg , box , format , name = arg .name )
401
414
elif is_list_like (arg ):
402
- if convert_cache is not None :
403
- from pandas import Series
404
- result = Series (arg ).map (convert_cache ).values
415
+ result = _maybe_convert_cache (arg , cache )
416
+ if result is None :
417
+ result = _convert_listlike (arg , box , format )
418
+ else :
419
+ result = result .values
405
420
if box :
406
421
result = DatetimeIndex (result , tz = tz )
407
- else :
408
- result = _convert_listlike (arg , box , format )
409
422
else :
410
423
result = _convert_listlike (np .array ([arg ]), box , format )[0 ]
411
424
0 commit comments