Skip to content

Commit 998e030

Browse files
committed
BUG: Fix to_datetime to properly deal with tz offsets pandas-dev#3944
Currently for certain formats of datetime strings, the tz offset will just be ignored.
1 parent e2a612a commit 998e030

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
@@ -995,7 +995,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
995995
format=None, utc=None, coerce=False, unit=None):
996996
cdef:
997997
Py_ssize_t i, n = len(values)
998-
object val
998+
object val, py_dt
999999
ndarray[int64_t] iresult
10001000
ndarray[object] oresult
10011001
pandas_datetimestruct dts
@@ -1085,18 +1085,16 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10851085
_check_dts_bounds(&dts)
10861086
except ValueError:
10871087
try:
1088-
iresult[i] = _pydatetime_to_dts(
1089-
parse_datetime_string(val, dayfirst=dayfirst),
1090-
&dts
1091-
)
1088+
py_dt = parse_datetime_string(val, dayfirst=dayfirst)
10921089
except Exception:
10931090
if coerce:
10941091
iresult[i] = iNaT
10951092
continue
10961093
raise TypeError
10971094

10981095
try:
1099-
_check_dts_bounds(&dts)
1096+
_ts = convert_to_tsobject(py_dt, None, None)
1097+
iresult[i] = _ts.value
11001098
except ValueError:
11011099
if coerce:
11021100
iresult[i] = iNaT

0 commit comments

Comments
 (0)