Skip to content

BUG: guess_datetime_format doesn't guess just year #49127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2022
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