@@ -112,7 +112,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
112
112
origin.
113
113
114
114
.. versionadded: 0.20.0
115
- cache : boolean, default False
115
+ cache : boolean, default True
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 dates.
118
118
@@ -310,16 +310,32 @@ 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 , tz ):
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
- Returns:
321
- Series of converted datetime arg or
322
- None if the conversion failed
313
+ def _maybe_convert_cache (arg , cache , box , format , name = None , tz = tz ):
314
+ """
315
+ Try to convert the datetimelike arg using
316
+ a cache of converted dates.
317
+
318
+ Parameters
319
+ ----------
320
+ arg : integer, float, string, datetime, list, tuple, 1-d array, Series
321
+ Datetime argument to convert
322
+ cache : boolean
323
+ If True, try to convert the dates with a cache
324
+ If False, short circuit and return None
325
+ Flag whether to cache the converted dates
326
+ box : boolean
327
+ If True, return a DatetimeIndex
328
+ if False, return an ndarray of values
329
+ tz : String or None
330
+ 'utc' if UTC=True was passed else None
331
+ name : String, default None
332
+ DatetimeIndex name
333
+ Returns
334
+ -------
335
+ Series if original argument was a Series
336
+ DatetimeIndex if box=True and original argument was not a Series
337
+ ndarray if box=False and original argument was not a Series
338
+ None if the conversion failed
323
339
"""
324
340
if cache and is_list_like (arg ) and len (arg ) >= 1000 :
325
341
unique_dates = algorithms .unique (arg )
@@ -328,9 +344,13 @@ def _maybe_convert_cache(arg, cache, tz):
328
344
cache_dates = _convert_listlike (unique_dates , True , format ,
329
345
tz = tz )
330
346
convert_cache = Series (cache_dates , index = unique_dates )
331
- if not isinstance (arg , Series ):
332
- arg = Series (arg )
333
- return arg .map (convert_cache )
347
+ result = Series (arg , name = name ).map (convert_cache )
348
+ if isinstance (arg , Series ):
349
+ return result
350
+ elif box :
351
+ return DatetimeIndex (result , name = name )
352
+ else :
353
+ return result .values
334
354
return None
335
355
336
356
if arg is None :
@@ -397,29 +417,21 @@ def _maybe_convert_cache(arg, cache, tz):
397
417
if isinstance (arg , tslib .Timestamp ):
398
418
result = arg
399
419
elif isinstance (arg , ABCSeries ):
400
- result = _maybe_convert_cache (arg , cache , tz )
420
+ result = _maybe_convert_cache (arg , cache , box , format , name = arg . name )
401
421
if result is None :
402
422
from pandas import Series
403
423
values = _convert_listlike (arg ._values , True , format )
404
424
result = Series (values , index = arg .index , name = arg .name )
405
425
elif isinstance (arg , (ABCDataFrame , MutableMapping )):
406
426
result = _assemble_from_unit_mappings (arg , errors = errors )
407
427
elif isinstance (arg , ABCIndexClass ):
408
- result = _maybe_convert_cache (arg , cache , tz )
428
+ result = _maybe_convert_cache (arg , cache , box , format , name = arg . name )
409
429
if result is None :
410
430
result = _convert_listlike (arg , box , format , name = arg .name )
411
- else :
412
- result = result .values
413
- if box :
414
- result = DatetimeIndex (result , tz = tz , name = arg .name )
415
431
elif is_list_like (arg ):
416
- result = _maybe_convert_cache (arg , cache , tz )
432
+ result = _maybe_convert_cache (arg , cache , box , format )
417
433
if result is None :
418
434
result = _convert_listlike (arg , box , format )
419
- else :
420
- result = result .values
421
- if box :
422
- result = DatetimeIndex (result , tz = tz )
423
435
else :
424
436
result = _convert_listlike (np .array ([arg ]), box , format )[0 ]
425
437
0 commit comments