Skip to content

ENH: Add 'records' outtype to to_dict method. #4936

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
merged 1 commit into from
Sep 30, 2013
Merged
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pandas 0.13
- Added a more informative error message when plot arguments contain
overlapping color and style arguments (:issue:`4402`)
- Significant table writing performance improvements in ``HDFStore``
- ``to_dict`` now takes ``records`` as a possible outtype. Returns an array
of column-keyed dictionaries. (:pullrequest:`4936`)
Copy link
Member

Choose a reason for hiding this comment

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

use :issue:4936`` instead of pullrequest


**API Changes**

Expand Down
2 changes: 2 additions & 0 deletions doc/source/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Enhancements
- Clipboard functionality now works with PySide (:issue:`4282`)
- Added a more informative error message when plot arguments contain
overlapping color and style arguments (:issue:`4402`)
- ``to_dict`` now takes ``records`` as a possible outtype. Returns an array
of column-keyed dictionaries. (:pullrequest:`4936`)
Copy link
Member

Choose a reason for hiding this comment

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

same for pullrequest here


Bug Fixes
~~~~~~~~~
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,11 +962,11 @@ def to_dict(self, outtype='dict'):

Parameters
----------
outtype : str {'dict', 'list', 'series'}
outtype : str {'dict', 'list', 'series', 'records'}
Determines the type of the values of the dictionary. The
default `dict` is a nested dictionary {column -> {index -> value}}.
`list` returns {column -> list(values)}. `series` returns
{column -> Series(values)}.
{column -> Series(values)}. `records` returns [{columns -> value}].
Abbreviations are allowed.


Expand All @@ -983,6 +983,9 @@ def to_dict(self, outtype='dict'):
return dict((k, v.tolist()) for k, v in compat.iteritems(self))
elif outtype.lower().startswith('s'):
return dict((k, v) for k, v in compat.iteritems(self))
elif outtype.lower().startswith('r'):
return [dict((k, v) for k, v in zip(self.columns, row)) \
Copy link
Member

Choose a reason for hiding this comment

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

you don't need the backslash here

for row in self.values]
else: # pragma: no cover
raise ValueError("outtype %s not understood" % outtype)

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3396,6 +3396,14 @@ def test_to_dict(self):
for k2, v2 in compat.iteritems(v):
self.assertEqual(v2, recons_data[k][k2])

recons_data = DataFrame(test_data).to_dict("r")

expected_records = [{'A': 1.0, 'B': '1'},
{'A': 2.0, 'B': '2'},
{'A': nan, 'B': '3'}]

tm.assert_almost_equal(recons_data, expected_records)

def test_to_records_dt64(self):
df = DataFrame([["one", "two", "three"],
["four", "five", "six"]],
Expand Down