diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 802e2e6a488d0..3d0a502bf408d 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:`51162`) - 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..60cb0f4490705 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): + # 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) + tm.assert_frame_equal(res, df) + def test_from_records_with_datetimes(self): # this may fail on certain platforms because of a numpy issue