Skip to content

Commit 216e365

Browse files
committed
Move docs and adjust test
1 parent 8e231ec commit 216e365

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

pandas/core/tools/datetimes.py

+33-22
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,26 @@ def _guess_datetime_format_for_array(arr, **kwargs):
3636
return _guess_datetime_format(arr[non_nan_elements[0]], **kwargs)
3737

3838

39-
def _maybe_cache(arg, format, cache, tz, _convert_listlike):
39+
def _maybe_cache(arg, format, cache, tz, convert_listlike):
4040
"""
4141
Create a cache of unique dates from an array of dates
4242
4343
Parameters
4444
----------
45-
arg : integer, float, string, datetime, list, tuple, 1-d array of dates
46-
format : string, strftime to parse time
47-
cache: boolean, whether to convert with cache
48-
tz: string, timezone of the dates
49-
_convert_listlike: function, conversion function to apply on dates
45+
arg : integer, float, string, datetime, list, tuple, 1-d array, Series
46+
format : string
47+
Strftime format to parse time
48+
cache : boolean
49+
True attempts to create a cache of converted values
50+
tz : string
51+
Timezone of the dates
52+
convert_listlike : function
53+
Conversion function to apply on dates
5054
5155
Returns
5256
-------
53-
cache_array: Series, cache of converted, unique dates, can be empty
57+
cache_array : Series
58+
Cache of converted, unique dates. Can be empty
5459
"""
5560
from pandas import Series
5661
cache_array = Series()
@@ -59,37 +64,43 @@ def _maybe_cache(arg, format, cache, tz, _convert_listlike):
5964
from pandas import Index
6065
if not Index(arg).is_unique:
6166
unique_dates = algorithms.unique(arg)
62-
cache_dates = _convert_listlike(unique_dates, True, format,
63-
tz=tz)
67+
cache_dates = convert_listlike(unique_dates, True, format, tz=tz)
6468
cache_array = Series(cache_dates, index=unique_dates)
6569
return cache_array
6670

6771

68-
def _convert_and_box_cache(arg, cache_array, box, errors, tz, name=None):
72+
def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
6973
"""
7074
Convert array of dates with a cache and box the result
7175
7276
Parameters
7377
----------
74-
arg : integer, float, string, datetime, list, tuple, 1-d array of dates
75-
cache_array: Series, cache of converted, unique dates
76-
box: boolean, True boxes result as an Index-like
77-
errors: string, 'ignore' plus box=True will convert result to Index
78-
tz: string, timezone of the dates
79-
name: string, default None. name for a DatetimeIndex
78+
arg : integer, float, string, datetime, list, tuple, 1-d array, Series
79+
cache_array : Series
80+
Cache of converted, unique dates
81+
box : boolean
82+
True boxes result as an Index-like, False returns an ndarray
83+
errors : string
84+
'ignore' plus box=True will convert result to Index
85+
name : string, default None
86+
Name for a DatetimeIndex
8087
8188
Returns
8289
-------
83-
result: Index-like if box=True else array-like of converted dates
90+
result : datetime of converted dates
91+
Returns:
92+
93+
- Index-like if box=True
94+
- ndarray if box=False
8495
"""
8596
from pandas import Series, DatetimeIndex, Index
8697
result = Series(arg).map(cache_array)
8798
if box:
8899
if errors == 'ignore':
89-
result = Index(result)
100+
return Index(result)
90101
else:
91-
result = DatetimeIndex(result, tz=tz, name=name)
92-
return result
102+
return DatetimeIndex(result, name=name)
103+
return result.values
93104

94105

95106
def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
@@ -443,14 +454,14 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
443454
elif isinstance(arg, ABCIndexClass):
444455
cache_array = _maybe_cache(arg, format, cache, tz, _convert_listlike)
445456
if not cache_array.empty:
446-
result = _convert_and_box_cache(arg, cache_array, box, errors, tz,
457+
result = _convert_and_box_cache(arg, cache_array, box, errors,
447458
name=arg.name)
448459
else:
449460
result = _convert_listlike(arg, box, format, name=arg.name)
450461
elif is_list_like(arg):
451462
cache_array = _maybe_cache(arg, format, cache, tz, _convert_listlike)
452463
if not cache_array.empty:
453-
result = _convert_and_box_cache(arg, cache_array, box, errors, tz)
464+
result = _convert_and_box_cache(arg, cache_array, box, errors)
454465
else:
455466
result = _convert_listlike(arg, box, format)
456467
else:

pandas/tests/indexes/datetimes/test_tools.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ def test_to_datetime_cache(self, utc, format, box, constructor):
414414
date = '20130101 00:00:00'
415415
test_dates = [date] * 10**5
416416
data = constructor(test_dates)
417-
result = pd.to_datetime(data, utc=utc, format=format, box=box)
417+
result = pd.to_datetime(data, utc=utc, format=format, box=box,
418+
cache=True)
418419
expected = pd.to_datetime(data, utc=utc, format=format, box=box,
419420
cache=False)
420421
if box:

0 commit comments

Comments
 (0)