Skip to content

Commit 1390683

Browse files
committed
Wrap cache logic in a function
1 parent e81e8c9 commit 1390683

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

pandas/core/tools/datetimes.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
116116
If True, use a cache of unique, converted dates to apply the datetime
117117
conversion. Produces signficant speed-ups when parsing duplicate date.
118118
119-
.. versionadded: 0.20.2
119+
.. versionadded: 0.21.0
120120
Returns
121121
-------
122122
ret : datetime if parsing succeeded.
@@ -310,6 +310,28 @@ 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):
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+
Result:
321+
Series of converted datetime arg or
322+
None if the conversion failed
323+
"""
324+
if cache and is_list_like(arg) and len(arg) >= 1000:
325+
unique_dates = algorithms.unique(arg)
326+
if len(unique_dates) != len(arg):
327+
from pandas import Series
328+
cache_dates = _convert_listlike(unique_dates, False, format)
329+
convert_cache = Series(cache_dates, index=unique_dates)
330+
if not isinstance(arg, Series):
331+
arg = Series(arg)
332+
return arg.map(convert_cache)
333+
return None
334+
313335
if arg is None:
314336
return None
315337

@@ -371,41 +393,32 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
371393
arg = np.asarray(arg)
372394
arg = arg + offset
373395

374-
convert_cache = None
375-
if cache and is_list_like(arg) and len(arg) >= 1000:
376-
unique_dates = algorithms.unique(arg)
377-
if len(unique_dates) != len(arg):
378-
from pandas import Series
379-
cache_dates = _convert_listlike(unique_dates, False, format)
380-
convert_cache = Series(cache_dates, index=unique_dates)
381-
382396
if isinstance(arg, tslib.Timestamp):
383397
result = arg
384398
elif isinstance(arg, ABCSeries):
385-
if convert_cache is not None:
386-
result = arg.map(convert_cache)
387-
else:
399+
result = _maybe_convert_cache(arg, cache)
400+
if result is None:
388401
from pandas import Series
389402
values = _convert_listlike(arg._values, True, format)
390403
result = Series(values, index=arg.index, name=arg.name)
391404
elif isinstance(arg, (ABCDataFrame, MutableMapping)):
392405
result = _assemble_from_unit_mappings(arg, errors=errors)
393406
elif isinstance(arg, ABCIndexClass):
394-
if convert_cache is not None:
395-
from pandas import Series
396-
result = Series(arg).map(convert_cache).values
407+
result = _maybe_convert_cache(arg, cache)
408+
if result is None:
409+
result = _convert_listlike(arg, box, format, name=arg.name)
410+
else:
411+
result = result.values
397412
if box:
398413
result = DatetimeIndex(result, tz=tz, name=arg.name)
399-
else:
400-
result = _convert_listlike(arg, box, format, name=arg.name)
401414
elif is_list_like(arg):
402-
if convert_cache is not None:
403-
from pandas import Series
404-
result = Series(arg).map(convert_cache).values
415+
result = _maybe_convert_cache(arg, cache)
416+
if result is None:
417+
result = _convert_listlike(arg, box, format)
418+
else:
419+
result = result.values
405420
if box:
406421
result = DatetimeIndex(result, tz=tz)
407-
else:
408-
result = _convert_listlike(arg, box, format)
409422
else:
410423
result = _convert_listlike(np.array([arg]), box, format)[0]
411424

0 commit comments

Comments
 (0)