From 110e9d96c583a1523a406a63aeedb2f884eec80f Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Wed, 20 Mar 2019 16:56:12 -0700 Subject: [PATCH] BUG: DatetimeIndex.to_frame does not drop timezone information --- doc/source/whatsnew/v0.25.0.rst | 1 + pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/common.py | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 2ed2c21ba5584..a33552119fc01 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -208,6 +208,7 @@ Timedelta Timezones ^^^^^^^^^ +- Bug in :func:`DatetimeIndex.to_frame` where timezone aware data would be converted to timezone naive data (:issue:`25809`) - 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:`Timestamp.tz_localize` and :func:`Timestamp.tz_convert` does not propagate ``freq`` (:issue:`25241`) - Bug in :func:`Series.at` where setting :class:`Timestamp` with timezone raises ``TypeError`` (:issue:`25506`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f6d7d27eca598..a3eed90ab5d80 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1208,7 +1208,7 @@ def to_frame(self, index=True, name=None): from pandas import DataFrame if name is None: name = self.name or 0 - result = DataFrame({name: self.values.copy()}) + result = DataFrame({name: self._values.copy()}) if index: result.index = self diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 6d29c147c4a4a..4c013de8f8021 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -85,6 +85,14 @@ def test_to_frame(self, name): df = idx.to_frame(index=False, name=idx_name) assert df.index is not idx + def test_to_frame_datetime_tz(self): + # GH 25809 + idx = pd.date_range(start='2019-01-01', end='2019-01-30', freq='D') + idx = idx.tz_localize('UTC') + result = idx.to_frame() + expected = pd.DataFrame(idx, index=idx) + tm.assert_frame_equal(result, expected) + def test_shift(self): # GH8083 test the base class for shift