Skip to content

Commit de604a6

Browse files
committed
Move box logic into maybe_convert_cache
1 parent bdb1a33 commit de604a6

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

pandas/core/tools/datetimes.py

+37-25
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
112112
origin.
113113
114114
.. versionadded: 0.20.0
115-
cache : boolean, default False
115+
cache : boolean, default True
116116
If True, use a cache of unique, converted dates to apply the datetime
117117
conversion. Produces signficant speed-ups when parsing duplicate dates.
118118
@@ -310,16 +310,32 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
310310
except (ValueError, TypeError):
311311
raise e
312312

313-
def _maybe_convert_cache(arg, cache, tz):
314-
"""Try to convert the datetimelike arg using
315-
a cache of converted dates.
316-
317-
arg: datetimelike arg from to_datetime
318-
cache: bool whether to convert using a cache
319-
320-
Returns:
321-
Series of converted datetime arg or
322-
None if the conversion failed
313+
def _maybe_convert_cache(arg, cache, box, format, name=None, tz=tz):
314+
"""
315+
Try to convert the datetimelike arg using
316+
a cache of converted dates.
317+
318+
Parameters
319+
----------
320+
arg : integer, float, string, datetime, list, tuple, 1-d array, Series
321+
Datetime argument to convert
322+
cache : boolean
323+
If True, try to convert the dates with a cache
324+
If False, short circuit and return None
325+
Flag whether to cache the converted dates
326+
box : boolean
327+
If True, return a DatetimeIndex
328+
if False, return an ndarray of values
329+
tz : String or None
330+
'utc' if UTC=True was passed else None
331+
name : String, default None
332+
DatetimeIndex name
333+
Returns
334+
-------
335+
Series if original argument was a Series
336+
DatetimeIndex if box=True and original argument was not a Series
337+
ndarray if box=False and original argument was not a Series
338+
None if the conversion failed
323339
"""
324340
if cache and is_list_like(arg) and len(arg) >= 1000:
325341
unique_dates = algorithms.unique(arg)
@@ -328,9 +344,13 @@ def _maybe_convert_cache(arg, cache, tz):
328344
cache_dates = _convert_listlike(unique_dates, True, format,
329345
tz=tz)
330346
convert_cache = Series(cache_dates, index=unique_dates)
331-
if not isinstance(arg, Series):
332-
arg = Series(arg)
333-
return arg.map(convert_cache)
347+
result = Series(arg, name=name).map(convert_cache)
348+
if isinstance(arg, Series):
349+
return result
350+
elif box:
351+
return DatetimeIndex(result, name=name)
352+
else:
353+
return result.values
334354
return None
335355

336356
if arg is None:
@@ -397,29 +417,21 @@ def _maybe_convert_cache(arg, cache, tz):
397417
if isinstance(arg, tslib.Timestamp):
398418
result = arg
399419
elif isinstance(arg, ABCSeries):
400-
result = _maybe_convert_cache(arg, cache, tz)
420+
result = _maybe_convert_cache(arg, cache, box, format, name=arg.name)
401421
if result is None:
402422
from pandas import Series
403423
values = _convert_listlike(arg._values, True, format)
404424
result = Series(values, index=arg.index, name=arg.name)
405425
elif isinstance(arg, (ABCDataFrame, MutableMapping)):
406426
result = _assemble_from_unit_mappings(arg, errors=errors)
407427
elif isinstance(arg, ABCIndexClass):
408-
result = _maybe_convert_cache(arg, cache, tz)
428+
result = _maybe_convert_cache(arg, cache, box, format, name=arg.name)
409429
if result is None:
410430
result = _convert_listlike(arg, box, format, name=arg.name)
411-
else:
412-
result = result.values
413-
if box:
414-
result = DatetimeIndex(result, tz=tz, name=arg.name)
415431
elif is_list_like(arg):
416-
result = _maybe_convert_cache(arg, cache, tz)
432+
result = _maybe_convert_cache(arg, cache, box, format)
417433
if result is None:
418434
result = _convert_listlike(arg, box, format)
419-
else:
420-
result = result.values
421-
if box:
422-
result = DatetimeIndex(result, tz=tz)
423435
else:
424436
result = _convert_listlike(np.array([arg]), box, format)[0]
425437

0 commit comments

Comments
 (0)