Skip to content

Commit f1c120d

Browse files
committed
REGR: from_records not initializing subclasses properly
1 parent 72fd708 commit f1c120d

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

doc/source/whatsnew/v2.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Other
175175
^^^^^
176176
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
177177
are not installed (:issue:`60196`)
178-
-
178+
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)
179179

180180
.. ---------------------------------------------------------------------------
181181
.. _whatsnew_230.contributors:

pandas/core/frame.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2317,7 +2317,10 @@ def maybe_reorder(
23172317
columns = columns.drop(exclude)
23182318

23192319
mgr = arrays_to_mgr(arrays, columns, result_index)
2320-
return cls._from_mgr(mgr, axes=mgr.axes)
2320+
df = DataFrame._from_mgr(mgr, axes=mgr.axes)
2321+
if cls is not DataFrame:
2322+
return cls(df, copy=False)
2323+
return df
23212324

23222325
def to_records(
23232326
self, index: bool = True, column_dtypes=None, index_dtypes=None

pandas/tests/frame/test_subclass.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,13 @@ def test_constructor_with_metadata():
769769
assert isinstance(subset, MySubclassWithMetadata)
770770

771771

772+
def test_constructor_with_metadata_from_records():
773+
# GH#57008
774+
df = MySubclassWithMetadata.from_records([{"a": 1, "b": 2}])
775+
assert df.my_metadata is None
776+
assert type(df) is MySubclassWithMetadata
777+
778+
772779
class SimpleDataFrameSubClass(DataFrame):
773780
"""A subclass of DataFrame that does not define a constructor."""
774781

0 commit comments

Comments
 (0)