Skip to content

BUG: DataFrame.from_records with tzaware #51162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down