Skip to content

Commit 634196a

Browse files
authored
ENH: dont guess %Y/%m (#49711)
1 parent 8d8d3cf commit 634196a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pandas/_libs/tslibs/parsing.pyx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,15 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
10131013
found_attrs.update(attrs)
10141014
break
10151015

1016-
# Only consider it a valid guess if we have a year, month and day,
1017-
# unless it's %Y or %Y-%m which conform with ISO8601. Note that we don't
1018-
# make an exception for %Y%m because it's explicitly not considered ISO8601.
1016+
# Only consider it a valid guess if we have a year, month and day.
1017+
# We make exceptions for %Y and %Y-%m (only with the `-` separator)
1018+
# as they conform with ISO8601.
10191019
if (
10201020
len({'year', 'month', 'day'} & found_attrs) != 3
1021-
and format_guess not in (['%Y'], ['%Y', None, '%m'])
1021+
and format_guess != ['%Y']
1022+
and not (
1023+
format_guess == ['%Y', None, '%m'] and tokens[1] == '-'
1024+
)
10221025
):
10231026
return None
10241027

pandas/tests/tslibs/test_parsing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def test_parsers_month_freq(date_str, expected):
149149
("2011-12-30", "%Y-%m-%d"),
150150
("2011", "%Y"),
151151
("2011-01", "%Y-%m"),
152-
("2011/01", "%Y/%m"),
153152
("30-12-2011", "%d-%m-%Y"),
154153
("2011-12-30 00:00:00", "%Y-%m-%d %H:%M:%S"),
155154
("2011-12-30T00:00:00", "%Y-%m-%dT%H:%M:%S"),
@@ -218,6 +217,7 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt):
218217
"51a",
219218
"13/2019",
220219
"202001", # YYYYMM isn't ISO8601
220+
"2020/01", # YYYY/MM isn't ISO8601 either
221221
],
222222
)
223223
def test_guess_datetime_format_invalid_inputs(invalid_dt):

0 commit comments

Comments
 (0)