Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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: 0 additions & 1 deletion doc/source/whatsnew/v0.23.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Bug Fixes
**Groupby/Resample/Rolling**

- Bug in :meth:`DataFrame.resample` when resampling ``NaT`` in ``TimeDeltaIndex`` (:issue:`13223`).
-

**Missing**

Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,8 @@ MultiIndex
I/O
^^^

- Bug in :meth:`DataFrame.to_dict` when the resulting dict contains non-Python scalars in the case of numeric data (:issue:`23753`)

.. _whatsnew_0240.bug_fixes.nan_with_str_dtype:

Proper handling of `np.NaN` in a string data-typed column with the Python engine
Expand Down
14 changes: 8 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,16 +1220,18 @@ def to_dict(self, orient='dict', into=dict):
elif orient.lower().startswith('sp'):
return into_c((('index', self.index.tolist()),
('columns', self.columns.tolist()),
('data', lib.map_infer(self.values.ravel(),
com.maybe_box_datetimelike)
.reshape(self.values.shape).tolist())))
('data', [
list(map(com.maybe_box_datetimelike, t))
for t in self.itertuples(index=False)]
)))
elif orient.lower().startswith('s'):
return into_c((k, com.maybe_box_datetimelike(v))
for k, v in compat.iteritems(self))
elif orient.lower().startswith('r'):
return [into_c((k, com.maybe_box_datetimelike(v))
for k, v in zip(self.columns, np.atleast_1d(row)))
for row in self.values]
return [
into_c((k, com.maybe_box_datetimelike(v))
for k, v in compat.iteritems(row._asdict()))
for row in self.itertuples(index=False)]
elif orient.lower().startswith('i'):
if not self.index.is_unique:
raise ValueError(
Expand Down
25 changes: 14 additions & 11 deletions pandas/tests/frame/test_convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_to_records_index_name(self):
def test_to_records_with_unicode_index(self):
# GH13172
# unicode_literals conflict with to_records
result = DataFrame([{u'a': u'x', u'b': 'y'}]).set_index(u'a')\
result = DataFrame([{u'a': u'x', u'b': 'y'}]).set_index(u'a') \
.to_records()
expected = np.rec.array([('x', 'y')], dtype=[('a', 'O'), ('b', 'O')])
tm.assert_almost_equal(result, expected)
Expand Down Expand Up @@ -281,17 +281,20 @@ def test_to_records_datetimeindex_with_tz(self, tz):
# both converted to UTC, so they are equal
tm.assert_numpy_array_equal(result, expected)

def test_to_dict_box_scalars(self):
# 14216
@pytest.mark.parametrize('orient,item_getter',
[('dict', lambda d, col, idx: d[col][idx]),
('records', lambda d, col, idx: d[idx][col]),
('list', lambda d, col, idx: d[col][idx]),
('split', lambda d, col, idx: d['data'][idx][d['columns'].index(col)]),
('index', lambda d, col, idx: d[idx][col])
])
def test_to_dict_box_scalars(self, orient, item_getter):
# 14216, 23753
# make sure that we are boxing properly
d = {'a': [1], 'b': ['b']}

result = DataFrame(d).to_dict()
assert isinstance(list(result['a'])[0], (int, long))
assert isinstance(list(result['b'])[0], (int, long))

result = DataFrame(d).to_dict(orient='records')
assert isinstance(result[0]['a'], (int, long))
df = DataFrame({'a': [1, 2], 'b': [.1, .2]})
result = df.to_dict(orient=orient)
assert isinstance(item_getter(result, 'a', 0), (int, long))
assert isinstance(item_getter(result, 'b', 0), float)

def test_frame_to_dict_tz(self):
# GH18372 When converting to dict with orient='records' columns of
Expand Down