From 92fd54047dc0ac613bd58a66ef5883c5f82c0ca4 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Sat, 29 Oct 2022 13:48:05 +0100 Subject: [PATCH] guess %Y-%m format --- pandas/_libs/tslibs/parsing.pyx | 5 +++-- pandas/tests/tslibs/test_parsing.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index 469e0721f1207..1312124cfb77b 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -1011,10 +1011,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None: break # Only consider it a valid guess if we have a year, month and day, - # unless it's %Y which is both common and unambiguous. + # unless it's %Y or %Y-%m which conform with ISO8601. Note that we don't + # make an exception for %Y%m because it's explicitly not considered ISO8601. if ( len({'year', 'month', 'day'} & found_attrs) != 3 - and format_guess != ['%Y'] + and format_guess not in (['%Y'], ['%Y', None, '%m']) ): return None diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index a4e12315d34e0..f47dd2e725aec 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -148,6 +148,8 @@ def test_parsers_month_freq(date_str, expected): ("20111230", "%Y%m%d"), ("2011-12-30", "%Y-%m-%d"), ("2011", "%Y"), + ("2011-01", "%Y-%m"), + ("2011/01", "%Y/%m"), ("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"), @@ -215,6 +217,7 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt): "this_is_not_a_datetime", "51a", "13/2019", + "202001", # YYYYMM isn't ISO8601 ], ) def test_guess_datetime_format_invalid_inputs(invalid_dt):