Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Oct 25, 2023
1 parent 8407cad commit b78c0fa
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,29 @@ def test_replace_list_method(self):
expected = tm.SubclassedDataFrame({"A": [0, 0, 0]})
assert isinstance(result, tm.SubclassedDataFrame)
tm.assert_frame_equal(result, expected)


class MySubclassWithMetadata(DataFrame):
_metadata = ["my_metadata"]

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)

my_metadata = kwargs.pop("my_metadata", None)
if args and isinstance(args[0], MySubclassWithMetadata):
my_metadata = args[0].my_metadata
self.my_metadata = my_metadata

@property
def _constructor(self):
return MySubclassWithMetadata


def test_constructor_with_metadata():
# https://github.com/pandas-dev/pandas/pull/54922
# https://github.com/pandas-dev/pandas/issues/55120
df = MySubclassWithMetadata(
np.random.default_rng(2).random((5, 3)), columns=["A", "B", "C"]
)
subset = df[["A", "B"]]
assert isinstance(subset, MySubclassWithMetadata)

0 comments on commit b78c0fa

Please sign in to comment.