Skip to content
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

BUG: Fix to_datetime to properly deal with tz offsets #3944 #5958

Merged
1 commit merged into from
Feb 4, 2014
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
23 changes: 23 additions & 0 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ def test_coerce_of_invalid_datetimes(self):
)
)

def test_parsing_timezone_offsets(self):
# All of these datetime strings with offsets are equivalent
# to the same datetime after the timezone offset is added
dt_strings = [
'01-01-2013 08:00:00+08:00',
'2013-01-01T08:00:00.000000000+0800',
'2012-12-31T16:00:00.000000000-0800',
'12-31-2012 23:00:00-01:00',
]

expected_output = tslib.array_to_datetime(
np.array(['01-01-2013 00:00:00'], dtype=object)
)

for dt_string in dt_strings:
self.assert_(
np.array_equal(
tslib.array_to_datetime(
np.array([dt_string], dtype=object)
),
expected_output
)
)

class TestTimestampNsOperations(tm.TestCase):
def setUp(self):
Expand Down
10 changes: 4 additions & 6 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
format=None, utc=None, coerce=False, unit=None):
cdef:
Py_ssize_t i, n = len(values)
object val
object val, py_dt
ndarray[int64_t] iresult
ndarray[object] oresult
pandas_datetimestruct dts
Expand Down Expand Up @@ -1085,18 +1085,16 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
_check_dts_bounds(&dts)
except ValueError:
try:
iresult[i] = _pydatetime_to_dts(
parse_datetime_string(val, dayfirst=dayfirst),
&dts
)
py_dt = parse_datetime_string(val, dayfirst=dayfirst)
except Exception:
if coerce:
iresult[i] = iNaT
continue
raise TypeError

try:
_check_dts_bounds(&dts)
_ts = convert_to_tsobject(py_dt, None, None)
iresult[i] = _ts.value
except ValueError:
if coerce:
iresult[i] = iNaT
Expand Down