@@ -231,9 +231,9 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
231
231
require_iso8601 = not infer_datetime_format
232
232
format = None
233
233
234
+ tz_parsed = None
235
+ result = None
234
236
try :
235
- result = None
236
-
237
237
if format is not None :
238
238
# shortcut formatting here
239
239
if format == '%Y%m%d' :
@@ -267,7 +267,8 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
267
267
raise
268
268
result = arg
269
269
270
- if result is None and (format is None or infer_datetime_format ):
270
+ if result is None :
271
+ assert format is None or infer_datetime_format
271
272
result , tz_parsed = tslib .array_to_datetime (
272
273
arg ,
273
274
errors = errors ,
@@ -276,36 +277,37 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
276
277
yearfirst = yearfirst ,
277
278
require_iso8601 = require_iso8601
278
279
)
279
- if tz_parsed is not None :
280
- if box :
281
- # We can take a shortcut since the datetime64 numpy array
282
- # is in UTC
283
- return DatetimeIndex ._simple_new (result , name = name ,
284
- tz = tz_parsed )
285
- else :
286
- # Convert the datetime64 numpy array to an numpy array
287
- # of datetime objects
288
- result = [Timestamp (ts , tz = tz_parsed ).to_pydatetime ()
289
- for ts in result ]
290
- return np .array (result , dtype = object )
291
-
292
- if box :
293
- # Ensure we return an Index in all cases where box=True
294
- if is_datetime64_dtype (result ):
295
- return DatetimeIndex (result , tz = tz , name = name )
296
- elif is_object_dtype (result ):
297
- # e.g. an Index of datetime objects
298
- from pandas import Index
299
- return Index (result , name = name )
300
- return result
301
-
302
280
except ValueError as e :
281
+ # Fallback to try to convert datetime objects
303
282
try :
304
283
values , tz = conversion .datetime_to_datetime64 (arg )
305
284
return DatetimeIndex ._simple_new (values , name = name , tz = tz )
306
285
except (ValueError , TypeError ):
307
286
raise e
308
287
288
+ if tz_parsed is not None :
289
+ if box :
290
+ # We can take a shortcut since the datetime64 numpy array
291
+ # is in UTC
292
+ return DatetimeIndex ._simple_new (result , name = name ,
293
+ tz = tz_parsed )
294
+ else :
295
+ # Convert the datetime64 numpy array to an numpy array
296
+ # of datetime objects
297
+ result = [Timestamp (ts , tz = tz_parsed ).to_pydatetime ()
298
+ for ts in result ]
299
+ return np .array (result , dtype = object )
300
+
301
+ if box :
302
+ # Ensure we return an Index in all cases where box=True
303
+ if is_datetime64_dtype (result ):
304
+ return DatetimeIndex (result , tz = tz , name = name )
305
+ elif is_object_dtype (result ):
306
+ # e.g. an Index of datetime objects
307
+ from pandas import Index
308
+ return Index (result , name = name )
309
+ return result
310
+
309
311
310
312
def _adjust_to_origin (arg , origin , unit ):
311
313
"""
0 commit comments