Skip to content

Commit 9c7b567

Browse files
Backport PR #54922 on branch 2.1.x (REGR: Restore _constructor_from_mgr to pass manager object to constructor) (#55704)
Backport PR #54922: REGR: Restore _constructor_from_mgr to pass manager object to constructor
1 parent 8059d86 commit 9c7b567

File tree

11 files changed

+77
-22
lines changed

11 files changed

+77
-22
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929
- Fixed performance regression with wide DataFrames, typically involving methods where all columns were accessed individually (:issue:`55256`, :issue:`55245`)
3030
- Fixed regression in :func:`merge_asof` raising ``TypeError`` for ``by`` with datetime and timedelta dtypes (:issue:`55453`)
3131
- Fixed regression in :meth:`DataFrame.to_sql` not roundtripping datetime columns correctly for sqlite when using ``detect_types`` (:issue:`55554`)
32+
- Fixed regression in construction of certain DataFrame or Series subclasses (:issue:`54922`)
3233

3334
.. ---------------------------------------------------------------------------
3435
.. _whatsnew_212.bug_fixes:

pandas/core/frame.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -639,28 +639,25 @@ def _constructor(self) -> Callable[..., DataFrame]:
639639
return DataFrame
640640

641641
def _constructor_from_mgr(self, mgr, axes):
642-
df = self._from_mgr(mgr, axes=axes)
643-
644-
if type(self) is DataFrame:
645-
# fastpath avoiding constructor call
646-
return df
642+
if self._constructor is DataFrame:
643+
# we are pandas.DataFrame (or a subclass that doesn't override _constructor)
644+
return self._from_mgr(mgr, axes=axes)
647645
else:
648646
assert axes is mgr.axes
649-
return self._constructor(df, copy=False)
647+
return self._constructor(mgr)
650648

651649
_constructor_sliced: Callable[..., Series] = Series
652650

653651
def _sliced_from_mgr(self, mgr, axes) -> Series:
654652
return Series._from_mgr(mgr, axes)
655653

656654
def _constructor_sliced_from_mgr(self, mgr, axes):
657-
ser = self._sliced_from_mgr(mgr, axes=axes)
658-
ser._name = None # caller is responsible for setting real name
659-
if type(self) is DataFrame:
660-
# fastpath avoiding constructor call
655+
if self._constructor_sliced is Series:
656+
ser = self._sliced_from_mgr(mgr, axes)
657+
ser._name = None # caller is responsible for setting real name
661658
return ser
662659
assert axes is mgr.axes
663-
return self._constructor_sliced(ser, copy=False)
660+
return self._constructor_sliced(mgr)
664661

665662
# ----------------------------------------------------------------------
666663
# Constructors

pandas/core/series.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,14 @@ def _constructor(self) -> Callable[..., Series]:
579579
return Series
580580

581581
def _constructor_from_mgr(self, mgr, axes):
582-
ser = self._from_mgr(mgr, axes=axes)
583-
ser._name = None # caller is responsible for setting real name
584-
if type(self) is Series:
585-
# fastpath avoiding constructor call
582+
if self._constructor is Series:
583+
# we are pandas.Series (or a subclass that doesn't override _constructor)
584+
ser = self._from_mgr(mgr, axes=axes)
585+
ser._name = None # caller is responsible for setting real name
586586
return ser
587587
else:
588588
assert axes is mgr.axes
589-
return self._constructor(ser, copy=False)
589+
return self._constructor(mgr)
590590

591591
@property
592592
def _constructor_expanddim(self) -> Callable[..., DataFrame]:
@@ -610,12 +610,12 @@ def _expanddim_from_mgr(self, mgr, axes) -> DataFrame:
610610
return DataFrame._from_mgr(mgr, axes=mgr.axes)
611611

612612
def _constructor_expanddim_from_mgr(self, mgr, axes):
613-
df = self._expanddim_from_mgr(mgr, axes)
614-
if type(self) is Series:
615-
# fastpath avoiding constructor
616-
return df
613+
from pandas.core.frame import DataFrame
614+
615+
if self._constructor_expanddim is DataFrame:
616+
return self._expanddim_from_mgr(mgr, axes)
617617
assert axes is mgr.axes
618-
return self._constructor_expanddim(df, copy=False)
618+
return self._constructor_expanddim(mgr)
619619

620620
# types
621621
@property

pandas/tests/frame/test_arithmetic.py

+3
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,9 @@ def test_frame_sub_nullable_int(any_int_ea_dtype):
20782078
tm.assert_frame_equal(result, expected)
20792079

