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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

# For example: 98
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

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

# right-pad so we get nanoseconds, then only take
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def test_parsers_month_freq(date_str, expected):
("2011-12-30 00:00:00.000000", "%Y-%m-%d %H:%M:%S.%f"),
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %H:%M:%S %p"),
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %H:%M:%S %p"),
("27.03.2003 14:55:00.000", "%d.%m.%Y %H:%M:%S.%f"), # GH50317
],
)
def test_guess_datetime_format_with_parseable_formats(string, fmt):
Expand Down