@@ -340,6 +340,17 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
340340
341341 tz = 'utc' if utc else None
342342
343+ def _maybe_convert_to_utc (arg , utc ):
344+ if utc :
345+ if isinstance (arg , ABCSeries ):
346+ arg = arg .dt .tz_localize ('UTC' )
347+ elif isinstance (arg , DatetimeIndex ):
348+ if arg .tz is None :
349+ arg = arg .tz_localize ('UTC' )
350+ else :
351+ arg = arg .tz_convert ('UTC' )
352+ return arg
353+
343354 def _convert_listlike (arg , box , format , name = None , tz = tz ):
344355
345356 if isinstance (arg , (list , tuple )):
@@ -359,7 +370,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
359370 return DatetimeIndex (arg , tz = tz , name = name )
360371 except ValueError :
361372 pass
362-
373+ arg = _maybe_convert_to_utc ( arg , utc )
363374 return arg
364375
365376 elif unit is not None :
@@ -378,12 +389,15 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
378389 elif getattr (arg , 'ndim' , 1 ) > 1 :
379390 raise TypeError ('arg must be a string, datetime, list, tuple, '
380391 '1-d array, or Series' )
381-
392+ # _ensure_object converts Series to numpy array, need to reconvert
393+ # upon return
394+ arg_is_series = isinstance (arg , ABCSeries )
382395 arg = _ensure_object (arg )
383396 require_iso8601 = False
384397
385398 if infer_datetime_format and format is None :
386- format = _guess_datetime_format_for_array (arg , dayfirst = dayfirst )
399+ format = _guess_datetime_format_for_array (arg ,
400+ dayfirst = dayfirst )
387401
388402 if format is not None :
389403 # There is a special fast-path for iso8601 formatted
@@ -410,7 +424,8 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
410424 # fallback
411425 if result is None :
412426 try :
413- result = tslib .array_strptime (arg , format , exact = exact ,
427+ result = tslib .array_strptime (arg , format ,
428+ exact = exact ,
414429 errors = errors )
415430 except tslib .OutOfBoundsDatetime :
416431 if errors == 'raise' :
@@ -434,9 +449,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
434449 yearfirst = yearfirst ,
435450 require_iso8601 = require_iso8601
436451 )
437-
438452 if is_datetime64_dtype (result ) and box :
439453 result = DatetimeIndex (result , tz = tz , name = name )
454+ # GH 6415
455+ elif arg_is_series :
456+ result = _maybe_convert_to_utc (Series (result , name = name ), utc )
440457 return result
441458
442459 except ValueError as e :
@@ -506,7 +523,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
506523 result = arg
507524 elif isinstance (arg , ABCSeries ):
508525 from pandas import Series
509- values = _convert_listlike (arg . _values , False , format )
526+ values = _convert_listlike (arg , False , format , name = arg . name )
510527 result = Series (values , index = arg .index , name = arg .name )
511528 elif isinstance (arg , (ABCDataFrame , MutableMapping )):
512529 result = _assemble_from_unit_mappings (arg , errors = errors )
0 commit comments