Skip to content

Commit b97a99e

Browse files
committed
Merge branch 'to_dict_records' of https://github.com/tshauck/pandas into tshauck-to_dict_records
Conflicts: doc/source/release.rst
2 parents c8ab2dd + ca6475d commit b97a99e

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ Improvements to existing features
156156
that cannot be concatenated (:issue:`4608`).
157157
- Add ``halflife`` option to exponentially weighted moving functions (PR
158158
:issue:`4998`)
159+
- ``to_dict`` now takes ``records`` as a possible outtype. Returns an array
160+
of column-keyed dictionaries. (:pullrequest:`4936`)
159161

160162
API Changes
161163
~~~~~~~~~~~

doc/source/v0.13.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ Enhancements
306306
- Clipboard functionality now works with PySide (:issue:`4282`)
307307
- Added a more informative error message when plot arguments contain
308308
overlapping color and style arguments (:issue:`4402`)
309+
- ``to_dict`` now takes ``records`` as a possible outtype. Returns an array
310+
of column-keyed dictionaries. (:pullrequest:`4936`)
309311

310312
- NaN handing in get_dummies (:issue:`4446`) with `dummy_na`
311313

pandas/core/frame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,11 @@ def to_dict(self, outtype='dict'):
644644
645645
Parameters
646646
----------
647-
outtype : str {'dict', 'list', 'series'}
647+
outtype : str {'dict', 'list', 'series', 'records'}
648648
Determines the type of the values of the dictionary. The
649649
default `dict` is a nested dictionary {column -> {index -> value}}.
650650
`list` returns {column -> list(values)}. `series` returns
651-
{column -> Series(values)}.
651+
{column -> Series(values)}. `records` returns [{columns -> value}].
652652
Abbreviations are allowed.
653653
654654
@@ -665,6 +665,9 @@ def to_dict(self, outtype='dict'):
665665
return dict((k, v.tolist()) for k, v in compat.iteritems(self))
666666
elif outtype.lower().startswith('s'):
667667
return dict((k, v) for k, v in compat.iteritems(self))
668+
elif outtype.lower().startswith('r'):
669+
return [dict((k, v) for k, v in zip(self.columns, row)) \
670+
for row in self.values]
668671
else: # pragma: no cover
669672
raise ValueError("outtype %s not understood" % outtype)
670673

pandas/tests/test_frame.py

+8
Original file line numberDiff line numberDiff line change
@@ -3633,6 +3633,14 @@ def test_to_dict(self):
36333633
for k2, v2 in compat.iteritems(v):
36343634
self.assertEqual(v2, recons_data[k][k2])
36353635

3636+
recons_data = DataFrame(test_data).to_dict("r")
3637+
3638+
expected_records = [{'A': 1.0, 'B': '1'},
3639+
{'A': 2.0, 'B': '2'},
3640+
{'A': nan, 'B': '3'}]
3641+
3642+
tm.assert_almost_equal(recons_data, expected_records)
3643+
36363644
def test_to_records_dt64(self):
36373645
df = DataFrame([["one", "two", "three"],
36383646
["four", "five", "six"]],

0 commit comments

Comments
 (0)