Skip to content

ENH Guess %Y-%m format #49389

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 1 commit into from
Oct 29, 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
5 changes: 3 additions & 2 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1011,10 +1011,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
break

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

Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ def test_parsers_month_freq(date_str, expected):
("20111230", "%Y%m%d"),
("2011-12-30", "%Y-%m-%d"),
("2011", "%Y"),
("2011-01", "%Y-%m"),
("2011/01", "%Y/%m"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is ISO 8601 conformant or is it just a byproduct of the implementation? Don't have the actual spec just looking at the Wikipedia. Didn't see anything for this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not strictly ISO8601 (which wants a hyphen), but in pandas it's still treated as if it were, so long as the separator is consistent

def format_is_iso(f: str) -> bint:
"""
Does format match the iso8601 set that can be handled by the C parser?
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
but must be consistent. Leading 0s in dates and times are optional.
"""
iso_template = '%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}'.format
excluded_formats = ['%Y%m%d', '%Y%m', '%Y']
for date_sep in [' ', '/', '\\', '-', '.', '']:

char valid_ymd_sep[] = {'-', '.', '/', '\\', ' '};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting...might be worth reconsidering but definitely out of scope for this PR

("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 @@ -215,6 +217,7 @@ def test_guess_datetime_format_with_locale_specific_formats(string, fmt):
"this_is_not_a_datetime",
"51a",
"13/2019",
"202001", # YYYYMM isn't ISO8601
],
)
def test_guess_datetime_format_invalid_inputs(invalid_dt):
Expand Down