Skip to content

Commit 0e2a0cd

Browse files
committed
add issue number and try except
1 parent 54c2491 commit 0e2a0cd

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

pandas/_libs/tslibs/strptime.pyx

+19-19
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def array_strptime(ndarray[object] values, object fmt,
292292
break
293293
elif parse_code == 19:
294294
z = found_dict['z']
295-
gmtoff, gmtoff_fraction = _parse_timezone_directive(z)
295+
gmtoff, gmtoff_fraction = parse_timezone_directive(z)
296296

297297
# If we know the wk of the year and what day of that wk, we can figure
298298
# out the Julian day of the year.
@@ -656,7 +656,7 @@ cdef _calc_julian_from_U_or_W(int year, int week_of_year,
656656
days_to_week = week_0_length + (7 * (week_of_year - 1))
657657
return 1 + days_to_week + day_of_week
658658

659-
cdef _parse_timezone_directive(object z):
659+
cdef parse_timezone_directive(object z):
660660
"""
661661
Parse the '%z' directive and return an offset from UTC
662662
@@ -674,22 +674,23 @@ cdef _parse_timezone_directive(object z):
674674
object gmtoff_remainder, gmtoff_remainder_padding
675675

676676
if z == 'Z':
677-
gmtoff = 0
678-
gmtoff_fraction = 0
677+
return (0, 0)
679678
else:
680-
if z[3] == ':':
681-
z = z[:3] + z[4:]
682-
if len(z) > 5:
683-
if z[5] != ':':
684-
msg = "Inconsistent use of : in {0}"
685-
raise ValueError(msg.format(z))
686-
z = z[:5] + z[6:]
687-
hours = int(z[1:3])
688-
minutes = int(z[3:5])
689-
seconds = int(z[5:7] or 0)
690-
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
691-
gmtoff_remainder = z[8:]
692-
679+
try:
680+
if z[3] == ':':
681+
z = z[:3] + z[4:]
682+
if len(z) > 5:
683+
if z[5] != ':':
684+
msg = "Inconsistent use of : in {0}"
685+
raise ValueError(msg.format(z))
686+
z = z[:5] + z[6:]
687+
hours = int(z[1:3])
688+
minutes = int(z[3:5])
689+
seconds = int(z[5:7] or 0)
690+
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
691+
gmtoff_remainder = z[8:]
692+
except ValueError:
693+
raise ValueError("Could not parse offset: {0}".format(z))
693694
# Pad to always return microseconds.
694695
pad_number = 6 - len(gmtoff_remainder)
695696
gmtoff_remainder_padding = "0" * pad_number
@@ -698,5 +699,4 @@ cdef _parse_timezone_directive(object z):
698699
if z.startswith("-"):
699700
gmtoff = -gmtoff
700701
gmtoff_fraction = -gmtoff_fraction
701-
702-
return (gmtoff, gmtoff_fraction)
702+
return (gmtoff, gmtoff_fraction)

pandas/tests/indexes/datetimes/test_tools.py

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def test_to_datetime_format_weeks(self, cache):
213213
def test_to_datetime_parse_tzname_or_tzoffset(self, box, const,
214214
assert_equal, fmt,
215215
dates, expected_dates):
216+
# GH 13486
216217
# %z or %Z parsing
217218
result = pd.to_datetime(dates, format=fmt, box=box)
218219
expected = const(expected_dates)
@@ -238,6 +239,7 @@ def test_to_datetime_parse_tzname_or_tzoffset(self, box, const,
238239
def test_to_datetime_parse_tzname_and_tzoffset(self, box, const,
239240
assert_equal, dates,
240241
expected_dates):
242+
# GH 13486
241243
# %z and %Z parsing
242244
fmt = '%Y-%m-%d %H:%M:%S %Z %z'
243245
result = pd.to_datetime(dates, format=fmt, box=box)

0 commit comments

Comments
 (0)