Skip to content

Commit 9e89eb1

Browse files
mroeschkePingviinituutti
authored andcommitted
BUG: to_datetime(strs, utc=True) used previous UTC offset (pandas-dev#25020)
1 parent e6ef079 commit 9e89eb1

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Timedelta
103103
Timezones
104104
^^^^^^^^^
105105

106-
-
106+
- Bug in :func:`to_datetime` with ``utc=True`` and datetime strings that would apply previously parsed UTC offsets to subsequent arguments (:issue:`24992`)
107107
-
108108
-
109109

pandas/_libs/tslib.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,8 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
645645
out_tzoffset_vals.add(out_tzoffset * 60.)
646646
tz = pytz.FixedOffset(out_tzoffset)
647647
value = tz_convert_single(value, tz, UTC)
648+
out_local = 0
649+
out_tzoffset = 0
648650
else:
649651
# Add a marker for naive string, to track if we are
650652
# parsing mixed naive and aware strings

pandas/tests/indexes/datetimes/test_tools.py

+23
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,29 @@ def test_iso_8601_strings_with_different_offsets(self):
714714
NaT], tz='UTC')
715715
tm.assert_index_equal(result, expected)
716716

717+
def test_iss8601_strings_mixed_offsets_with_naive(self):
718+
# GH 24992
719+
result = pd.to_datetime([
720+
'2018-11-28T00:00:00',
721+
'2018-11-28T00:00:00+12:00',
722+
'2018-11-28T00:00:00',
723+
'2018-11-28T00:00:00+06:00',
724+
'2018-11-28T00:00:00'
725+
], utc=True)
726+
expected = pd.to_datetime([
727+
'2018-11-28T00:00:00',
728+
'2018-11-27T12:00:00',
729+
'2018-11-28T00:00:00',
730+
'2018-11-27T18:00:00',
731+
'2018-11-28T00:00:00'
732+
], utc=True)
733+
tm.assert_index_equal(result, expected)
734+
735+
items = ['2018-11-28T00:00:00+12:00', '2018-11-28T00:00:00']
736+
result = pd.to_datetime(items, utc=True)
737+
expected = pd.to_datetime(list(reversed(items)), utc=True)[::-1]
738+
tm.assert_index_equal(result, expected)
739+
717740
def test_non_iso_strings_with_tz_offset(self):
718741
result = to_datetime(['March 1, 2018 12:00:00+0400'] * 2)
719742
expected = DatetimeIndex([datetime(2018, 3, 1, 12,

0 commit comments

Comments
 (0)