Skip to content

BUG: parsing ISO8601 string with format= and timezone name fails #49747

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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ Timedelta
Timezones
^^^^^^^^^
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` with object-dtype containing multiple timezone-aware ``datetime`` objects with heterogeneous timezones to a :class:`DatetimeTZDtype` incorrectly raising (:issue:`32581`)
- Bug in :func:`to_datetime` was failing to parse date strings with timezone name when ``format`` was specified with ``%Z`` (:issue:`49748`)
-

Numeric
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def format_is_iso(f: str) -> bint:

for date_sep in [' ', '/', '\\', '-', '.', '']:
for time_sep in [' ', 'T']:
for micro_or_tz in ['', '%z', '%Z', '.%f', '.%f%z', '.%f%Z']:
for micro_or_tz in ['', '%z', '.%f', '.%f%z']:
if (iso_template(date_sep=date_sep,
time_sep=time_sep,
micro_or_tz=micro_or_tz,
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/src/datetime/np_datetime_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,

/* UTC specifier */
if (*substr == 'Z') {
if (compare_format(&format, &format_len, "%Z", 2, exact)) {
if (compare_format(&format, &format_len, "%z", 2, exact)) {
goto parse_error;
}
/* "Z" should be equivalent to tz offset "+00:00" */
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,6 @@ def test_to_datetime_iso8601(self, cache, arg, exp_str):
("2012-01-01 10", "%Y-%m-%d %H:%M"),
("2012-01-01 10:00", "%Y-%m-%d %H:%M:%S"),
("2012-01-01 10:00:00", "%Y-%m-%d %H:%M:%S.%f"),
("2012-01-01 10:00:00.123", "%Y-%m-%d %H:%M:%S.%f%Z"),
Copy link
Member Author

Choose a reason for hiding this comment

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

this one no longer goes down the ISO8601 fastpath, so removing it from here

("2012-01-01 10:00:00.123", "%Y-%m-%d %H:%M:%S.%f%z"),
(0, "%Y-%m-%d"),
],
Expand Down Expand Up @@ -1861,7 +1860,7 @@ def test_to_datetime_iso8601_valid(self, input, format):
[
("2020-01-01T00:00:00.000000000+00:00", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2020-01-01T00:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z"),
("2020-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%Z"),
("2020-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
],
)
def test_to_datetime_iso8601_with_timezone_valid(self, input, format):
Expand Down Expand Up @@ -1916,6 +1915,12 @@ def test_to_datetime_with_apply(self, cache):
result = td.apply(to_datetime, format="%b %y", cache=cache)
tm.assert_series_equal(result, expected)

def test_to_datetime_timezone_name(self):
# https://github.com/pandas-dev/pandas/issues/49748
result = to_datetime("2020-01-01 00:00:00UTC", format="%Y-%m-%d %H:%M:%S%Z")
expected = Timestamp(2020, 1, 1).tz_localize("UTC")
assert result == expected

@td.skip_if_not_us_locale
def test_to_datetime_with_apply_with_empty_str(self, cache):
# this is only locale tested with US/None locales
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ def test_parse_time_string_check_instance_type_raise_exception():
("%Y%m%d %H:%M:%S", True),
("%Y-%m-%dT%H:%M:%S", True),
("%Y-%m-%dT%H:%M:%S%z", True),
("%Y-%m-%dT%H:%M:%S%Z", True),
("%Y-%m-%dT%H:%M:%S%Z", False),
Copy link
Member Author

Choose a reason for hiding this comment

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

these are no longer detected as ISO because the pandas ISO8601 fastpath can't handle them

("%Y-%m-%dT%H:%M:%S.%f", True),
("%Y-%m-%dT%H:%M:%S.%f%z", True),
("%Y-%m-%dT%H:%M:%S.%f%Z", True),
("%Y-%m-%dT%H:%M:%S.%f%Z", False),
("%Y%m%d", False),
("%Y%m", False),
("%Y", False),
Expand Down