Skip to content

Added paragraph on creating DataFrame from list of namedtuples #35507

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 4 commits into from
Aug 5, 2020
Merged
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions doc/source/user_guide/dsintro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,30 @@ The result will be a DataFrame with the same index as the input Series, and
with one column whose name is the original name of the Series (only if no other
column name provided).


.. _basics.dataframe.from_list_namedtuples:

From a list of namedtuples
~~~~~~~~~~~~~~~~~~~~~~~~~~

The first namedtuple will be used to determine the columns of the DataFrame.
Copy link
Member

Choose a reason for hiding this comment

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

I might write:

The field names of the first ``namedtuple`` in the list determine the columns of the ``DataFrame``. 
The remaining namedtuples (or tuples) are simply unpacked and their values are fed
into the rows of the ``DataFrame``. If any of those tuples is shorter than the first ``namedtuple`` 
then the later columns in the corresponding row are marked as missing values or, in case 
it is longer than the first ``namedtuple``, a ``ValueError`` is raised.

This means that further (named)tuples are simply unpacked. If additional
tuples are shorter, the later columns are marked as missing data, while
longer tuples cause a ``ValueError``.

.. ipython:: python

from collections import namedtuple
Copy link
Member

Choose a reason for hiding this comment

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

CI Checks report a problem with the indent here


Copy link
Member

Choose a reason for hiding this comment

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

you have a trailing whitespace here

Point = namedtuple('Point', 'x y')
Copy link
Member

Choose a reason for hiding this comment

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

also indent problem


pd.DataFrame([Point(0, 0), Point(0, 3), Point(2, 3)])
Copy link
Member

Choose a reason for hiding this comment

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

also indent problem


Point3D = namedtuple('Point3D', 'x y z')
Copy link
Member

Choose a reason for hiding this comment

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

also indent problem


pd.DataFrame([Point3D(0, 0, 0), Point3D(0, 3, 5), Point(2, 3)])
Copy link
Member

Choose a reason for hiding this comment

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

also indent problem



.. _basics.dataframe.from_list_dataclasses:

From a list of dataclasses
Expand Down