Skip to content

DOC: update the docstring of pandas.DataFrame.to_dict #20162

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 5 commits into from
Mar 13, 2018
Merged
Changes from 3 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
41 changes: 25 additions & 16 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,21 +924,26 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None):
return cls(data, index=index, columns=columns, dtype=dtype)

def to_dict(self, orient='dict', into=dict):
"""Convert DataFrame to dictionary.
"""
Convert DataFrame to dictionary.

Method converting the DataFrame to a Python dictionary,
the type of the key-value pairs can be customized with
the parameters (see below).
Copy link
Member

Choose a reason for hiding this comment

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

A bit repetitive, I wouldn't repeat that it converts to a dictionary again, and directly explain that you can customize.


Parameters
----------
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
Determines the type of the values of the dictionary.

- dict (default) : dict like {column -> {index -> value}}
- list : dict like {column -> [values]}
- series : dict like {column -> Series(values)}
- split : dict like
{index -> [index], columns -> [columns], data -> [values]}
- records : list like
- 'dict' (default) : dict like {column -> {index -> value}}
- 'list' : dict like {column -> [values]}
- 'series' : dict like {column -> Series(values)}
- 'split' : dict like
{index' -> [index], columns -> [columns], data -> [values]}
Copy link
Member

Choose a reason for hiding this comment

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

Can you on this line do {'index' -> [index], 'columns' -> [columns], 'data' -> [values]} (as in this case those are actual strings and not a placeholder for what is in the data)

- 'records' : list like
[{column -> value}, ... , {column -> value}]
- index : dict like {index -> {column -> value}}
- 'index' : dict like {index -> {column -> value}}

Abbreviations are allowed. `s` indicates `series` and `sp`
indicates `split`.
Expand All @@ -949,16 +954,21 @@ def to_dict(self, orient='dict', into=dict):
instance of the mapping type you want. If you want a
collections.defaultdict, you must pass it initialized.

.. versionadded:: 0.21.0
.. versionadded:: 0.21.0.
Copy link
Member

Choose a reason for hiding this comment

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

The validation script is wrong, you don't need the final dot here.


Returns
-------
result : collections.Mapping like {column -> {index -> value}}

See Also
--------
DataFrame.from_dict: create a DataFrame from a dictionary

Copy link
Member

Choose a reason for hiding this comment

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

I'm guessing if it'd make sense to also add to_json here. I think users checking this method can be looking for that.

Examples
--------
>>> df = pd.DataFrame(
{'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
>>> df = pd.DataFrame({'col1': [1, 2],
... 'col2': [0.5, 0.75]},
... index=['a', 'b'])
>>> df
col1 col2
a 1 0.50
Expand All @@ -975,9 +985,8 @@ def to_dict(self, orient='dict', into=dict):
b 0.75
Name: col2, dtype: float64}
>>> df.to_dict('split')
{'columns': ['col1', 'col2'],
'data': [[1.0, 0.5], [2.0, 0.75]],
'index': ['a', 'b']}
{'index': ['a', 'b'], 'columns': ['col1', 'col2'],
'data': [[1.0, 0.5], [2.0, 0.75]]}
>>> df.to_dict('records')
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
>>> df.to_dict('index')
Expand All @@ -994,8 +1003,8 @@ def to_dict(self, orient='dict', into=dict):

>>> dd = defaultdict(list)
>>> df.to_dict('records', into=dd)
[defaultdict(<type 'list'>, {'col2': 0.5, 'col1': 1.0}),
defaultdict(<type 'list'>, {'col2': 0.75, 'col1': 2.0})]
[defaultdict(<class 'list'>, {'col1': 1.0, 'col2': 0.5}),
defaultdict(<class 'list'>, {'col1': 2.0, 'col2': 0.75})]
"""
if not self.columns.is_unique:
warnings.warn("DataFrame columns are not unique, some "
Expand Down