Skip to content

Commit a37b78d

Browse files
authored
BUG: parsing ISO8601 string with format= and timezone name fails (#49747)
* %z -> %Z * add whatsnew note and regression test Co-authored-by: MarcoGorelli <>
1 parent 961005a commit a37b78d

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ Timedelta
629629
Timezones
630630
^^^^^^^^^
631631
- 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`)
632+
- Bug in :func:`to_datetime` was failing to parse date strings with timezone name when ``format`` was specified with ``%Z`` (:issue:`49748`)
632633
-
633634

634635
Numeric

pandas/_libs/tslibs/parsing.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ def format_is_iso(f: str) -> bint:
894894

895895
for date_sep in [' ', '/', '\\', '-', '.', '']:
896896
for time_sep in [' ', 'T']:
897-
for micro_or_tz in ['', '%z', '%Z', '.%f', '.%f%z', '.%f%Z']:
897+
for micro_or_tz in ['', '%z', '.%f', '.%f%z']:
898898
if (iso_template(date_sep=date_sep,
899899
time_sep=time_sep,
900900
micro_or_tz=micro_or_tz,

pandas/_libs/tslibs/src/datetime/np_datetime_strings.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
539539

540540
/* UTC specifier */
541541
if (*substr == 'Z') {
542-
if (compare_format(&format, &format_len, "%Z", 2, exact)) {
542+
if (compare_format(&format, &format_len, "%z", 2, exact)) {
543543
goto parse_error;
544544
}
545545
/* "Z" should be equivalent to tz offset "+00:00" */

pandas/tests/tools/test_to_datetime.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,6 @@ def test_to_datetime_iso8601(self, cache, arg, exp_str):
17551755
("2012-01-01 10", "%Y-%m-%d %H:%M"),
17561756
("2012-01-01 10:00", "%Y-%m-%d %H:%M:%S"),
17571757
("2012-01-01 10:00:00", "%Y-%m-%d %H:%M:%S.%f"),
1758-
("2012-01-01 10:00:00.123", "%Y-%m-%d %H:%M:%S.%f%Z"),
17591758
("2012-01-01 10:00:00.123", "%Y-%m-%d %H:%M:%S.%f%z"),
17601759
(0, "%Y-%m-%d"),
17611760
],
@@ -1861,7 +1860,7 @@ def test_to_datetime_iso8601_valid(self, input, format):
18611860
[
18621861
("2020-01-01T00:00:00.000000000+00:00", "%Y-%m-%dT%H:%M:%S.%f%z"),
18631862
("2020-01-01T00:00:00+00:00", "%Y-%m-%dT%H:%M:%S%z"),
1864-
("2020-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%Z"),
1863+
("2020-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z"),
18651864
],
18661865
)
18671866
def test_to_datetime_iso8601_with_timezone_valid(self, input, format):
@@ -1916,6 +1915,12 @@ def test_to_datetime_with_apply(self, cache):
19161915
result = td.apply(to_datetime, format="%b %y", cache=cache)
19171916
tm.assert_series_equal(result, expected)
19181917

1918+
def test_to_datetime_timezone_name(self):
1919+
# https://github.com/pandas-dev/pandas/issues/49748
1920+
result = to_datetime("2020-01-01 00:00:00UTC", format="%Y-%m-%d %H:%M:%S%Z")
1921+
expected = Timestamp(2020, 1, 1).tz_localize("UTC")
1922+
assert result == expected
1923+
19191924
@td.skip_if_not_us_locale
19201925
def test_to_datetime_with_apply_with_empty_str(self, cache):
19211926
# this is only locale tested with US/None locales

pandas/tests/tslibs/test_parsing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ def test_parse_time_string_check_instance_type_raise_exception():
284284
("%Y%m%d %H:%M:%S", True),
285285
("%Y-%m-%dT%H:%M:%S", True),
286286
("%Y-%m-%dT%H:%M:%S%z", True),
287-
("%Y-%m-%dT%H:%M:%S%Z", True),
287+
("%Y-%m-%dT%H:%M:%S%Z", False),
288288
("%Y-%m-%dT%H:%M:%S.%f", True),
289289
("%Y-%m-%dT%H:%M:%S.%f%z", True),
290-
("%Y-%m-%dT%H:%M:%S.%f%Z", True),
290+
("%Y-%m-%dT%H:%M:%S.%f%Z", False),
291291
("%Y%m%d", False),
292292
("%Y%m", False),
293293
("%Y", False),

0 commit comments

Comments
 (0)