@@ -60,7 +60,42 @@ def _maybe_cache(arg, format, cache, convert_listlike):
60
60
return cache_array
61
61
62
62
63
- def _convert_and_box_cache (arg , cache_array , box , errors , name = None ):
63
+ def _box_if_needed (dt_array , box , default , tz , name ):
64
+ """
65
+ Properly boxes the ndarray of datetimes (if requested) to DatetimeIndex
66
+ if it is possible or to generic Index instead
67
+
68
+ Parameters
69
+ ----------
70
+ dt_array: 1-d array
71
+ array of datetimes to be boxed
72
+ box : boolean
73
+ True boxes result as an Index-like, False returns an ndarray
74
+ tz : object
75
+ None or 'utc'
76
+ name : string, default None
77
+ Name for a resulting index
78
+
79
+ Returns
80
+ -------
81
+ result : datetime of converted dates
82
+ Returns:
83
+
84
+ - Index-like if box=True
85
+ - ndarray if box=False
86
+ """
87
+ if box :
88
+ from pandas import DatetimeIndex , Index
89
+ print (type (dt_array ))
90
+ if is_datetime64_dtype (dt_array ):
91
+ return DatetimeIndex (dt_array , tz = tz , name = name )
92
+ #elif is_object_dtype(dt_array):
93
+ # e.g. an Index of datetime objects
94
+ return Index (dt_array , name = name )
95
+ return default
96
+
97
+
98
+ def _convert_and_box_cache (arg , cache_array , box , name = None ):
64
99
"""
65
100
Convert array of dates with a cache and box the result
66
101
@@ -71,8 +106,6 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
71
106
Cache of converted, unique dates
72
107
box : boolean
73
108
True boxes result as an Index-like, False returns an ndarray
74
- errors : string
75
- 'ignore' plus box=True will convert result to Index
76
109
name : string, default None
77
110
Name for a DatetimeIndex
78
111
@@ -86,12 +119,7 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
86
119
"""
87
120
from pandas import Series , DatetimeIndex , Index
88
121
result = Series (arg ).map (cache_array )
89
- if box :
90
- if errors == 'ignore' :
91
- return Index (result , name = name )
92
- else :
93
- return DatetimeIndex (result , name = name )
94
- return result .values
122
+ return _box_if_needed (result , box , result .values , None , name )
95
123
96
124
97
125
def _return_parsed_timezone_results (result , timezones , box , tz , name ):
@@ -315,15 +343,7 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
315
343
for ts in result ]
316
344
return np .array (result , dtype = object )
317
345
318
- if box :
319
- # Ensure we return an Index in all cases where box=True
320
- if is_datetime64_dtype (result ):
321
- return DatetimeIndex (result , tz = tz , name = name )
322
- elif is_object_dtype (result ):
323
- # e.g. an Index of datetime objects
324
- from pandas import Index
325
- return Index (result , name = name )
326
- return result
346
+ return _box_if_needed (result , box , result , tz , name )
327
347
328
348
329
349
def _adjust_to_origin (arg , origin , unit ):
@@ -605,15 +625,15 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
605
625
elif isinstance (arg , ABCIndexClass ):
606
626
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
607
627
if not cache_array .empty :
608
- result = _convert_and_box_cache (arg , cache_array , box , errors ,
628
+ result = _convert_and_box_cache (arg , cache_array , box ,
609
629
name = arg .name )
610
630
else :
611
631
convert_listlike = partial (convert_listlike , name = arg .name )
612
632
result = convert_listlike (arg , box , format )
613
633
elif is_list_like (arg ):
614
634
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
615
635
if not cache_array .empty :
616
- result = _convert_and_box_cache (arg , cache_array , box , errors )
636
+ result = _convert_and_box_cache (arg , cache_array , box )
617
637
else :
618
638
result = convert_listlike (arg , box , format )
619
639
else :
0 commit comments