Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Other
^^^^^
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
are not installed (:issue:`60196`)
-
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)

.. ---------------------------------------------------------------------------
.. _whatsnew_230.contributors:
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