-
-
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
Conversation
Hello @Cheukting! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on March 13, 2018 at 19:21 Hours UTC |
pandas/core/frame.py
Outdated
('col2', OrderedDict([('a', 0.5), ('b', 0.75)]))]) | ||
|
||
If you want a `defaultdict`, you need to initialize it: | ||
|
||
>>> 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}), |
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.
I think that having the output directly below the first >
is correct. Not a big deal thoughl.
Please change PR's title to: "DOC: update the pandas.DataFrame.to_dict docstring" |
8aefd21
to
fe60a54
Compare
pandas/core/frame.py
Outdated
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']) |
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.
,'col2'
-> , 'col2'
fe60a54
to
a4121e7
Compare
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.
Small comments. Looks good
In the list of 'orient' options, can you also quote the options? (like 'dict' (default)
(to make clear it is a string)
pandas/core/frame.py
Outdated
|
||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, couple of minor things.
pandas/core/frame.py
Outdated
@@ -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 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.
pandas/core/frame.py
Outdated
See Also | ||
-------- | ||
DataFrame.from_dict: create a DataFrame from a dictionary | ||
|
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.
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.
pandas/core/frame.py
Outdated
|
||
Method converting the DataFrame to a Python dictionary, | ||
the type of the key-value pairs can be customized with | ||
the parameters (see below). |
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.
pandas/core/frame.py
Outdated
- 'list' : dict like {column -> [values]} | ||
- 'series' : dict like {column -> Series(values)} | ||
- 'split' : dict like | ||
{index' -> [index], columns -> [columns], data -> [values]} |
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.
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)
Grammar.
Cleaned up the example output formatting a tad. Thanks @Cheukting ! |
Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):
scripts/validate_docstrings.py <your-function-or-method>
git diff upstream/master -u -- "*.py" | flake8 --diff
python doc/make.py --single <your-function-or-method>
Please include the output of the validation script below between the "```" ticks:
If the validation script still gives errors, but you think there is a good reason
to deviate in this case (and there are certainly such cases), please state this
explicitly.