diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index e23bf6269893d..d8be68d9ccf62 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -251,6 +251,8 @@ def _maybe_cache( unique_dates = unique(arg) if len(unique_dates) < len(arg): + # GH-55345 checking for parse errors + convert_listlike(arg, format) cache_dates = convert_listlike(unique_dates, format) # GH#45319 try: diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index aefaba1aed058..f8ed06026d5e5 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -3686,3 +3686,11 @@ def test_to_datetime_with_empty_str_utc_false_offsets_and_format_mixed(): to_datetime( ["2020-01-01 00:00+00:00", "2020-01-01 00:00+02:00", ""], format="mixed" ) + + +def test_to_datetime_parse_error_for_more_than_50rows(): + # GH-55345 + msg = 'when parsing with format "%Y-%m-%d": " 09", at position 50.' + + with pytest.raises(ValueError, match=msg): + to_datetime(["2012-01-01"] * 50 + ["2012-01-02 09"])