Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
In [2]: pd.to_datetime("00:01:18", format='H%:M%:S%') # raises, as expected
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~/pandas-dev/pandas/_libs/tslibs/strptime.pyx:136, in pandas._libs.tslibs.strptime.array_strptime()
135 try:
--> 136 format_regex = _TimeRE_cache.compile(fmt)
137 # KeyError raised when a bad format is found; can be specified as
File ~/mambaforge/envs/pandas-dev/lib/python3.8/_strptime.py:263, in TimeRE.compile(self, format)
262 """Return a compiled re object for the format string."""
--> 263 return re_compile(self.pattern(format), IGNORECASE)
File ~/mambaforge/envs/pandas-dev/lib/python3.8/_strptime.py:257, in TimeRE.pattern(self, format)
254 directive_index = format.index('%')+1
255 processed_format = "%s%s%s" % (processed_format,
256 format[:directive_index-1],
--> 257 self[format[directive_index]])
258 format = format[directive_index+1:]
File ~/pandas-dev/pandas/_libs/tslibs/strptime.pyx:461, in pandas._libs.tslibs.strptime.TimeRE.__getitem__()
460 return self._Z
--> 461 return super().__getitem__(key)
462
KeyError: ':'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Cell In[2], line 1
----> 1 pd.to_datetime("00:01:18", format='H%:M%:S%')
File ~/pandas-dev/pandas/core/tools/datetimes.py:1100, in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
1098 result = convert_listlike(argc, format)
1099 else:
-> 1100 result = convert_listlike(np.array([arg]), format)[0]
1101 if isinstance(arg, bool) and isinstance(result, np.bool_):
1102 result = bool(result) # TODO: avoid this kludge.
File ~/pandas-dev/pandas/core/tools/datetimes.py:442, in _convert_listlike_datetimes(arg, format, name, utc, unit, errors, dayfirst, yearfirst, exact)
439 require_iso8601 = format is not None and format_is_iso(format)
441 if format is not None and not require_iso8601:
--> 442 return _to_datetime_with_format(
443 arg,
444 orig_arg,
445 name,
446 utc,
447 format,
448 exact,
449 errors,
450 )
452 result, tz_parsed = objects_to_datetime64ns(
453 arg,
454 dayfirst=dayfirst,
(...)
461 exact=exact,
462 )
464 if tz_parsed is not None:
465 # We can take a shortcut since the datetime64 numpy array
466 # is in UTC
File ~/pandas-dev/pandas/core/tools/datetimes.py:543, in _to_datetime_with_format(arg, orig_arg, name, utc, fmt, exact, errors)
540 return _box_as_indexlike(result, utc=utc, name=name)
542 # fallback
--> 543 res = _array_strptime_with_fallback(arg, name, utc, fmt, exact, errors)
544 return res
File ~/pandas-dev/pandas/core/tools/datetimes.py:485, in _array_strptime_with_fallback(arg, name, utc, fmt, exact, errors)
481 """
482 Call array_strptime, with fallback behavior depending on 'errors'.
483 """
484 try:
--> 485 result, timezones = array_strptime(
486 arg, fmt, exact=exact, errors=errors, utc=utc
487 )
488 except OutOfBoundsDatetime:
489 if errors == "raise":
File ~/pandas-dev/pandas/_libs/tslibs/strptime.pyx:126, in pandas._libs.tslibs.strptime.array_strptime()
124
125 global _TimeRE_cache, _regex_cache
--> 126 with _cache_lock:
127 if _getlang() != _TimeRE_cache.locale_time.lang:
128 _TimeRE_cache = TimeRE()
File ~/pandas-dev/pandas/_libs/tslibs/strptime.pyx:144, in pandas._libs.tslibs.strptime.array_strptime()
142 bad_directive = "%"
143 del err
--> 144 raise ValueError(f"'{bad_directive}' is a bad directive "
145 f"in format '{fmt}'")
146 # IndexError only occurs when the format string is "%"
ValueError: ':' is a bad directive in format 'H%:M%:S%'
In [3]: pd.to_datetime("00:01:18", format='H%:M%:S%', errors='coerce') # doesn't raise
Out[3]: NaT
Issue Description
The issue with this overly broad try-except:
https://github.com/pandas-dev/pandas/blob/1d5f05c33c613508727ee7b971ad56723d474446/pandas/core/tools/datetimes.py#L484-L505
The try-except should happen within the loop, as is done in tslib.pyx
:
https://github.com/pandas-dev/pandas/blob/65987973b4299004aa794f84ef187a1b6b58aec1/pandas/_libs/tslib.pyx#L528-L531
Related: https://github.com/pandas-dev/pandas/issues/24763, but I really disagree that this is desirable behaviour
Expected Behavior
pd.to_datetime("00:01:18", format='H%:M%:S%', errors='coerce')
should still raise
errors='coerce'
controls what happens during parsing - here, however, the error happens before the parsing even begins, because the format is invalid