diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index e0fcc829ad326..5c93edfee79f2 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -1053,8 +1053,12 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None: found_attrs.update(attrs) break - # Only consider it a valid guess if we have a year, month and day - if len({'year', 'month', 'day'} & found_attrs) != 3: + # Only consider it a valid guess if we have a year, month and day, + # unless it's %Y which is both common and unambiguous. + if ( + len({'year', 'month', 'day'} & found_attrs) != 3 + and format_guess != ['%Y'] + ): return None output_format = [] diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 9588f54388d1e..fcfca5a27763b 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -147,6 +147,7 @@ def test_parsers_month_freq(date_str, expected): [ ("20111230", "%Y%m%d"), ("2011-12-30", "%Y-%m-%d"), + ("2011", "%Y"), ("30-12-2011", "%d-%m-%Y"), ("2011-12-30 00:00:00", "%Y-%m-%d %H:%M:%S"), ("2011-12-30T00:00:00", "%Y-%m-%dT%H:%M:%S"), @@ -208,7 +209,6 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt): @pytest.mark.parametrize( "invalid_dt", [ - "2013", "01/2013", "12:00:00", "1/1/1/1",