Skip to content

Commit d6c0deb

Browse files
committed
ENH: Provide dict object for to_dict() pandas-dev#16122
1 parent 086c598 commit d6c0deb

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

pandas/core/frame.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -894,14 +894,10 @@ def to_dict(self, orient='dict', into=dict):
894894
Returns
895895
-------
896896
result : collections.Mapping like {column -> {index -> value}}
897-
If ``into`` is collections.defaultdict, the return
898-
value's default_factory will be None.
899897
900898
Examples
901899
--------
902-
>>> from pandas import DataFrame
903-
>>> from collections import OrderedDict, defaultdict
904-
>>> df = DataFrame(
900+
>>> df = pd.DataFrame(
905901
{'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
906902
>>> df
907903
col1 col2
@@ -928,7 +924,7 @@ def to_dict(self, orient='dict', into=dict):
928924
{'a': {'col1': 1.0, 'col2': 0.5}, 'b': {'col1': 2.0, 'col2': 0.75}}
929925
930926
You can also specify the mapping type.
931-
927+
>>> from collections import OrderedDict, defaultdict
932928
>>> df.to_dict(into=OrderedDict)
933929
OrderedDict([('col1', OrderedDict([('a', 1), ('b', 2)])),
934930
('col2', OrderedDict([('a', 0.5), ('b', 0.75)]))])

pandas/core/series.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,8 @@ def tolist(self):
10771077

10781078
def to_dict(self, into=dict):
10791079
"""
1080-
Convert Series to {label -> value} dict or dict-like object
1080+
Convert Series to {label -> value} dict or dict-like object.
1081+
10811082
Parameters
10821083
----------
10831084
into : class, default dict
@@ -1096,11 +1097,10 @@ def to_dict(self, into=dict):
10961097
10971098
Examples
10981099
--------
1099-
>>> from pandas import Series
1100-
>>> from collections import OrderedDict, defaultdict
1101-
>>> s = Series([1, 2, 3, 4])
1100+
>>> s = pd.Series([1, 2, 3, 4])
11021101
>>> s.to_dict()
11031102
{0: 1, 1: 2, 2: 3, 3: 4}
1103+
>>> from collections import OrderedDict, defaultdict
11041104
>>> s.to_dict(OrderedDict)
11051105
OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
11061106
>>> dd = defaultdict(list)

0 commit comments

Comments
 (0)