Skip to content

Commit

Permalink
BUG: guess_datetime_format doesn't guess just year (pandas-dev#49127)
Browse files Browse the repository at this point in the history
* guess %Y format

* fixup

Co-authored-by: MarcoGorelli <>
  • Loading branch information
MarcoGorelli authored and noatamir committed Nov 9, 2022
1 parent 1d19e81 commit 5139f44
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
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

0 comments on commit 5139f44

Please sign in to comment.