From deada8858ab4231164cc7eb9cabd5515772c68ea Mon Sep 17 00:00:00 2001 From: Gabriel Araujo Date: Tue, 18 Aug 2015 14:24:47 -0300 Subject: [PATCH 1/3] ENH: Index orientation option when converting a DataFrame into a dict. Added an index orient option for DataFrame.to_dict method. Updated tests with index option. --- pandas/core/frame.py | 5 ++++- pandas/tests/test_frame.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ed46dad2846c9..85058549d9b70 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -748,7 +748,7 @@ def to_dict(self, orient='dict'): Parameters ---------- - orient : str {'dict', 'list', 'series', 'split', 'records'} + orient : str {'dict', 'list', 'series', 'split', 'records', 'index'} Determines the type of the values of the dictionary. - dict (default) : dict like {column -> {index -> value}} @@ -758,6 +758,7 @@ def to_dict(self, orient='dict'): {index -> [index], columns -> [columns], data -> [values]} - records : list like [{column -> value}, ... , {column -> value}] + - index : dict like {index -> {column -> value}} Abbreviations are allowed. `s` indicates `series` and `sp` indicates `split`. @@ -782,6 +783,8 @@ def to_dict(self, orient='dict'): elif orient.lower().startswith('r'): return [dict((k, 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()) else: raise ValueError("orient '%s' not understood" % orient) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 465f1da05ebde..aed3a27bfbefc 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -4474,9 +4474,15 @@ def test_to_dict(self): tm.assert_almost_equal(recons_data, expected_records) + recons_data = DataFrame(test_data).to_dict("i") + + for k, v in compat.iteritems(test_data): + for k2, v2 in compat.iteritems(v): + self.assertEqual(v2, recons_data[k2][k]) + def test_to_dict_invalid_orient(self): df = DataFrame({'A':[0, 1]}) - self.assertRaises(ValueError, df.to_dict, orient='invalid') + self.assertRaises(ValueError, df.to_dict, orient='xinvalid') def test_to_records_dt64(self): df = DataFrame([["one", "two", "three"], From 164cbde2b2369618b54580f04ba69e2cca9d3d23 Mon Sep 17 00:00:00 2001 From: Gabriel Araujo Date: Wed, 19 Aug 2015 20:13:22 -0300 Subject: [PATCH 2/3] DOC: Updates DataFrame.to_dict docstring. --- doc/source/whatsnew/v0.17.0.txt | 2 ++ pandas/core/frame.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 0c3af5c4bd64f..a0ab379c53571 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -174,6 +174,8 @@ Other enhancements - ``msgpack`` submodule has been updated to 0.4.6 with backward compatibility (:issue:`10581`) +- ``DataFrame.to_dict`` now accepts the *index* option in ``orient`` keyword argument. + .. ipython :: python s = pd.Series(['A', 'B', 'C', 'A', 'B', 'D']) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 85058549d9b70..57ffbc8a8ee39 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -760,6 +760,8 @@ def to_dict(self, orient='dict'): [{column -> value}, ... , {column -> value}] - index : dict like {index -> {column -> value}} + .. versionadded:: 0.17.0 + Abbreviations are allowed. `s` indicates `series` and `sp` indicates `split`. From 61ef723a37b2c2d985d8e33c90e7a273aaca5318 Mon Sep 17 00:00:00 2001 From: Gabriel Araujo Date: Thu, 20 Aug 2015 16:26:39 -0300 Subject: [PATCH 3/3] DOC: Added the issue number for reference. --- doc/source/whatsnew/v0.17.0.txt | 2 +- pandas/tests/test_frame.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index a0ab379c53571..ea116278e1eb2 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -174,7 +174,7 @@ Other enhancements - ``msgpack`` submodule has been updated to 0.4.6 with backward compatibility (:issue:`10581`) -- ``DataFrame.to_dict`` now accepts the *index* option in ``orient`` keyword argument. +- ``DataFrame.to_dict`` now accepts the *index* option in ``orient`` keyword argument (:issue:`10844`). .. ipython :: python diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index aed3a27bfbefc..1797129191162 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -4474,6 +4474,7 @@ def test_to_dict(self): tm.assert_almost_equal(recons_data, expected_records) + # GH10844 recons_data = DataFrame(test_data).to_dict("i") for k, v in compat.iteritems(test_data):