Skip to content

Commit 8deae52

Browse files
authored
BUG: DataFrame.from_records with tzaware (#51162)
1 parent b161fa7 commit 8deae52

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ Datetimelike
11311131
- Bug in :func:`to_datetime` with unit of "Y" or "M" giving incorrect results, not matching pointwise :class:`Timestamp` results (:issue:`50870`)
11321132
- Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with datetime or timedelta dtypes incorrectly raising ``ValueError`` (:issue:`11312`)
11331133
- Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`)
1134+
- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`51162`)
11341135
-
11351136

11361137
Timedelta

pandas/core/internals/construction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,13 +764,13 @@ def to_arrays(
764764
# see test_from_records_with_index_data, test_from_records_bad_index_column
765765
if columns is not None:
766766
arrays = [
767-
data._ixs(i, axis=1).values
767+
data._ixs(i, axis=1)._values
768768
for i, col in enumerate(data.columns)
769769
if col in columns
770770
]
771771
else:
772772
columns = data.columns
773-
arrays = [data._ixs(i, axis=1).values for i in range(len(columns))]
773+
arrays = [data._ixs(i, axis=1)._values for i in range(len(columns))]
774774

775775
return arrays, columns
776776

pandas/tests/frame/constructors/test_from_records.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@
1515
Interval,
1616
RangeIndex,
1717
Series,
18+
date_range,
1819
)
1920
import pandas._testing as tm
2021

2122

2223
class TestFromRecords:
24+
def test_from_records_dt64tz_frame(self):
25+
# GH#51162 don't lose tz when calling from_records with DataFrame input
26+
dti = date_range("2016-01-01", periods=10, tz="US/Pacific")
27+
df = DataFrame({i: dti for i in range(4)})
28+
res = DataFrame.from_records(df)
29+
tm.assert_frame_equal(res, df)
30+
2331
def test_from_records_with_datetimes(self):
2432

2533
# this may fail on certain platforms because of a numpy issue

0 commit comments

Comments
 (0)