Skip to content

Commit 65fd5ba

Browse files
GH1396 Add concat compatibility for subclasses of DataFrame (#1713)
* GH1396 Add concat compatibility for subclasses of DataFrame * GH1396 Add concat compatibility for subclasses of DataFrame * GH1396 Add concat compatibility for subclasses of DataFrame
1 parent f3e33e2 commit 65fd5ba

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

pandas-stubs/core/reshape/concat.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from collections.abc import (
66
from typing import (
77
Literal,
88
Never,
9+
TypeVar,
910
overload,
1011
)
1112

@@ -23,6 +24,8 @@ from pandas._typing import (
2324
HashableT4,
2425
)
2526

27+
DataFrameT0 = TypeVar("DataFrameT0", bound=DataFrame)
28+
2629
@overload
2730
def concat(
2831
objs: Iterable[None] | Mapping[HashableT1, None],
@@ -63,6 +66,19 @@ def concat( # type: ignore[overload-overlap]
6366
sort: bool = False,
6467
) -> Series: ...
6568
@overload
69+
def concat(
70+
objs: Iterable[DataFrameT0 | None],
71+
*,
72+
axis: Axis = 0,
73+
join: Literal["inner", "outer"] = "outer",
74+
ignore_index: bool = False,
75+
keys: Iterable[HashableT2] | None = None,
76+
levels: Sequence[list[HashableT3] | tuple[HashableT3, ...]] | None = None,
77+
names: list[HashableT4] | None = None,
78+
verify_integrity: bool = False,
79+
sort: bool = False,
80+
) -> DataFrameT0: ...
81+
@overload
6682
def concat(
6783
objs: Iterable[NDFrame | None] | Mapping[HashableT1, NDFrame | None],
6884
*,

tests/test_pandas.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,21 @@ def test_concat_args() -> None:
404404
)
405405

406406

407+
def test_frame_subclass_concat() -> None:
408+
"""Test concatenate subclass of DataFrame GH1396."""
409+
410+
class ChildDataFrame(pd.DataFrame):
411+
@property
412+
def _constructor(self) -> type[ChildDataFrame]:
413+
return ChildDataFrame
414+
415+
cdf1 = ChildDataFrame(data={"a": [0]})
416+
cdf2 = ChildDataFrame(data={"a": [1]})
417+
418+
cdf = pd.concat([cdf1, cdf2], ignore_index=True)
419+
check(assert_type(cdf, ChildDataFrame), ChildDataFrame)
420+
421+
407422
def test_types_json_normalize() -> None:
408423
data1: list[dict[str, Any]] = [
409424
{"id": 1, "name": {"first": "Coleen", "last": "Volk"}},

0 commit comments

Comments
 (0)