Skip to content

Commit a4e8149

Browse files
authored
REGR: from_records not initializing subclasses properly (#60726)
* REGR: from_records not initializing subclasses properly * Move whatsnew
1 parent 72fd708 commit a4e8149

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

doc/source/whatsnew/v2.3.0.rst

-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ Other
175175
^^^^^
176176
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
177177
are not installed (:issue:`60196`)
178-
-
179178

180179
.. ---------------------------------------------------------------------------
181180
.. _whatsnew_230.contributors:

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ Other
812812
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
813813
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
814814
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)
815+
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)
815816

816817
.. ***DO NOT USE THIS SECTION***
817818

pandas/core/frame.py

+4-1
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

+7
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)