@@ -36,21 +36,26 @@ def _guess_datetime_format_for_array(arr, **kwargs):
36
36
return _guess_datetime_format (arr [non_nan_elements [0 ]], ** kwargs )
37
37
38
38
39
- def _maybe_cache (arg , format , cache , tz , _convert_listlike ):
39
+ def _maybe_cache (arg , format , cache , tz , convert_listlike ):
40
40
"""
41
41
Create a cache of unique dates from an array of dates
42
42
43
43
Parameters
44
44
----------
45
- arg : integer, float, string, datetime, list, tuple, 1-d array of dates
46
- format : string, strftime to parse time
47
- cache: boolean, whether to convert with cache
48
- tz: string, timezone of the dates
49
- _convert_listlike: function, conversion function to apply on dates
45
+ arg : integer, float, string, datetime, list, tuple, 1-d array, Series
46
+ format : string
47
+ Strftime format to parse time
48
+ cache : boolean
49
+ True attempts to create a cache of converted values
50
+ tz : string
51
+ Timezone of the dates
52
+ convert_listlike : function
53
+ Conversion function to apply on dates
50
54
51
55
Returns
52
56
-------
53
- cache_array: Series, cache of converted, unique dates, can be empty
57
+ cache_array : Series
58
+ Cache of converted, unique dates. Can be empty
54
59
"""
55
60
from pandas import Series
56
61
cache_array = Series ()
@@ -59,37 +64,43 @@ def _maybe_cache(arg, format, cache, tz, _convert_listlike):
59
64
from pandas import Index
60
65
if not Index (arg ).is_unique :
61
66
unique_dates = algorithms .unique (arg )
62
- cache_dates = _convert_listlike (unique_dates , True , format ,
63
- tz = tz )
67
+ cache_dates = convert_listlike (unique_dates , True , format , tz = tz )
64
68
cache_array = Series (cache_dates , index = unique_dates )
65
69
return cache_array
66
70
67
71
68
- def _convert_and_box_cache (arg , cache_array , box , errors , tz , name = None ):
72
+ def _convert_and_box_cache (arg , cache_array , box , errors , name = None ):
69
73
"""
70
74
Convert array of dates with a cache and box the result
71
75
72
76
Parameters
73
77
----------
74
- arg : integer, float, string, datetime, list, tuple, 1-d array of dates
75
- cache_array: Series, cache of converted, unique dates
76
- box: boolean, True boxes result as an Index-like
77
- errors: string, 'ignore' plus box=True will convert result to Index
78
- tz: string, timezone of the dates
79
- name: string, default None. name for a DatetimeIndex
78
+ arg : integer, float, string, datetime, list, tuple, 1-d array, Series
79
+ cache_array : Series
80
+ Cache of converted, unique dates
81
+ box : boolean
82
+ True boxes result as an Index-like, False returns an ndarray
83
+ errors : string
84
+ 'ignore' plus box=True will convert result to Index
85
+ name : string, default None
86
+ Name for a DatetimeIndex
80
87
81
88
Returns
82
89
-------
83
- result: Index-like if box=True else array-like of converted dates
90
+ result : datetime of converted dates
91
+ Returns:
92
+
93
+ - Index-like if box=True
94
+ - ndarray if box=False
84
95
"""
85
96
from pandas import Series , DatetimeIndex , Index
86
97
result = Series (arg ).map (cache_array )
87
98
if box :
88
99
if errors == 'ignore' :
89
- result = Index (result )
100
+ return Index (result )
90
101
else :
91
- result = DatetimeIndex (result , tz = tz , name = name )
92
- return result
102
+ return DatetimeIndex (result , name = name )
103
+ return result . values
93
104
94
105
95
106
def to_datetime (arg , errors = 'raise' , dayfirst = False , yearfirst = False ,
@@ -443,14 +454,14 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
443
454
elif isinstance (arg , ABCIndexClass ):
444
455
cache_array = _maybe_cache (arg , format , cache , tz , _convert_listlike )
445
456
if not cache_array .empty :
446
- result = _convert_and_box_cache (arg , cache_array , box , errors , tz ,
457
+ result = _convert_and_box_cache (arg , cache_array , box , errors ,
447
458
name = arg .name )
448
459
else :
449
460
result = _convert_listlike (arg , box , format , name = arg .name )
450
461
elif is_list_like (arg ):
451
462
cache_array = _maybe_cache (arg , format , cache , tz , _convert_listlike )
452
463
if not cache_array .empty :
453
- result = _convert_and_box_cache (arg , cache_array , box , errors , tz )
464
+ result = _convert_and_box_cache (arg , cache_array , box , errors )
454
465
else :
455
466
result = _convert_listlike (arg , box , format )
456
467
else :
0 commit comments