Skip to content

DOC: Examples for DataFrame.from_records #34058

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 6 commits into from
May 12, 2020

Conversation

bdice
Copy link
Contributor

@bdice bdice commented May 7, 2020

This PR adds examples to the documentation for pandas.DataFrame.from_records. It also rewords a couple sentences to clarify that the input data can be a sequence of dicts or sequence of tuples (but not a sequence of DataFrames).

@bdice
Copy link
Contributor Author

bdice commented May 7, 2020

Validated docstring, pasting here as requested by the contributing guide.

$ python scripts/validate_docstrings.py pandas.DataFrame.from_records

################################################################################
################## Docstring (pandas.DataFrame.from_records)  ##################
################################################################################

Convert structured or record ndarray to DataFrame.

Creates a DataFrame object from a structured ndarray, sequence of
tuples, sequence of dicts, or DataFrame.

Parameters
----------
data : ndarray (structured dtype), sequence of tuples, sequence of dicts, or DataFrame
    Structured input data.
index : str, list of fields, array-like
    Field of array to use as the index, alternately a specific set of
    input labels to use.
exclude : sequence, default None
    Columns or fields to exclude.
columns : sequence, default None
    Column names to use. If the passed data do not have names
    associated with them, this argument provides names for the
    columns. Otherwise this argument indicates the order of the columns
    in the result (any names not found in the data will become all-NA
    columns).
coerce_float : bool, default False
    Attempt to convert values of non-string, non-numeric objects (like
    decimal.Decimal) to floating point, useful for SQL result sets.
nrows : int, default None
    Number of rows to read if data is an iterator.

Returns
-------
DataFrame

See Also
--------
DataFrame.from_dict : DataFrame from dict of array-like or dicts.
DataFrame : DataFrame object creation using constructor.

Examples
--------
Data can be provided as a structured ndarray:

>>> data = np.array([(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')],
...                 dtype=[('col_1', 'i4'), ('col_2', 'U1')])
>>> pd.DataFrame.from_records(data)
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

Data can be provided as a list of dicts:

>>> data = [{'col_1': 3, 'col_2': 'a'},
...         {'col_1': 2, 'col_2': 'b'},
...         {'col_1': 1, 'col_2': 'c'},
...         {'col_1': 0, 'col_2': 'd'}]
>>> pd.DataFrame.from_records(data)
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

Data can be provided as a list of tuples with corresponding columns:

>>> data = [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')]
>>> pd.DataFrame.from_records(data, columns=['col_1', 'col_2'])
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

################################################################################
################################## Validation ##################################
################################################################################

@bdice bdice changed the title Doc examples from records DOC: Examples for DataFrame.from_records May 7, 2020
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Thanks @bdice

Just suggesting a change on the param description

Replaced "sequence of tuples, sequence of dicts" with "sequence of tuples or dicts."
@bdice
Copy link
Contributor Author

bdice commented May 8, 2020

@MarcoGorelli Thanks for the feedback -- I considered that and wasn't sure if it would be clear, but I agree with your suggestion and have applied that change. That reduces the line length enough that the # noqa: E501 should no longer be necessary (85 char < 88 char).

@bdice bdice requested a review from MarcoGorelli May 8, 2020 13:00
@pep8speaks
Copy link

pep8speaks commented May 8, 2020

Hello @bdice! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-05-08 13:04:29 UTC

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@jorisvandenbossche jorisvandenbossche added this to the 1.1 milestone May 9, 2020
@simonjayhawkins
Copy link
Member

test failures unrelated.

@simonjayhawkins simonjayhawkins merged commit 9a741d3 into pandas-dev:master May 12, 2020
@simonjayhawkins
Copy link
Member

Thanks @bdice

@bdice bdice deleted the doc_examples_from_records branch May 12, 2020 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: Missing examples of from_records.
5 participants