Skip to content

REGR: from_records not initializing subclasses properly #60726

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 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ Other
^^^^^
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
are not installed (:issue:`60196`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_230.contributors:
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ Other
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)

.. ***DO NOT USE THIS SECTION***

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2317,7 +2317,10 @@ def maybe_reorder(
columns = columns.drop(exclude)

mgr = arrays_to_mgr(arrays, columns, result_index)
return cls._from_mgr(mgr, axes=mgr.axes)
df = DataFrame._from_mgr(mgr, axes=mgr.axes)
if cls is not DataFrame:
return cls(df, copy=False)
return df

def to_records(
self, index: bool = True, column_dtypes=None, index_dtypes=None
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,13 @@ def test_constructor_with_metadata():
assert isinstance(subset, MySubclassWithMetadata)


def test_constructor_with_metadata_from_records():
# GH#57008
df = MySubclassWithMetadata.from_records([{"a": 1, "b": 2}])
assert df.my_metadata is None
assert type(df) is MySubclassWithMetadata


class SimpleDataFrameSubClass(DataFrame):
"""A subclass of DataFrame that does not define a constructor."""

Expand Down
Loading