Skip to content

Commit 1af1f6c

Browse files
committed
[BUG-FIX] DataFrame created with tzinfo cannot use to_dict(orient="records")
Columns with datetimez are not returning arrays. Closes #18372
1 parent c4a2cd3 commit 1af1f6c

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Bug Fixes
108108
Conversion
109109
^^^^^^^^^^
110110

111-
-
111+
- Bug in :func:`DataFrame.to_dict` where columns of datetimez where not arrays when used with ``orient='records'`` (:issue:`18372`)
112112
-
113113
-
114114

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ def to_dict(self, orient='dict', into=dict):
993993
for k, v in compat.iteritems(self))
994994
elif orient.lower().startswith('r'):
995995
return [into_c((k, _maybe_box_datetimelike(v))
996-
for k, v in zip(self.columns, row))
996+
for k, v in zip(self.columns, np.atleast_1d(row)))
997997
for row in self.values]
998998
elif orient.lower().startswith('i'):
999999
return into_c((k, v.to_dict(into)) for k, v in self.iterrows())

pandas/tests/tseries/test_timezones.py

+11
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,17 @@ def test_frame_reset_index(self):
882882
rs = roundtripped.index.tz
883883
assert xp == rs
884884

885+
def test_frame_to_dict_tz(self):
886+
data = [(datetime(2017, 11, 18, 21, 53, 0, 219225, tzinfo=pytz.utc),),
887+
(datetime(2017, 11, 18, 22, 6, 30, 61810, tzinfo=pytz.utc,),)]
888+
df = DataFrame(list(data), columns=["datetime", ])
889+
890+
rec = df.to_dict(orient='records')
891+
t1 = Timestamp('2017-11-18 21:53:00.219225+0000', tz=pytz.utc)
892+
t2 = Timestamp('2017-11-18 22:06:30.061810+0000', tz=pytz.utc)
893+
tm.assert_dict_equal(rec[0], {'data': t1})
894+
tm.assert_dict_equal(rec[1], {'data': t2})
895+
885896
def test_dateutil_tzoffset_support(self):
886897
values = [188.5, 328.25]
887898
tzinfo = tzoffset(None, 7200)

0 commit comments

Comments
 (0)