Skip to content

Commit 5995dc3

Browse files
rpetchlerjreback
authored andcommitted
ENH: Add orient argument and split option to DataFrame.to_dict. (GH7840)
Update documentation with deprecation and enhancement notices. Remove indentation from list in docstring.
1 parent d65c9d7 commit 5995dc3

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

doc/source/v0.15.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ Prior Version Deprecations/Changes
767767
Deprecations
768768
~~~~~~~~~~~~
769769

770+
- The ``outtype`` argument to ``pd.DataFrame.to_dict`` has been deprecated in favor of ``orient``. (:issue:`7840`)
770771
- The ``convert_dummies`` method has been deprecated in favor of
771772
``get_dummies`` (:issue:`8140`)
772773
- The ``infer_dst`` argument in ``tz_localize`` will be deprecated in favor of
@@ -849,7 +850,7 @@ Enhancements
849850
idx
850851
idx + pd.offsets.MonthEnd(3)
851852

852-
853+
- Added ``split`` as an option to the ``orient`` argument in ``pd.DataFrame.to_dict``. (:issue:`7840`)
853854

854855
- The ``get_dummies`` method can now be used on DataFrames. By default only
855856
catagorical columns are encoded as 0's and 1's, while other columns are

pandas/core/frame.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -640,19 +640,25 @@ def from_dict(cls, data, orient='columns', dtype=None):
640640

641641
return cls(data, index=index, columns=columns, dtype=dtype)
642642

643-
def to_dict(self, outtype='dict'):
644-
"""
645-
Convert DataFrame to dictionary.
643+
@deprecate_kwarg(old_arg_name='outtype', new_arg_name='orient')
644+
def to_dict(self, orient='dict'):
645+
"""Convert DataFrame to dictionary.
646646
647647
Parameters
648648
----------
649-
outtype : str {'dict', 'list', 'series', 'records'}
650-
Determines the type of the values of the dictionary. The
651-
default `dict` is a nested dictionary {column -> {index -> value}}.
652-
`list` returns {column -> list(values)}. `series` returns
653-
{column -> Series(values)}. `records` returns [{columns -> value}].
654-
Abbreviations are allowed.
649+
orient : str {'dict', 'list', 'series', 'split', 'records'}
650+
Determines the type of the values of the dictionary.
651+
652+
- dict (default) : dict like {column -> {index -> value}}
653+
- list : dict like {column -> [values]}
654+
- series : dict like {column -> Series(values)}
655+
- split : dict like
656+
{index -> [index], columns -> [columns], data -> [values]}
657+
- records : list like
658+
[{column -> value}, ... , {column -> value}]
655659
660+
Abbreviations are allowed. `s` indicates `series` and `sp`
661+
indicates `split`.
656662
657663
Returns
658664
-------
@@ -661,13 +667,17 @@ def to_dict(self, outtype='dict'):
661667
if not self.columns.is_unique:
662668
warnings.warn("DataFrame columns are not unique, some "
663669
"columns will be omitted.", UserWarning)
664-
if outtype.lower().startswith('d'):
670+
if orient.lower().startswith('d'):
665671
return dict((k, v.to_dict()) for k, v in compat.iteritems(self))
666-
elif outtype.lower().startswith('l'):
672+
elif orient.lower().startswith('l'):
667673
return dict((k, v.tolist()) for k, v in compat.iteritems(self))
668-
elif outtype.lower().startswith('s'):
674+
elif orient.lower().startswith('sp'):
675+
return {'index': self.index.tolist(),
676+
'columns': self.columns.tolist(),
677+
'data': self.values.tolist()}
678+
elif orient.lower().startswith('s'):
669679
return dict((k, v) for k, v in compat.iteritems(self))
670-
elif outtype.lower().startswith('r'):
680+
elif orient.lower().startswith('r'):
671681
return [dict((k, v) for k, v in zip(self.columns, row))
672682
for row in self.values]
673683
else: # pragma: no cover

pandas/tests/test_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,13 @@ def test_to_dict(self):
40374037
for k2, v2 in compat.iteritems(v):
40384038
self.assertEqual(v2, recons_data[k][k2])
40394039

4040+
recons_data = DataFrame(test_data).to_dict("sp")
4041+
4042+
expected_split = {'columns': ['A', 'B'], 'index': ['1', '2', '3'],
4043+
'data': [[1.0, '1'], [2.0, '2'], [nan, '3']]}
4044+
4045+
tm.assert_almost_equal(recons_data, expected_split)
4046+
40404047
recons_data = DataFrame(test_data).to_dict("r")
40414048

40424049
expected_records = [{'A': 1.0, 'B': '1'},

0 commit comments

Comments
 (0)