20802080

2081+
@pytest.mark.filterwarnings(
2082+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
2083+
)
20812084
def test_frame_op_subclass_nonclass_constructor():
20822085
# GH#43201 subclass._constructor is a function, not the subclass itself
20832086

pandas/tests/frame/test_subclass.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
)
1111
import pandas._testing as tm
1212

13+
pytestmark = pytest.mark.filterwarnings(
14+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
15+
)
16+
1317

1418
@pytest.fixture()
1519
def gpd_style_subclass_df():
@@ -734,8 +738,36 @@ def test_replace_list_method(self):
734738
# https://github.com/pandas-dev/pandas/pull/46018
735739
df = tm.SubclassedDataFrame({"A": [0, 1, 2]})
736740
msg = "The 'method' keyword in SubclassedDataFrame.replace is deprecated"
737-
with tm.assert_produces_warning(FutureWarning, match=msg):
741+
with tm.assert_produces_warning(
742+
FutureWarning, match=msg, raise_on_extra_warnings=False
743+
):
738744
result = df.replace([1, 2], method="ffill")
739745
expected = tm.SubclassedDataFrame({"A": [0, 0, 0]})
740746
assert isinstance(result, tm.SubclassedDataFrame)
741747
tm.assert_frame_equal(result, expected)
748+
749+
750+
class MySubclassWithMetadata(DataFrame):
751+
_metadata = ["my_metadata"]
752+
753+
def __init__(self, *args, **kwargs) -> None:
754+
super().__init__(*args, **kwargs)
755+
756+
my_metadata = kwargs.pop("my_metadata", None)
757+
if args and isinstance(args[0], MySubclassWithMetadata):
758+
my_metadata = args[0].my_metadata # type: ignore[has-type]
759+
self.my_metadata = my_metadata
760+
761+
@property
762+
def _constructor(self):
763+
return MySubclassWithMetadata
764+
765+
766+
def test_constructor_with_metadata():
767+
# https://github.com/pandas-dev/pandas/pull/54922
768+
# https://github.com/pandas-dev/pandas/issues/55120
769+
df = MySubclassWithMetadata(
770+
np.random.default_rng(2).random((5, 3)), columns=["A", "B", "C"]
771+
)
772+
subset = df[["A", "B"]]
773+
assert isinstance(subset, MySubclassWithMetadata)

pandas/tests/groupby/test_groupby_subclass.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import pandas._testing as tm
1212
from pandas.tests.groupby import get_groupby_method_args
1313

14+
pytestmark = pytest.mark.filterwarnings(
15+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
16+
)
17+
1418

1519
@pytest.mark.parametrize(
1620
"obj",

pandas/tests/reshape/concat/test_concat.py

+3
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,9 @@ def test_duplicate_keys_same_frame():
591591
tm.assert_frame_equal(result, expected)
592592

593593

594+
@pytest.mark.filterwarnings(
595+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
596+
)
594597
@pytest.mark.parametrize(
595598
"obj",
596599
[

pandas/tests/reshape/merge/test_merge.py

+3
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,9 @@ def test_merge_nan_right2(self):
689689
)[["i1", "i2", "i1_", "i3"]]
690690
tm.assert_frame_equal(result, expected)
691691

692+
@pytest.mark.filterwarnings(
693+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
694+
)
692695
def test_merge_type(self, df, df2):
693696
class NotADataFrame(DataFrame):
694697
@property

pandas/tests/reshape/merge/test_merge_ordered.py

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def test_multigroup(self, left, right):
7070
result = merge_ordered(left, right, on="key", left_by="group")
7171
assert result["group"].notna().all()
7272

73+
@pytest.mark.filterwarnings(
74+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
75+
)
7376
def test_merge_type(self, left, right):
7477
class NotADataFrame(DataFrame):
7578
@property

pandas/tests/series/methods/test_to_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from pandas import (
24
DataFrame,
35
Index,
@@ -40,6 +42,9 @@ def test_to_frame(self, datetime_series):
4042
)
4143
tm.assert_frame_equal(rs, xp)
4244

45+
@pytest.mark.filterwarnings(
46+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
47+
)
4348
def test_to_frame_expanddim(self):
4449
# GH#9762
4550

pandas/tests/series/test_subclass.py

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import pandas as pd
55
import pandas._testing as tm
66

7+
pytestmark = pytest.mark.filterwarnings(
8+
"ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning"
9+
)
10+
711

812
class TestSeriesSubclassing:
913
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)