Skip to content

Commit 67257cb

Browse files
committed
Rework _box_if_needed into _box_as_indexlike
1 parent cd7914d commit 67257cb

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

pandas/core/tools/datetimes.py

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

6262

63-
def _box_if_needed(dt_array, box, default, tz, name=None):
63+
def _box_as_indexlike(dt_array, tz=None, name=None):
6464
"""
65-
Properly boxes the ndarray of datetimes (if requested) to DatetimeIndex
65+
Properly boxes the ndarray of datetimes to DatetimeIndex
6666
if it is possible or to generic Index instead
6767
6868
Parameters
6969
----------
7070
dt_array: 1-d array
7171
array of datetimes to be boxed
72-
box : boolean
73-
True boxes result as an Index-like, False returns an ndarray
7472
tz : object
7573
None or 'utc'
7674
name : string, default None
@@ -79,18 +77,13 @@ def _box_if_needed(dt_array, box, default, tz, name=None):
7977
Returns
8078
-------
8179
result : datetime of converted dates
82-
Returns:
83-
84-
- Index-like if box=True
85-
- ndarray if box=False
80+
- DatetimeIndex if convertible to sole datetime64 type
81+
- general Index otherwise
8682
"""
87-
if box:
88-
from pandas import DatetimeIndex, Index
89-
if is_datetime64_dtype(dt_array):
90-
return DatetimeIndex(dt_array, tz=tz, name=name)
91-
# e.g. an Index of datetime objects
92-
return Index(dt_array, name=name)
93-
return default
83+
from pandas import DatetimeIndex, Index
84+
if is_datetime64_dtype(dt_array):
85+
return DatetimeIndex(dt_array, tz=tz, name=name)
86+
return Index(dt_array, name=name)
9487

9588

9689
def _convert_and_box_cache(arg, cache_array, box, name=None):
@@ -117,7 +110,9 @@ def _convert_and_box_cache(arg, cache_array, box, name=None):
117110
"""
118111
from pandas import Series
119112
result = Series(arg).map(cache_array)
120-
return _box_if_needed(result, box, result.values, None, name)
113+
if box:
114+
return _box_as_indexlike(result, tz=None, name=name)
115+
return result.values
121116

122117

123118
def _return_parsed_timezone_results(result, timezones, box, tz, name):
@@ -341,7 +336,9 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
341336
for ts in result]
342337
return np.array(result, dtype=object)
343338

344-
return _box_if_needed(result, box, result, tz, name)
339+
if box:
340+
return _box_as_indexlike(result, tz=tz, name=name)
341+
return result
345342

346343

347344
def _adjust_to_origin(arg, origin, unit):

0 commit comments

Comments
 (0)