@@ -340,6 +340,17 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
340
340
341
341
tz = 'utc' if utc else None
342
342
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
+
343
354
def _convert_listlike (arg , box , format , name = None , tz = tz ):
344
355
345
356
if isinstance (arg , (list , tuple )):
@@ -359,7 +370,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
359
370
return DatetimeIndex (arg , tz = tz , name = name )
360
371
except ValueError :
361
372
pass
362
-
373
+ arg = _maybe_convert_to_utc ( arg , utc )
363
374
return arg
364
375
365
376
elif unit is not None :
@@ -378,12 +389,15 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
378
389
elif getattr (arg , 'ndim' , 1 ) > 1 :
379
390
raise TypeError ('arg must be a string, datetime, list, tuple, '
380
391
'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 )
382
395
arg = _ensure_object (arg )
383
396
require_iso8601 = False
384
397
385
398
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 )
387
401
388
402
if format is not None :
389
403
# There is a special fast-path for iso8601 formatted
@@ -410,7 +424,8 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
410
424
# fallback
411
425
if result is None :
412
426
try :
413
- result = tslib .array_strptime (arg , format , exact = exact ,
427
+ result = tslib .array_strptime (arg , format ,
428
+ exact = exact ,
414
429
errors = errors )
415
430
except tslib .OutOfBoundsDatetime :
416
431
if errors == 'raise' :
@@ -434,9 +449,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
434
449
yearfirst = yearfirst ,
435
450
require_iso8601 = require_iso8601
436
451
)
437
-
438
452
if is_datetime64_dtype (result ) and box :
439
453
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 )
440
457
return result
441
458
442
459
except ValueError as e :
@@ -506,7 +523,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
506
523
result = arg
507
524
elif isinstance (arg , ABCSeries ):
508
525
from pandas import Series
509
- values = _convert_listlike (arg . _values , False , format )
526
+ values = _convert_listlike (arg , False , format , name = arg . name )
510
527
result = Series (values , index = arg .index , name = arg .name )
511
528
elif isinstance (arg , (ABCDataFrame , MutableMapping )):
512
529
result = _assemble_from_unit_mappings (arg , errors = errors )
0 commit comments