8
8
import pandas .tslib as tslib
9
9
import pandas .core .common as com
10
10
from pandas .compat import StringIO , callable
11
+ from pandas .core .common import ABCIndexClass
11
12
import pandas .compat as compat
12
13
from pandas .util .decorators import deprecate_kwarg
13
14
@@ -277,7 +278,7 @@ def _to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
277
278
from pandas .core .series import Series
278
279
from pandas .tseries .index import DatetimeIndex
279
280
280
- def _convert_listlike (arg , box , format ):
281
+ def _convert_listlike (arg , box , format , name = None ):
281
282
282
283
if isinstance (arg , (list ,tuple )):
283
284
arg = np .array (arg , dtype = 'O' )
@@ -286,15 +287,15 @@ def _convert_listlike(arg, box, format):
286
287
if com .is_datetime64_ns_dtype (arg ):
287
288
if box and not isinstance (arg , DatetimeIndex ):
288
289
try :
289
- return DatetimeIndex (arg , tz = 'utc' if utc else None )
290
+ return DatetimeIndex (arg , tz = 'utc' if utc else None , name = name )
290
291
except ValueError :
291
292
pass
292
293
293
294
return arg
294
295
elif format is None and com .is_integer_dtype (arg ) and unit == 'ns' :
295
296
result = arg .astype ('datetime64[ns]' )
296
297
if box :
297
- return DatetimeIndex (result , tz = 'utc' if utc else None )
298
+ return DatetimeIndex (result , tz = 'utc' if utc else None , name = name )
298
299
299
300
return result
300
301
@@ -355,13 +356,13 @@ def _convert_listlike(arg, box, format):
355
356
require_iso8601 = require_iso8601 )
356
357
357
358
if com .is_datetime64_dtype (result ) and box :
358
- result = DatetimeIndex (result , tz = 'utc' if utc else None )
359
+ result = DatetimeIndex (result , tz = 'utc' if utc else None , name = name )
359
360
return result
360
361
361
362
except ValueError as e :
362
363
try :
363
364
values , tz = tslib .datetime_to_datetime64 (arg )
364
- return DatetimeIndex ._simple_new (values , None , tz = tz )
365
+ return DatetimeIndex ._simple_new (values , name = name , tz = tz )
365
366
except (ValueError , TypeError ):
366
367
raise e
367
368
@@ -372,6 +373,8 @@ def _convert_listlike(arg, box, format):
372
373
elif isinstance (arg , Series ):
373
374
values = _convert_listlike (arg .values , False , format )
374
375
return Series (values , index = arg .index , name = arg .name )
376
+ elif isinstance (arg , ABCIndexClass ):
377
+ return _convert_listlike (arg , box , format , name = arg .name )
375
378
elif com .is_list_like (arg ):
376
379
return _convert_listlike (arg , box , format )
377
380
0 commit comments