From bfd1c435b35044f861e54b8ab3b8a71aee50fcc5 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Tue, 29 Jan 2019 23:03:35 -0800 Subject: [PATCH 1/2] BUG: to_datetime(..., utc=True) used previous UTC offset --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/_libs/tslib.pyx | 2 ++ pandas/tests/indexes/datetimes/test_tools.py | 23 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index d0ddb6e09d555..1b46994334abf 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -96,7 +96,7 @@ Timedelta Timezones ^^^^^^^^^ -- +- Bug in :func:`to_datetime` with ``utc=True` and datetime strings that would apply previously parsed UTC offsets to subsequent arguments (:issue:`24992`) - - diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 798e338d5581b..f932e236b5218 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -645,6 +645,8 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise', out_tzoffset_vals.add(out_tzoffset * 60.) tz = pytz.FixedOffset(out_tzoffset) value = tz_convert_single(value, tz, UTC) + out_local = 0 + out_tzoffset = 0 else: # Add a marker for naive string, to track if we are # parsing mixed naive and aware strings diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index 38f5eab15041f..b94935d2521eb 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -714,6 +714,29 @@ def test_iso_8601_strings_with_different_offsets(self): NaT], tz='UTC') tm.assert_index_equal(result, expected) + def test_iss8601_strings_mixed_offsets_with_naive(self): + # GH 24992 + result = pd.to_datetime([ + '2018-11-28T00:00:00', + '2018-11-28T00:00:00+12:00', + '2018-11-28T00:00:00', + '2018-11-28T00:00:00+06:00', + '2018-11-28T00:00:00' + ], utc=True) + expected = pd.to_datetime([ + '2018-11-28T00:00:00', + '2018-11-27T12:00:00', + '2018-11-28T00:00:00', + '2018-11-27T18:00:00', + '2018-11-28T00:00:00' + ], utc=True) + tm.assert_index_equal(result, expected) + + items = ['2018-11-28T00:00:00+12:00', '2018-11-28T00:00:00'] + result = pd.to_datetime(items, utc=True) + expected = pd.to_datetime(list(reversed(items)), utc=True)[::-1] + tm.assert_index_equal(result, expected) + def test_non_iso_strings_with_tz_offset(self): result = to_datetime(['March 1, 2018 12:00:00+0400'] * 2) expected = DatetimeIndex([datetime(2018, 3, 1, 12, From df719905678d84e6b6dfdd825e66f6be1004c466 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Wed, 30 Jan 2019 10:11:56 -0800 Subject: [PATCH 2/2] Add missing tick --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 1b46994334abf..a81d7598f4cf4 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -96,7 +96,7 @@ Timedelta Timezones ^^^^^^^^^ -- Bug in :func:`to_datetime` with ``utc=True` and datetime strings that would apply previously parsed UTC offsets to subsequent arguments (:issue:`24992`) +- Bug in :func:`to_datetime` with ``utc=True`` and datetime strings that would apply previously parsed UTC offsets to subsequent arguments (:issue:`24992`) - -