Skip to content

BUG: guess_datetime_format fails to infer timeformat #57471

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 5 commits into from
Feb 21, 2024

Conversation

yuanx749
Copy link
Contributor

@yuanx749
Copy link
Contributor Author

Explanation of the fix:

The previous code below failed for date time str of the kind like "2023-11-09T20:23:46".

In L1006, parsed_formatted is parsed to the same "2023" for both "%H%M" and "%Y", then in L1010 format_guess[0] becomes "%H%M", rather than the correct "%Y". Noticing the order in datetime_attrs_to_format, I thus move "%Y" before "%H%M" to fix the bug.

for attrs, attr_format, padding in datetime_attrs_to_format:
# If a given attribute has been placed in the format string, skip
# over other formats for that same underlying attribute (IE, month
# can be represented in multiple different ways)
if set(attrs) & found_attrs:
continue
if parsed_datetime.tzinfo is None and attr_format in ("%Z", "%z"):
continue
parsed_formatted = parsed_datetime.strftime(attr_format)
for i, token_format in enumerate(format_guess):
token_filled = _fill_token(tokens[i], padding)
if token_format is None and token_filled == parsed_formatted:
format_guess[i] = attr_format
tokens[i] = token_filled
found_attrs.update(attrs)
break

@yuanx749 yuanx749 marked this pull request as ready for review February 17, 2024 15:57
@MarcoGorelli
Copy link
Member

it's been a while since I looked at this function :) taking a look today, thanks for the fix

@MarcoGorelli MarcoGorelli added this to the 3.0 milestone Feb 21, 2024
@MarcoGorelli MarcoGorelli added the Datetime Datetime data dtype label Feb 21, 2024
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks @yuanx749 !

@MarcoGorelli MarcoGorelli merged commit 563c677 into pandas-dev:main Feb 21, 2024
@yuanx749 yuanx749 deleted the guess_datetime_format branch February 22, 2024 04:06
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: to_datetime fails to infer timeformat depending on hour of day
2 participants