Skip to content

BUG guess_Datetime_format doesn't guess 27.03.2003 14:55:00.000 #50319

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
Dec 22, 2022

Conversation

MarcoGorelli
Copy link
Member

Comment on lines 1019 to 1025
if re.search(r"\d*\.\d+", token) is None:
# For example: 98
token_filled = token.zfill(padding)
else:
# For example: 00.123
seconds, nanoseconds = token.split(".")
seconds = f"{int(seconds):02d}"
Copy link
Member Author

@MarcoGorelli MarcoGorelli Dec 17, 2022

Choose a reason for hiding this comment

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

previously,

seconds = f"{int(seconds):02d}"

was failing if token was just a single character '.'

If we make the check more precise, then we avoid that issue

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM. Doesn't need a whatsnew right?

@@ -1016,9 +1016,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:

cdef str _fill_token(token: str, padding: int):
cdef str token_filled
if "." not in token:
if re.search(r"\d*\.\d+", token) is None:
Copy link
Member

Choose a reason for hiding this comment

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

is the leading \d* needed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

\d+ would be better actually, thanks

This is for when it corresponds with the %S.%f directive

@MarcoGorelli
Copy link
Member Author

LGTM. Doesn't need a whatsnew right?

thanks! and no, this wouldn't (yet) make a user-facing difference

it fixes #2586 by accident, but I'd say that that fits into the PDEP4-related part of the whatsnew (as in, PDEP4 should have fixed that, it just didn't quite because of an error in implementation here)

@mroeschke mroeschke added this to the 2.0 milestone Dec 18, 2022
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM. Merge when ready @jbrockmendel

token_filled = token.zfill(padding)
else:
# For example: 00.123
seconds, nanoseconds = token.split(".")
Copy link
Member

Choose a reason for hiding this comment

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

is there a risk that this raises if there is a "." but not the regex?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup, you'd get

In [6]: token = '.'

In [7]: seconds, nanoseconds = token.split(".")

In [8]: seconds = f"{int(seconds):02d}"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [8], line 1
----> 1 seconds = f"{int(seconds):02d}"

ValueError: invalid literal for int() with base 10: ''

which is what happens in the string in the linked issue ('27.03.2003 14:55:00.000')

Copy link
Member

Choose a reason for hiding this comment

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

i take it this is caught elsewhere so isnt a problem?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, if the padding was 2 (say) then '.'.zfill(2) would give '0.', and in

token_filled = _fill_token(tokens[i], padding)
if token_format is None and token_filled == parsed_formatted:

it wouldn't match parsed_formatted

@MarcoGorelli
Copy link
Member Author

merging as there's been an approval, and this is quite minor, and it unblocks another PR - if there's any other concerns / feedback I can address them in a follow-up

@MarcoGorelli MarcoGorelli merged commit cc478c4 into pandas-dev:main Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG guess_Datetime_format doesn't guess 27.03.2003 14:55:00.000
3 participants