Skip to content

Added dataclass into dataframe API reference #37632

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 1 commit into from
Nov 4, 2020
Merged
Changes from all 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
12 changes: 11 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class DataFrame(NDFrame, OpsMixin):
Parameters
----------
data : ndarray (structured or homogeneous), Iterable, dict, or DataFrame
Dict can contain Series, arrays, constants, or list-like objects. If
Dict can contain Series, arrays, constants, dataclass or list-like objects. If
Copy link
Member

Choose a reason for hiding this comment

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

We are not adding dataclass to a dictionary, but rather as it's own object. So something like this would make more sense:

Iterable, dict, DataFrame or dataclass

data is a dict, column order follows insertion-order.

.. versionchanged:: 0.25.0
Expand Down Expand Up @@ -420,6 +420,16 @@ class DataFrame(NDFrame, OpsMixin):
0 1 2 3
1 4 5 6
2 7 8 9

Constructing DataFrame from dataclass:

>>> from dataclasses import make_dataclass
>>> Point = make_dataclass("Point", [("x", int), ("y", int)])
>>> 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.

Can you change this:

    >>> from dataclasses import make_dataclass
    >>> Point = make_dataclass("Point", [("x", int), ("y", int)])
    >>> df3 = pd.DataFrame([Point(0, 0), Point(0, 3), Point(2, 3)])
    >>> df3

x y
0 0 0
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 align the columns with the data here.

1 0 3
2 2 3
"""

_internal_names_set = {"columns", "index"} | NDFrame._internal_names_set
Expand Down