Skip to content

Commit 7fb03d4

Browse files
cmp0xffDr-Irv
andauthored
feat: #1300 concat Series with dtype (#1302)
* feat: #1300 (comment) * fix(comment): https://github.com/pandas-dev/pandas-stubs/pull/1302/files#r2247860692 * Apply suggestion from @Dr-Irv https://github.com/pandas-dev/pandas-stubs/pull/1302/files#r2248094383 Co-authored-by: Irv Lustig <[email protected]> --------- Co-authored-by: Irv Lustig <[email protected]>
1 parent 8e21317 commit 7fb03d4

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

pandas-stubs/core/reshape/concat.pyi

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from pandas import (
1515
from typing_extensions import Never
1616

1717
from pandas._typing import (
18+
S2,
1819
Axis,
1920
AxisIndex,
2021
HashableT1,
@@ -38,7 +39,21 @@ def concat( # type: ignore[overload-overlap]
3839
copy: bool = ...,
3940
) -> DataFrame: ...
4041
@overload
41-
def concat( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
42+
def concat( # pyright: ignore[reportOverlappingOverload]
43+
objs: Iterable[Series[S2]],
44+
*,
45+
axis: AxisIndex = ...,
46+
join: Literal["inner", "outer"] = ...,
47+
ignore_index: bool = ...,
48+
keys: Iterable[HashableT2] = ...,
49+
levels: Sequence[list[HashableT3] | tuple[HashableT3, ...]] = ...,
50+
names: list[HashableT4] | None = ...,
51+
verify_integrity: bool = ...,
52+
sort: bool = ...,
53+
copy: bool = ...,
54+
) -> Series[S2]: ...
55+
@overload
56+
def concat( # type: ignore[overload-overlap]
4257
objs: Iterable[Series] | Mapping[HashableT1, Series],
4358
*,
4459
axis: AxisIndex = ...,

tests/test_frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,8 +2535,8 @@ def test_types_regressions() -> None:
25352535
ts1 = pd.concat([s1, s2], axis=0)
25362536
ts2 = pd.concat([s1, s2])
25372537

2538-
check(assert_type(ts1, pd.Series), pd.Series)
2539-
check(assert_type(ts2, pd.Series), pd.Series)
2538+
check(assert_type(ts1, "pd.Series[int]"), pd.Series, np.integer)
2539+
check(assert_type(ts2, "pd.Series[int]"), pd.Series, np.integer)
25402540

25412541
# https://github.com/microsoft/python-type-stubs/issues/110
25422542
check(assert_type(pd.Timestamp("2021-01-01"), pd.Timestamp), datetime.date)

tests/test_pandas.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,30 @@ def test_types_concat() -> None:
132132
s = pd.Series([0, 1, -10])
133133
s2 = pd.Series([7, -5, 10])
134134

135-
check(assert_type(pd.concat([s, s2]), pd.Series), pd.Series)
135+
check(assert_type(pd.concat([s, s2]), "pd.Series[int]"), pd.Series, np.integer)
136136
check(assert_type(pd.concat([s, s2], axis=1), pd.DataFrame), pd.DataFrame)
137137
check(
138-
assert_type(pd.concat([s, s2], keys=["first", "second"], sort=True), pd.Series),
138+
assert_type(
139+
pd.concat([s, s2], keys=["first", "second"], sort=True), "pd.Series[int]"
140+
),
139141
pd.Series,
142+
np.integer,
140143
)
141144
check(
142145
assert_type(
143146
pd.concat([s, s2], keys=["first", "second"], names=["source", "row"]),
144-
pd.Series,
147+
"pd.Series[int]",
145148
),
146149
pd.Series,
150+
np.integer,
147151
)
148152
check(
149153
assert_type(
150154
pd.concat([s, s2], keys=["first", "second"], names=None),
151-
pd.Series,
155+
"pd.Series[int]",
152156
),
153157
pd.Series,
158+
np.integer,
154159
)
155160

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

238243

244+
def test_concat_series_mixed_numeric() -> None:
245+
"""Test concatenation of Series with mixed numeric types.
246+
247+
Derived from test_types_concat."""
248+
s = pd.Series([0, 1, -10])
249+
s2 = pd.Series([7.0, -5, 10])
250+
251+
check(assert_type(pd.concat([s, s2]), pd.Series), pd.Series, np.floating)
252+
check(
253+
assert_type(pd.concat([s, s2], keys=["first", "second"], sort=True), pd.Series),
254+
pd.Series,
255+
np.floating,
256+
)
257+
check(
258+
assert_type(
259+
pd.concat([s, s2], keys=["first", "second"], names=["source", "row"]),
260+
pd.Series,
261+
),
262+
pd.Series,
263+
np.floating,
264+
)
265+
check(
266+
assert_type(
267+
pd.concat([s, s2], keys=["first", "second"], names=None),
268+
pd.Series,
269+
),
270+
pd.Series,
271+
np.floating,
272+
)
273+
274+
239275
def test_concat_args() -> None:
240276
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
241277
df2 = pd.DataFrame(data={"col1": [10, 20], "col2": [30, 40]}, index=[2, 3])

0 commit comments

Comments
 (0)