Skip to content

Commit

Permalink
REGR: from_records not initializing subclasses properly (#60726)
Browse files Browse the repository at this point in the history
* REGR: from_records not initializing subclasses properly

* Move whatsnew
  • Loading branch information
phofl authored Jan 17, 2025
1 parent 72fd708 commit a4e8149
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
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

0 comments on commit a4e8149

Please sign in to comment.