Skip to content

BUG: guess_datetime_format doesn't guess format correctly for UTC+1 #48954

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
Oct 5, 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
10 changes: 9 additions & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ from pandas._libs.tslibs.np_datetime cimport (
string_to_dts,
)
from pandas._libs.tslibs.offsets cimport is_offset_object
from pandas._libs.tslibs.strptime import array_strptime
from pandas._libs.tslibs.util cimport (
get_c_string_buf_and_size,
is_array,
Expand Down Expand Up @@ -958,7 +959,9 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:

Returns
-------
ret : datetime format string (for `strftime` or `strptime`)
str or None : ret
datetime format string (for `strftime` or `strptime`),
or None if it can't be guessed.
"""

if not isinstance(dt_str, str):
Expand Down Expand Up @@ -1079,6 +1082,11 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:

guessed_format = ''.join(output_format)

try:
array_strptime(np.asarray([dt_str], dtype=object), guessed_format)
except ValueError:
# Doesn't parse, so this can't be the correct format.
return None
# rebuild string, capturing any inferred padding
dt_str = ''.join(tokens)
if parsed_datetime.strftime(guessed_format) == dt_str:
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,25 @@ def test_parsers_month_freq(date_str, expected):
("2011-12-30T00:00:00", "%Y-%m-%dT%H:%M:%S"),
("2011-12-30T00:00:00UTC", "%Y-%m-%dT%H:%M:%S%Z"),
("2011-12-30T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+9", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+09", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+9", None),
("2011-12-30T00:00:00+09", None),
("2011-12-30T00:00:00+090", None),
("2011-12-30T00:00:00+0900", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00-0900", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+09:00", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+09:000", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+9:0", "%Y-%m-%dT%H:%M:%S%z"),
("2011-12-30T00:00:00+09:000", None),
("2011-12-30T00:00:00+9:0", None),
("2011-12-30T00:00:00+09:", None),
("2011-12-30T00:00:00.000000UTC", "%Y-%m-%dT%H:%M:%S.%f%Z"),
("2011-12-30T00:00:00.000000Z", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+9", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+09", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+9", None),
("2011-12-30T00:00:00.000000+09", None),
("2011-12-30T00:00:00.000000+090", None),
("2011-12-30T00:00:00.000000+0900", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000-0900", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+09:00", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+09:000", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+9:0", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+09:000", None),
("2011-12-30T00:00:00.000000+9:0", None),
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment in the test to note that odd +9 format shouldn't be supported by %z but dateutil.parse can support this format?

("2011-12-30T00:00:00.000000+09:", None),
("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"),
Expand Down