Skip to content

Commit 4603c63

Browse files
authored
BUG: retain tz in to_records (pandas-dev#32535)
* BUG: retain tz in to_records * whatsnew * woops * comment
1 parent 810a4e5 commit 4603c63

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ Other
388388
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
389389
- Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`)
390390
- Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`)
391+
- Bug in :meth:`DataFrame.to_records` incorrectly losing timezone information in timezone-aware ``datetime64`` columns (:issue:`32535`)
391392

392393
.. ---------------------------------------------------------------------------
393394

pandas/core/frame.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,9 @@ def to_records(
17731773
else:
17741774
ix_vals = [self.index.values]
17751775

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

17781780
count = 0
17791781
index_names = list(self.index.names)
@@ -1788,7 +1790,7 @@ def to_records(
17881790

17891791
names = [str(name) for name in itertools.chain(index_names, self.columns)]
17901792
else:
1791-
arrays = [self[c]._internal_get_values() for c in self.columns]
1793+
arrays = [np.asarray(self.iloc[:, i]) for i in range(len(self.columns))]
17921794
names = [str(c) for c in self.columns]
17931795
index_names = []
17941796

pandas/tests/frame/methods/test_to_records.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
import numpy as np
44
import pytest
55

6-
from pandas import CategoricalDtype, DataFrame, MultiIndex, Series, date_range
6+
from pandas import (
7+
CategoricalDtype,
8+
DataFrame,
9+
MultiIndex,
10+
Series,
11+
Timestamp,
12+
date_range,
13+
)
714
import pandas._testing as tm
815

916

@@ -18,6 +25,17 @@ def test_to_records_dt64(self):
1825
result = df.to_records()["index"][0]
1926
assert expected == result
2027

28+
def test_to_records_dt64tz_column(self):
29+
# GH#32535 dont less tz in to_records
30+
df = DataFrame({"A": date_range("2012-01-01", "2012-01-02", tz="US/Eastern")})
31+
32+
result = df.to_records()
33+
34+
assert result.dtype["A"] == object
35+
val = result[0][1]
36+
assert isinstance(val, Timestamp)
37+
assert val == df.loc[0, "A"]
38+
2139
def test_to_records_with_multindex(self):
2240
# GH#3189
2341
index = [

0 commit comments

Comments
 (0)