Skip to content

BUG: retain tz in to_records #32535

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
6 changes: 4 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,9 @@ def to_records(
else:
ix_vals = [self.index.values]

arrays = ix_vals + [self[c]._internal_get_values() for c in self.columns]
arrays = ix_vals + [
np.asarray(self.iloc[:, i]) for i in range(len(self.columns))
]

count = 0
index_names = list(self.index.names)
Expand All @@ -1786,7 +1788,7 @@ def to_records(

names = [str(name) for name in itertools.chain(index_names, self.columns)]
else:
arrays = [self[c]._internal_get_values() for c in self.columns]
arrays = [np.asarray(self.iloc[:, i]) for i in range(len(self.columns))]
names = [str(c) for c in self.columns]
index_names = []

Expand Down
19 changes: 18 additions & 1 deletion pandas/tests/frame/methods/test_to_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import numpy as np
import pytest

from pandas import CategoricalDtype, DataFrame, MultiIndex, Series, date_range
from pandas import (
CategoricalDtype,
DataFrame,
MultiIndex,
Series,
Timestamp,
date_range,
)
import pandas._testing as tm


Expand All @@ -18,6 +25,16 @@ def test_to_records_dt64(self):
result = df.to_records()["index"][0]
assert expected == result

def test_to_records_dt64tz_column(self):
df = DataFrame({"A": date_range("2012-01-01", "2012-01-02", tz="US/Eastern")})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can add the issue numbe here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated+green


result = df.to_records()

assert result.dtype["A"] == object
val = result[0][1]
assert isinstance(val, Timestamp)
assert val == df.loc[0, "A"]

def test_to_records_with_multindex(self):
# GH#3189
index = [
Expand Down