From 02033d45b681d602f18b2c01b7429356e18e7186 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Sun, 16 Oct 2022 09:38:02 +0100 Subject: [PATCH 1/2] guess %Y format --- pandas/_libs/tslibs/parsing.pyx | 8 ++++++-- pandas/tests/tslibs/test_parsing.py | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) 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..642162cb8a864 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"), From 5b50d8c559d2b103209f9871d7fe0168baab28f6 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Sun, 16 Oct 2022 09:55:10 +0100 Subject: [PATCH 2/2] fixup --- pandas/tests/tslibs/test_parsing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 642162cb8a864..fcfca5a27763b 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -209,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",