|
10 | 10 | ) |
11 | 11 | import pandas._testing as tm |
12 | 12 |
|
| 13 | +pytestmark = pytest.mark.filterwarnings( |
| 14 | + "ignore:Passing a BlockManager|Passing a SingleBlockManager:DeprecationWarning" |
| 15 | +) |
| 16 | + |
13 | 17 |
|
14 | 18 | @pytest.fixture() |
15 | 19 | def gpd_style_subclass_df(): |
@@ -734,8 +738,36 @@ def test_replace_list_method(self): |
734 | 738 | # https://github.com/pandas-dev/pandas/pull/46018 |
735 | 739 | df = tm.SubclassedDataFrame({"A": [0, 1, 2]}) |
736 | 740 | 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 | + ): |
738 | 744 | result = df.replace([1, 2], method="ffill") |
739 | 745 | expected = tm.SubclassedDataFrame({"A": [0, 0, 0]}) |
740 | 746 | assert isinstance(result, tm.SubclassedDataFrame) |
741 | 747 | 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) |
0 commit comments