Skip to content

Commit f4374cc

Browse files
vnlitvinovanmyachev
authored andcommitted
Fix to_datetime caching logic so test_to_datetime_offset passes
1 parent 1f4b199 commit f4374cc

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
@@ -59,7 +59,42 @@ def _maybe_cache(arg, format, cache, convert_listlike):
5959
return cache_array
6060

6161

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):
6398
"""
6499
Convert array of dates with a cache and box the result
65100
@@ -70,8 +105,6 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
70105
Cache of converted, unique dates
71106
box : boolean
72107
True boxes result as an Index-like, False returns an ndarray
73-
errors : string
74-
'ignore' plus box=True will convert result to Index
75108
name : string, default None
76109
Name for a DatetimeIndex
77110
@@ -85,12 +118,7 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
85118
"""
86119
from pandas import Series, DatetimeIndex, Index
87120
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)
94122

95123

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

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

327347

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

0 commit comments

Comments
 (0)