Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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",
Expand Down