Skip to content

Commit 7c9292b

Browse files
committed
Fix to_datetime caching logic so test_to_datetime_offset passes
1 parent 281137d commit 7c9292b

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

pandas/core/tools/datetimes.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,42 @@ def _maybe_cache(arg, format, cache, convert_listlike):
6060
return cache_array
6161

6262

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):
6499
"""
65100
Convert array of dates with a cache and box the result
66101
@@ -71,8 +106,6 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
71106
Cache of converted, unique dates
72107
box : boolean
73108
True boxes result as an Index-like, False returns an ndarray
74-
errors : string
75-
'ignore' plus box=True will convert result to Index
76109
name : string, default None
77110
Name for a DatetimeIndex
78111
@@ -86,12 +119,7 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
86119
"""
87120
from pandas import Series, DatetimeIndex, Index
88121
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)
95123

96124

97125
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,
315343
for ts in result]
316344
return np.array(result, dtype=object)
317345

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)
327347

328348

329349
def _adjust_to_origin(arg, origin, unit):
@@ -605,15 +625,15 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
605625
elif isinstance(arg, ABCIndexClass):
606626
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
607627
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,
609629
name=arg.name)
610630
else:
611631
convert_listlike = partial(convert_listlike, name=arg.name)
612632
result = convert_listlike(arg, box, format)
613633
elif is_list_like(arg):
614634
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
615635
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)
617637
else:
618638
result = convert_listlike(arg, box, format)
619639
else:

0 commit comments

Comments
 (0)