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