Skip to content

Commit e0fe9a0

Browse files
ianhiphofl
andauthored
Backport to 2.3.x: REGR: from_records not initializing subclasses properly (#60726) (#62436)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 23a1085 commit e0fe9a0

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v2.3.3.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Bug fixes
5858
- The :meth:`DataFrame.iloc` now works correctly with ``copy_on_write`` option when assigning values after subsetting the columns of a homogeneous DataFrame (:issue:`60309`)
5959

6060

61+
Other Bug fixes
62+
~~~~~~~~~~~~~~~~
63+
64+
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)
65+
66+
6167
.. ---------------------------------------------------------------------------
6268
.. _whatsnew_233.contributors:
6369

pandas/core/frame.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,8 +2549,10 @@ def maybe_reorder(
25492549

25502550
manager = _get_option("mode.data_manager", silent=True)
25512551
mgr = arrays_to_mgr(arrays, columns, result_index, typ=manager)
2552-
2553-
return cls._from_mgr(mgr, axes=mgr.axes)
2552+
df = DataFrame._from_mgr(mgr, axes=mgr.axes)
2553+
if cls is not DataFrame:
2554+
return cls(df, copy=False)
2555+
return df
25542556

25552557
def to_records(
25562558
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
@@ -784,6 +784,13 @@ def test_constructor_with_metadata():
784784
assert isinstance(subset, MySubclassWithMetadata)
785785

786786

787+
def test_constructor_with_metadata_from_records():
788+
# GH#57008
789+
df = MySubclassWithMetadata.from_records([{"a": 1, "b": 2}])
790+
assert df.my_metadata is None
791+
assert type(df) is MySubclassWithMetadata
792+
793+
787794
class SimpleDataFrameSubClass(DataFrame):
788795
"""A subclass of DataFrame that does not define a constructor."""
789796

0 commit comments

Comments
 (0)