Skip to content

Commit 8a60158

Browse files
author
y-p
committed
Merge pull request #5958 from danbirken/fix-to-datetime-timezone-offset
BUG: Fix to_datetime to properly deal with tz offsets #3944
2 parents 554c717 + 998e030 commit 8a60158

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pandas/tseries/tests/test_tslib.py

+23
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ def test_coerce_of_invalid_datetimes(self):
189189
)
190190
)
191191

192+
def test_parsing_timezone_offsets(self):
193+
# All of these datetime strings with offsets are equivalent
194+
# to the same datetime after the timezone offset is added
195+
dt_strings = [
196+
'01-01-2013 08:00:00+08:00',
197+
'2013-01-01T08:00:00.000000000+0800',
198+
'2012-12-31T16:00:00.000000000-0800',
199+
'12-31-2012 23:00:00-01:00',
200+
]
201+
202+
expected_output = tslib.array_to_datetime(
203+
np.array(['01-01-2013 00:00:00'], dtype=object)
204+
)
205+
206+
for dt_string in dt_strings:
207+
self.assert_(
208+
np.array_equal(
209+
tslib.array_to_datetime(
210+
np.array([dt_string], dtype=object)
211+
),
212+
expected_output
213+
)
214+
)
192215

193216
class TestTimestampNsOperations(tm.TestCase):
194217
def setUp(self):

pandas/tslib.pyx

+4-6
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10011001
format=None, utc=None, coerce=False, unit=None):
10021002
cdef:
10031003
Py_ssize_t i, n = len(values)
1004-
object val
1004+
object val, py_dt
10051005
ndarray[int64_t] iresult
10061006
ndarray[object] oresult
10071007
pandas_datetimestruct dts
@@ -1096,18 +1096,16 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10961096
_check_dts_bounds(&dts)
10971097
except ValueError:
10981098
try:
1099-
iresult[i] = _pydatetime_to_dts(
1100-
parse_datetime_string(val, dayfirst=dayfirst),
1101-
&dts
1102-
)
1099+
py_dt = parse_datetime_string(val, dayfirst=dayfirst)
11031100
except Exception:
11041101
if coerce:
11051102
iresult[i] = iNaT
11061103
continue
11071104
raise TypeError
11081105

11091106
try:
1110-
_check_dts_bounds(&dts)
1107+
_ts = convert_to_tsobject(py_dt, None, None)
1108+
iresult[i] = _ts.value
11111109
except ValueError:
11121110
if coerce:
11131111
iresult[i] = iNaT

0 commit comments

Comments
 (0)