Skip to content

feat: #1300 concat Series with dtype #1302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pandas-stubs/core/reshape/concat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ from pandas import (
from typing_extensions import Never

from pandas._typing import (
S2,
Axis,
AxisIndex,
HashableT1,
Expand All @@ -38,7 +39,21 @@ def concat( # type: ignore[overload-overlap]
copy: bool = ...,
) -> DataFrame: ...
@overload
def concat( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
def concat( # pyright: ignore[reportOverlappingOverload]
objs: Iterable[Series[S2]],
*,
axis: AxisIndex = ...,
join: Literal["inner", "outer"] = ...,
ignore_index: bool = ...,
keys: Iterable[HashableT2] = ...,
levels: Sequence[list[HashableT3] | tuple[HashableT3, ...]] = ...,
names: list[HashableT4] | None = ...,
verify_integrity: bool = ...,
sort: bool = ...,
copy: bool = ...,
) -> Series[S2]: ...
@overload
def concat( # type: ignore[overload-overlap]
objs: Iterable[Series] | Mapping[HashableT1, Series],
*,
axis: AxisIndex = ...,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2523,8 +2523,8 @@ def test_types_regressions() -> None:
ts1 = pd.concat([s1, s2], axis=0)
ts2 = pd.concat([s1, s2])

check(assert_type(ts1, pd.Series), pd.Series)
check(assert_type(ts2, pd.Series), pd.Series)
check(assert_type(ts1, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(ts2, "pd.Series[int]"), pd.Series, np.integer)

# https://github.com/microsoft/python-type-stubs/issues/110
check(assert_type(pd.Timestamp("2021-01-01"), pd.Timestamp), datetime.date)
Expand Down
44 changes: 40 additions & 4 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,30 @@ def test_types_concat() -> None:
s = pd.Series([0, 1, -10])
s2 = pd.Series([7, -5, 10])

check(assert_type(pd.concat([s, s2]), pd.Series), pd.Series)
check(assert_type(pd.concat([s, s2]), "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(pd.concat([s, s2], axis=1), pd.DataFrame), pd.DataFrame)
check(
assert_type(pd.concat([s, s2], keys=["first", "second"], sort=True), pd.Series),
assert_type(
pd.concat([s, s2], keys=["first", "second"], sort=True), "pd.Series[int]"
),
pd.Series,
np.integer,
)
check(
assert_type(
pd.concat([s, s2], keys=["first", "second"], names=["source", "row"]),
pd.Series,
"pd.Series[int]",
),
pd.Series,
np.integer,
)
check(
assert_type(
pd.concat([s, s2], keys=["first", "second"], names=None),
pd.Series,
"pd.Series[int]",
),
pd.Series,
np.integer,
)

# Depends on the axis
Expand Down Expand Up @@ -236,6 +241,37 @@ def test_types_concat() -> None:
check(assert_type(pd.concat([pd.DataFrame(), data]), pd.DataFrame), pd.DataFrame)


def test_concat_series_mixed_numeric() -> None:
"""Test concatenation of Series with mixed numeric types.

Derived from test_types_concat."""
s = pd.Series([0, 1, -10])
s2 = pd.Series([7.0, -5, 10])

check(assert_type(pd.concat([s, s2]), pd.Series), pd.Series, np.floating)
check(
assert_type(pd.concat([s, s2], keys=["first", "second"], sort=True), pd.Series),
pd.Series,
np.floating,
)
check(
assert_type(
pd.concat([s, s2], keys=["first", "second"], names=["source", "row"]),
pd.Series,
),
pd.Series,
np.floating,
)
check(
assert_type(
pd.concat([s, s2], keys=["first", "second"], names=None),
pd.Series,
),
pd.Series,
np.floating,
)


def test_concat_args() -> None:
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
df2 = pd.DataFrame(data={"col1": [10, 20], "col2": [30, 40]}, index=[2, 3])
Expand Down
Loading