-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 1 commit
a4121e7
dd2d78e
a9fa2f2
9a05746
f86ea83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -924,7 +924,12 @@ 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). | ||
|
||
Parameters | ||
---------- | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
-------- | ||
from_dict: create a DataFrame from a dictionary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from_dict -> DataFrame.from_dict |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing if it'd make sense to also add |
||
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 | ||
|
@@ -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') | ||
|
@@ -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 " | ||
|
There was a problem hiding this comment.
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.