From 90eb28575b78362f0a67933fc973089579116840 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 3 Aug 2019 13:24:57 -0700 Subject: [PATCH 1/4] BUG: fix to_datetime(dti, utc=True) --- pandas/core/tools/datetimes.py | 3 +++ pandas/tests/indexes/datetimes/test_tools.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 172084e97a959..bc6b91b5b4412 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -334,6 +334,9 @@ def _convert_listlike_datetimes( return DatetimeIndex(arg, tz=tz, name=name) except ValueError: pass + elif box and tz: + # DatetimeArray, DatetimeIndex + return arg.tz_localize(tz) return arg diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index 10d422e8aa52c..cda6a27918748 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -1623,6 +1623,16 @@ def test_dayfirst(self, cache): tm.assert_index_equal(expected, idx5) tm.assert_index_equal(expected, idx6) + def test_to_datetime_dta_tz(self): + dti = date_range("2015-04-05", periods=3).rename("foo") + expected = dti.tz_localize("UTC") + result = to_datetime(dti, utc=True) + tm.assert_index_equal(result, expected) + + dta = dti._data + result = to_datetime(dta, utc=True) + tm.assert_equal(result, expected._data) + class TestGuessDatetimeFormat: @td.skip_if_not_us_locale From 624437e87ed3372727dab6b9881c89f98f6b6b83 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 4 Aug 2019 08:54:17 -0700 Subject: [PATCH 2/4] whatsnew, suggested edits --- doc/source/whatsnew/v0.25.1.rst | 2 +- pandas/core/tools/datetimes.py | 2 +- pandas/tests/indexes/datetimes/test_tools.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index c80195af413f7..588bd2a40e935 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -31,7 +31,7 @@ Categorical Datetimelike ^^^^^^^^^^^^ - +- Bug in :func:`to_datetime` where passing a timezone-naive :class:`DatetimeArray` or :class:`DatetimeIndex` and utc=True would incorrectly return a timezone-naive result (:issue:`27733`) - - - diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index bc6b91b5b4412..b07647cf5b5fb 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -334,7 +334,7 @@ def _convert_listlike_datetimes( return DatetimeIndex(arg, tz=tz, name=name) except ValueError: pass - elif box and tz: + elif tz: # DatetimeArray, DatetimeIndex return arg.tz_localize(tz) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index cda6a27918748..272df6e15f5eb 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -1629,7 +1629,7 @@ def test_to_datetime_dta_tz(self): result = to_datetime(dti, utc=True) tm.assert_index_equal(result, expected) - dta = dti._data + dta = dti.array result = to_datetime(dta, utc=True) tm.assert_equal(result, expected._data) From 2226635da470d052ecf36daf15e8dd22c369ae95 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 4 Aug 2019 16:33:07 -0700 Subject: [PATCH 3/4] parametrize --- pandas/tests/indexes/datetimes/test_tools.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index 272df6e15f5eb..23540041a3d70 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -1623,15 +1623,17 @@ def test_dayfirst(self, cache): tm.assert_index_equal(expected, idx5) tm.assert_index_equal(expected, idx6) - def test_to_datetime_dta_tz(self): + @pytest.mark.parametrize("klass", [DatetimeIndex, DatetimeArray]) + def test_to_datetime_dta_tz(self, klass): + # GH#27733 dti = date_range("2015-04-05", periods=3).rename("foo") expected = dti.tz_localize("UTC") - result = to_datetime(dti, utc=True) - tm.assert_index_equal(result, expected) - dta = dti.array - result = to_datetime(dta, utc=True) - tm.assert_equal(result, expected._data) + obj = klass(dti) + expected = klass(expected) + + result = to_datetime(obj, utc=True) + tm.assert_equal(result, expected) class TestGuessDatetimeFormat: From b0d8fbcf56214904105033f8e75634faaa950704 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 4 Aug 2019 16:33:55 -0700 Subject: [PATCH 4/4] backquotes --- doc/source/whatsnew/v0.25.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index cf8658f05d623..51307d6771559 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -31,7 +31,7 @@ Categorical Datetimelike ^^^^^^^^^^^^ -- Bug in :func:`to_datetime` where passing a timezone-naive :class:`DatetimeArray` or :class:`DatetimeIndex` and utc=True would incorrectly return a timezone-naive result (:issue:`27733`) +- Bug in :func:`to_datetime` where passing a timezone-naive :class:`DatetimeArray` or :class:`DatetimeIndex` and ``utc=True`` would incorrectly return a timezone-naive result (:issue:`27733`) - - -