@@ -359,7 +359,9 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
359
359
return DatetimeIndex (arg , tz = tz , name = name )
360
360
except ValueError :
361
361
pass
362
-
362
+ from pandas import Series
363
+ if isinstance (arg , Series ) and utc :
364
+ arg = arg .dt .tz_localize ('utc' )
363
365
return arg
364
366
365
367
elif unit is not None :
@@ -379,11 +381,12 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
379
381
raise TypeError ('arg must be a string, datetime, list, tuple, '
380
382
'1-d array, or Series' )
381
383
382
- arg = _ensure_object (arg )
384
+ obj_arg = _ensure_object (arg )
383
385
require_iso8601 = False
384
386
385
387
if infer_datetime_format and format is None :
386
- format = _guess_datetime_format_for_array (arg , dayfirst = dayfirst )
388
+ format = _guess_datetime_format_for_array (obj_arg ,
389
+ dayfirst = dayfirst )
387
390
388
391
if format is not None :
389
392
# There is a special fast-path for iso8601 formatted
@@ -402,46 +405,50 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
402
405
# shortcut formatting here
403
406
if format == '%Y%m%d' :
404
407
try :
405
- result = _attempt_YYYYMMDD (arg , errors = errors )
408
+ result = _attempt_YYYYMMDD (obj_arg , errors = errors )
406
409
except :
407
410
raise ValueError ("cannot convert the input to "
408
411
"'%Y%m%d' date format" )
409
412
410
413
# fallback
411
414
if result is None :
412
415
try :
413
- result = tslib .array_strptime (arg , format , exact = exact ,
416
+ result = tslib .array_strptime (obj_arg , format ,
417
+ exact = exact ,
414
418
errors = errors )
415
419
except tslib .OutOfBoundsDatetime :
416
420
if errors == 'raise' :
417
421
raise
418
- result = arg
422
+ result = obj_arg
419
423
except ValueError :
420
424
# if format was inferred, try falling back
421
425
# to array_to_datetime - terminate here
422
426
# for specified formats
423
427
if not infer_datetime_format :
424
428
if errors == 'raise' :
425
429
raise
426
- result = arg
430
+ result = obj_arg
427
431
428
432
if result is None and (format is None or infer_datetime_format ):
429
433
result = tslib .array_to_datetime (
430
- arg ,
434
+ obj_arg ,
431
435
errors = errors ,
432
436
utc = utc ,
433
437
dayfirst = dayfirst ,
434
438
yearfirst = yearfirst ,
435
439
require_iso8601 = require_iso8601
436
440
)
437
-
441
+ from pandas import Series
438
442
if is_datetime64_dtype (result ) and box :
439
443
result = DatetimeIndex (result , tz = tz , name = name )
444
+ # GH 6415
445
+ elif isinstance (arg , Series ) and utc :
446
+ result = Series (result , name = name ).dt .tz_localize ('utc' )
440
447
return result
441
448
442
449
except ValueError as e :
443
450
try :
444
- values , tz = tslib .datetime_to_datetime64 (arg )
451
+ values , tz = tslib .datetime_to_datetime64 (obj_arg )
445
452
return DatetimeIndex ._simple_new (values , name = name , tz = tz )
446
453
except (ValueError , TypeError ):
447
454
raise e
@@ -506,7 +513,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
506
513
result = arg
507
514
elif isinstance (arg , ABCSeries ):
508
515
from pandas import Series
509
- values = _convert_listlike (arg . _values , False , format )
516
+ values = _convert_listlike (arg , False , format , name = arg . name )
510
517
result = Series (values , index = arg .index , name = arg .name )
511
518
elif isinstance (arg , (ABCDataFrame , MutableMapping )):
512
519
result = _assemble_from_unit_mappings (arg , errors = errors )
0 commit comments