Skip to content

BUG: Fix to_dict() problem when using datetime DataFrame #11247 #11327

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

Closed
wants to merge 1 commit into from
Closed
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/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ Bug Fixes
- Bugs in ``to_excel`` with duplicate columns (:issue:`11007`, :issue:`10982`, :issue:`10970`)
- Fixed a bug that prevented the construction of an empty series of dtype
``datetime64[ns, tz]`` (:issue:`11245`).
- Bug in ``DataFrame.to_dict()`` produces an datetime object instead of Timestamp when only datetime is present in data (:issue:`11327`)
7 changes: 4 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,12 @@ def to_dict(self, orient='dict'):
elif orient.lower().startswith('sp'):
return {'index': self.index.tolist(),
'columns': self.columns.tolist(),
'data': self.values.tolist()}
'data': lib.map_infer(self.values.ravel(), _maybe_box_datetimelike)
.reshape(self.values.shape).tolist()}
elif orient.lower().startswith('s'):
return dict((k, v) for k, v in compat.iteritems(self))
return dict((k, _maybe_box_datetimelike(v)) for k, v in compat.iteritems(self))
elif orient.lower().startswith('r'):
return [dict((k, v) for k, v in zip(self.columns, row))
return [dict((k, _maybe_box_datetimelike(v)) for k, v in zip(self.columns, row))
for row in self.values]
elif orient.lower().startswith('i'):
return dict((k, v.to_dict()) for k, v in self.iterrows())
Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4728,6 +4728,55 @@ def test_to_dict(self):
for k2, v2 in compat.iteritems(v):
self.assertEqual(v2, recons_data[k2][k])

def test_to_dict_timestamp(self):
# GH11247
tsmp = Timestamp('20130101')
test_data = DataFrame({'A': [tsmp, tsmp], 'B': [tsmp, tsmp]})
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make this a mixed frame ,e.g. add a float and a string say. obviously need to adjust the tests as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I adjusted the tests and now it tests both mixed and single dtypes

test_data_mixed = DataFrame({'A': [tsmp, tsmp], 'B': [1, 2]})

expected_records = [{'A': tsmp, 'B': tsmp},
{'A': tsmp, 'B': tsmp}]
expected_records_mixed = [{'A': tsmp, 'B': 1},
{'A': tsmp, 'B': 2}]

tm.assert_almost_equal(test_data.to_dict(
orient='records'), expected_records)
tm.assert_almost_equal(test_data_mixed.to_dict(
orient='records'), expected_records_mixed)

expected_series = {
'A': Series([tsmp, tsmp]),
'B': Series([tsmp, tsmp]),
}
expected_series_mixed = {
'A': Series([tsmp, tsmp]),
'B': Series([1, 2]),
}

tm.assert_almost_equal(test_data.to_dict(
orient='series'), expected_series)
tm.assert_almost_equal(test_data_mixed.to_dict(
orient='series'), expected_series_mixed)

expected_split = {
'index': [0, 1],
'data': [[tsmp, tsmp],
[tsmp, tsmp]],
'columns': ['A', 'B']
}
expected_split_mixed = {
'index': [0, 1],
'data': [[tsmp, 1],
[tsmp, 2]],
'columns': ['A', 'B']
}

tm.assert_almost_equal(test_data.to_dict(
orient='split'), expected_split)
tm.assert_almost_equal(test_data_mixed.to_dict(
orient='split'), expected_split_mixed)


def test_to_dict_invalid_orient(self):
df = DataFrame({'A':[0, 1]})
self.assertRaises(ValueError, df.to_dict, orient='xinvalid')
Expand Down