@@ -35,11 +35,35 @@ def _guess_datetime_format_for_array(arr, **kwargs):
35
35
if len (non_nan_elements ):
36
36
return _guess_datetime_format (arr [non_nan_elements [0 ]], ** kwargs )
37
37
38
+ def _maybe_cache (arg , format , cache , tz , _convert_listlike ):
39
+ """Create a cache of unique dates from an array of dates"""
40
+ from pandas import Series
41
+ cache_array = Series ()
42
+ if cache :
43
+ # Perform a quicker unique check
44
+ from pandas import Index
45
+ if not Index (arg ).is_unique :
46
+ unique_dates = algorithms .unique (arg )
47
+ cache_dates = _convert_listlike (unique_dates , True , format ,
48
+ tz = tz )
49
+ cache_array = Series (cache_dates , index = unique_dates )
50
+ return cache_array
51
+
52
+ def _convert_and_box_cache (arg , cache_array , box , name = None ):
53
+ """Convert array of dates with a cache and box the result"""
54
+ from pandas import Series
55
+ from pandas .core .indexes .datetimes import DatetimeIndex
56
+ result = Series (arg ).map (cache_array )
57
+ if box :
58
+ result = DatetimeIndex (result , name = name )
59
+ else :
60
+ result = result .values
61
+ return result
38
62
39
63
def to_datetime (arg , errors = 'raise' , dayfirst = False , yearfirst = False ,
40
64
utc = None , box = True , format = None , exact = True ,
41
65
unit = None , infer_datetime_format = False , origin = 'unix' ,
42
- cache = True ):
66
+ cache = False ):
43
67
"""
44
68
Convert argument to datetime.
45
69
@@ -310,51 +334,6 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
310
334
except (ValueError , TypeError ):
311
335
raise e
312
336
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
339
- """
340
- if cache and is_list_like (arg ) and len (arg ) >= 1000 :
341
- # Perform a quicker unique check
342
- from pandas import Index
343
- if not Index (arg ).is_unique :
344
- unique_dates = algorithms .unique (arg )
345
- from pandas import Series
346
- cache_dates = _convert_listlike (unique_dates , True , format ,
347
- tz = tz )
348
- convert_cache = Series (cache_dates , index = unique_dates )
349
- result = Series (arg , name = name ).map (convert_cache )
350
- if isinstance (arg , Series ):
351
- return result
352
- elif box :
353
- return DatetimeIndex (result , name = name )
354
- else :
355
- return result .values
356
- return None
357
-
358
337
if arg is None :
359
338
return None
360
339
@@ -419,20 +398,27 @@ def _maybe_convert_cache(arg, cache, box, format, name=None, tz=tz):
419
398
if isinstance (arg , tslib .Timestamp ):
420
399
result = arg
421
400
elif isinstance (arg , ABCSeries ):
422
- result = _maybe_convert_cache (arg , cache , box , format , name = arg .name )
423
- if result is None :
401
+ cache_array = _maybe_cache (arg , format , cache , tz , _convert_listlike )
402
+ if not cache_array .empty :
403
+ result = arg .map (cache_array )
404
+ else :
424
405
from pandas import Series
425
406
values = _convert_listlike (arg ._values , True , format )
426
407
result = Series (values , index = arg .index , name = arg .name )
427
408
elif isinstance (arg , (ABCDataFrame , MutableMapping )):
428
409
result = _assemble_from_unit_mappings (arg , errors = errors )
429
410
elif isinstance (arg , ABCIndexClass ):
430
- result = _maybe_convert_cache (arg , cache , box , format , name = arg .name )
431
- if result is None :
411
+ cache_array = _maybe_cache (arg , format , cache , tz , _convert_listlike )
412
+ if not cache_array .empty :
413
+ result = _convert_and_box_cache (arg , cache_array , box ,
414
+ name = arg .name )
415
+ else :
432
416
result = _convert_listlike (arg , box , format , name = arg .name )
433
417
elif is_list_like (arg ):
434
- result = _maybe_convert_cache (arg , cache , box , format )
435
- if result is None :
418
+ cache_array = _maybe_cache (arg , format , cache , tz , _convert_listlike )
419
+ if not cache_array .empty :
420
+ result = _convert_and_box_cache (arg , cache_array , box )
421
+ else :
436
422
result = _convert_listlike (arg , box , format )
437
423
else :
438
424
result = _convert_listlike (np .array ([arg ]), box , format )[0 ]
0 commit comments