@@ -284,7 +284,7 @@ def _convert_listlike_datetimes(
284
284
tz : Optional [Timezone ] = None ,
285
285
unit : Optional [str ] = None ,
286
286
errors : Optional [str ] = None ,
287
- infer_datetime_format : Optional [ bool ] = None ,
287
+ infer_datetime_format : bool = False ,
288
288
dayfirst : Optional [bool ] = None ,
289
289
yearfirst : Optional [bool ] = None ,
290
290
exact : bool = True ,
@@ -305,7 +305,7 @@ def _convert_listlike_datetimes(
305
305
None or string of the frequency of the passed data
306
306
errors : string
307
307
error handing behaviors from to_datetime, 'raise', 'coerce', 'ignore'
308
- infer_datetime_format : boolean
308
+ infer_datetime_format : bool, default False
309
309
inferring format behavior from to_datetime
310
310
dayfirst : boolean
311
311
dayfirst parsing behavior from to_datetime
@@ -413,17 +413,16 @@ def _convert_listlike_datetimes(
413
413
require_iso8601 = not infer_datetime_format
414
414
format = None
415
415
416
- tz_parsed = None
417
416
result = None
418
417
419
418
if format is not None :
420
419
try :
421
420
# shortcut formatting here
422
421
if format == "%Y%m%d" :
422
+ # pass orig_arg as float-dtype may have been converted to
423
+ # datetime64[ns]
424
+ orig_arg = ensure_object (orig_arg )
423
425
try :
424
- # pass orig_arg as float-dtype may have been converted to
425
- # datetime64[ns]
426
- orig_arg = ensure_object (orig_arg )
427
426
result = _attempt_YYYYMMDD (orig_arg , errors = errors )
428
427
except (ValueError , TypeError , OutOfBoundsDatetime ) as err :
429
428
raise ValueError (
@@ -432,36 +431,12 @@ def _convert_listlike_datetimes(
432
431
433
432
# fallback
434
433
if result is None :
435
- try :
436
- result , timezones = array_strptime (
437
- arg , format , exact = exact , errors = errors
438
- )
439
- if "%Z" in format or "%z" in format :
440
- return _return_parsed_timezone_results (
441
- result , timezones , tz , name
442
- )
443
- except OutOfBoundsDatetime :
444
- if errors == "raise" :
445
- raise
446
- elif errors == "coerce" :
447
- result = np .empty (arg .shape , dtype = "M8[ns]" )
448
- iresult = result .view ("i8" )
449
- iresult .fill (iNaT )
450
- else :
451
- result = arg
452
- except ValueError :
453
- # if format was inferred, try falling back
454
- # to array_to_datetime - terminate here
455
- # for specified formats
456
- if not infer_datetime_format :
457
- if errors == "raise" :
458
- raise
459
- elif errors == "coerce" :
460
- result = np .empty (arg .shape , dtype = "M8[ns]" )
461
- iresult = result .view ("i8" )
462
- iresult .fill (iNaT )
463
- else :
464
- result = arg
434
+ result = _array_strptime_with_fallback (
435
+ arg , name , tz , format , exact , errors , infer_datetime_format
436
+ )
437
+ if result is not None :
438
+ return result
439
+
465
440
except ValueError as e :
466
441
# Fallback to try to convert datetime objects if timezone-aware
467
442
# datetime objects are found without passing `utc=True`
@@ -485,16 +460,63 @@ def _convert_listlike_datetimes(
485
460
allow_object = True ,
486
461
)
487
462
488
- if tz_parsed is not None :
489
- # We can take a shortcut since the datetime64 numpy array
490
- # is in UTC
491
- dta = DatetimeArray (result , dtype = tz_to_dtype (tz_parsed ))
492
- return DatetimeIndex ._simple_new (dta , name = name )
463
+ if tz_parsed is not None :
464
+ # We can take a shortcut since the datetime64 numpy array
465
+ # is in UTC
466
+ dta = DatetimeArray (result , dtype = tz_to_dtype (tz_parsed ))
467
+ return DatetimeIndex ._simple_new (dta , name = name )
493
468
494
469
utc = tz == "utc"
495
470
return _box_as_indexlike (result , utc = utc , name = name )
496
471
497
472
473
+ def _array_strptime_with_fallback (
474
+ arg ,
475
+ name ,
476
+ tz ,
477
+ fmt : str ,
478
+ exact : bool ,
479
+ errors : Optional [str ],
480
+ infer_datetime_format : bool ,
481
+ ) -> Optional [Index ]:
482
+ """
483
+ Call array_strptime, with fallback behavior depending on 'errors'.
484
+ """
485
+ utc = tz == "utc"
486
+
487
+ try :
488
+ result , timezones = array_strptime (arg , fmt , exact = exact , errors = errors )
489
+ if "%Z" in fmt or "%z" in fmt :
490
+ return _return_parsed_timezone_results (result , timezones , tz , name )
491
+ except OutOfBoundsDatetime :
492
+ if errors == "raise" :
493
+ raise
494
+ elif errors == "coerce" :
495
+ result = np .empty (arg .shape , dtype = "M8[ns]" )
496
+ iresult = result .view ("i8" )
497
+ iresult .fill (iNaT )
498
+ else :
499
+ result = arg
500
+ except ValueError :
501
+ # if fmt was inferred, try falling back
502
+ # to array_to_datetime - terminate here
503
+ # for specified formats
504
+ if not infer_datetime_format :
505
+ if errors == "raise" :
506
+ raise
507
+ elif errors == "coerce" :
508
+ result = np .empty (arg .shape , dtype = "M8[ns]" )
509
+ iresult = result .view ("i8" )
510
+ iresult .fill (iNaT )
511
+ else :
512
+ result = arg
513
+ else :
514
+ # Indicates to the caller to fallback to objects_to_datetime64ns
515
+ return None
516
+
517
+ return _box_as_indexlike (result , utc = utc , name = name )
518
+
519
+
498
520
def _adjust_to_origin (arg , origin , unit ):
499
521
"""
500
522
Helper function for to_datetime.
0 commit comments