From de8d1c3d6524ec0c7689eb668135eb3202b7ef57 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 4 Feb 2023 08:04:25 -0800 Subject: [PATCH 1/2] BUG: DataFrame.from_records with tzaware --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/internals/construction.py | 4 ++-- pandas/tests/frame/constructors/test_from_records.py | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 802e2e6a488d0..f0412cfd3fa72 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1131,6 +1131,7 @@ Datetimelike - Bug in :func:`to_datetime` with unit of "Y" or "M" giving incorrect results, not matching pointwise :class:`Timestamp` results (:issue:`50870`) - Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with datetime or timedelta dtypes incorrectly raising ``ValueError`` (:issue:`11312`) - Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`) +- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`??`) - Timedelta diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 9bdfd7991689b..ce709917d7123 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -764,13 +764,13 @@ def to_arrays( # see test_from_records_with_index_data, test_from_records_bad_index_column if columns is not None: arrays = [ - data._ixs(i, axis=1).values + data._ixs(i, axis=1)._values for i, col in enumerate(data.columns) if col in columns ] else: columns = data.columns - arrays = [data._ixs(i, axis=1).values for i in range(len(columns))] + arrays = [data._ixs(i, axis=1)._values for i in range(len(columns))] return arrays, columns diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index d4427dd789b16..cccdc2947f40d 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -15,11 +15,19 @@ Interval, RangeIndex, Series, + date_range, ) import pandas._testing as tm class TestFromRecords: + def test_from_records_dt64tz_frame(self): + # don't lose tz when calling from_records with DataFrame input + dti = date_range("2016-01-01", periods=10, tz="US/Pacific") + df = DataFrame({i: dti for i in range(4)}) + res = DataFrame.from_records(df) + tm.assert_frame_equal(res, df) + def test_from_records_with_datetimes(self): # this may fail on certain platforms because of a numpy issue From 9e3301d39a3cb8979bbd5f278f2c1b2878614c6d Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 4 Feb 2023 08:05:35 -0800 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v2.0.0.rst | 2 +- pandas/tests/frame/constructors/test_from_records.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index f0412cfd3fa72..3d0a502bf408d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1131,7 +1131,7 @@ Datetimelike - Bug in :func:`to_datetime` with unit of "Y" or "M" giving incorrect results, not matching pointwise :class:`Timestamp` results (:issue:`50870`) - Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with datetime or timedelta dtypes incorrectly raising ``ValueError`` (:issue:`11312`) - Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`) -- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`??`) +- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`51162`) - Timedelta diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index cccdc2947f40d..60cb0f4490705 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -22,7 +22,7 @@ class TestFromRecords: def test_from_records_dt64tz_frame(self): - # don't lose tz when calling from_records with DataFrame input + # GH#51162 don't lose tz when calling from_records with DataFrame input dti = date_range("2016-01-01", periods=10, tz="US/Pacific") df = DataFrame({i: dti for i in range(4)}) res = DataFrame.from_records(df)