Skip to content

Commit d3adfe5

Browse files
author
MarcoGorelli
committed
guess %Y-%m format
1 parent 22417cf commit d3adfe5

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pandas/_libs/tslibs/parsing.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
10111011
break
10121012

10131013
# Only consider it a valid guess if we have a year, month and day,
1014-
# unless it's %Y which is both common and unambiguous.
1014+
# unless it's %Y or %Y-%m which conform with ISO8601. Note that we don't
1015+
# make an exception for %Y%m because it's explicitly not considered ISO8601.
10151016
if (
10161017
len({'year', 'month', 'day'} & found_attrs) != 3
1017-
and format_guess != ['%Y']
1018+
and format_guess not in (['%Y'], ['%Y', None, '%m'])
10181019
):
10191020
return None
10201021

pandas/tests/tslibs/test_parsing.py

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def test_parsers_month_freq(date_str, expected):
148148
("20111230", "%Y%m%d"),
149149
("2011-12-30", "%Y-%m-%d"),
150150
("2011", "%Y"),
151+
("2011-01", "%Y-%m"),
151152
("30-12-2011", "%d-%m-%Y"),
152153
("2011-12-30 00:00:00", "%Y-%m-%d %H:%M:%S"),
153154
("2011-12-30T00:00:00", "%Y-%m-%dT%H:%M:%S"),
@@ -215,6 +216,7 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt):
215216
"this_is_not_a_datetime",
216217
"51a",
217218
"13/2019",
219+
"202001", # YYYYMM isn't ISO8601
218220
],
219221
)
220222
def test_guess_datetime_format_invalid_inputs(invalid_dt):

0 commit comments

Comments
 (0)