From 0e2a0cd0f59f66a914658f3ca853da65e9c4c354 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 18 Mar 2018 18:33:29 -0700 Subject: [PATCH] add issue number and try except --- pandas/_libs/tslibs/strptime.pyx | 38 ++++++++++---------- pandas/tests/indexes/datetimes/test_tools.py | 2 ++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index 0c75c5472172c..d21205a332c81 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -292,7 +292,7 @@ def array_strptime(ndarray[object] values, object fmt, break elif parse_code == 19: z = found_dict['z'] - gmtoff, gmtoff_fraction = _parse_timezone_directive(z) + gmtoff, gmtoff_fraction = parse_timezone_directive(z) # If we know the wk of the year and what day of that wk, we can figure # out the Julian day of the year. @@ -656,7 +656,7 @@ cdef _calc_julian_from_U_or_W(int year, int week_of_year, days_to_week = week_0_length + (7 * (week_of_year - 1)) return 1 + days_to_week + day_of_week -cdef _parse_timezone_directive(object z): +cdef parse_timezone_directive(object z): """ Parse the '%z' directive and return an offset from UTC @@ -674,22 +674,23 @@ cdef _parse_timezone_directive(object z): object gmtoff_remainder, gmtoff_remainder_padding if z == 'Z': - gmtoff = 0 - gmtoff_fraction = 0 + return (0, 0) else: - if z[3] == ':': - z = z[:3] + z[4:] - if len(z) > 5: - if z[5] != ':': - msg = "Inconsistent use of : in {0}" - raise ValueError(msg.format(z)) - z = z[:5] + z[6:] - hours = int(z[1:3]) - minutes = int(z[3:5]) - seconds = int(z[5:7] or 0) - gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds - gmtoff_remainder = z[8:] - + try: + if z[3] == ':': + z = z[:3] + z[4:] + if len(z) > 5: + if z[5] != ':': + msg = "Inconsistent use of : in {0}" + raise ValueError(msg.format(z)) + z = z[:5] + z[6:] + hours = int(z[1:3]) + minutes = int(z[3:5]) + seconds = int(z[5:7] or 0) + gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds + gmtoff_remainder = z[8:] + except ValueError: + raise ValueError("Could not parse offset: {0}".format(z)) # Pad to always return microseconds. pad_number = 6 - len(gmtoff_remainder) gmtoff_remainder_padding = "0" * pad_number @@ -698,5 +699,4 @@ cdef _parse_timezone_directive(object z): if z.startswith("-"): gmtoff = -gmtoff gmtoff_fraction = -gmtoff_fraction - - return (gmtoff, gmtoff_fraction) + return (gmtoff, gmtoff_fraction) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index f425590bcff4a..7b5dbfaa798c5 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -213,6 +213,7 @@ def test_to_datetime_format_weeks(self, cache): def test_to_datetime_parse_tzname_or_tzoffset(self, box, const, assert_equal, fmt, dates, expected_dates): + # GH 13486 # %z or %Z parsing result = pd.to_datetime(dates, format=fmt, box=box) expected = const(expected_dates) @@ -238,6 +239,7 @@ def test_to_datetime_parse_tzname_or_tzoffset(self, box, const, def test_to_datetime_parse_tzname_and_tzoffset(self, box, const, assert_equal, dates, expected_dates): + # GH 13486 # %z and %Z parsing fmt = '%Y-%m-%d %H:%M:%S %Z %z' result = pd.to_datetime(dates, format=fmt, box=box)