Skip to content

Commit 03375c6

Browse files
committed
overload concat function
1 parent 61372ea commit 03375c6

File tree

6 files changed

+45
-20
lines changed

6 files changed

+45
-20
lines changed

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
intended for public consumption
44
"""
55
from textwrap import dedent
6-
from typing import Dict, Optional, TYPE_CHECKING, Tuple, Union
6+
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union
77
from warnings import catch_warnings, simplefilter, warn
88

99
import numpy as np

pandas/core/reshape/concat.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
concat routines
33
"""
44

5-
from typing import List, Union
5+
from typing import List, Mapping, Sequence, Union, overload
66

77
import numpy as np
88

@@ -25,9 +25,28 @@
2525
# ---------------------------------------------------------------------
2626
# Concatenate DataFrame objects
2727

28+
FrameOrSeriesUnion = Union["DataFrame", "Series"]
2829

30+
31+
@overload
32+
def concat(
33+
objs: Union[Sequence["DataFrame"], Mapping[str, "DataFrame"]],
34+
axis=0,
35+
join: str = "outer",
36+
ignore_index: bool = False,
37+
keys=None,
38+
levels=None,
39+
names=None,
40+
verify_integrity: bool = False,
41+
sort: bool = False,
42+
copy: bool = True,
43+
) -> "DataFrame":
44+
...
45+
46+
47+
@overload
2948
def concat(
30-
objs,
49+
objs: Union[Sequence[FrameOrSeriesUnion], Mapping[str, FrameOrSeriesUnion]],
3150
axis=0,
3251
join: str = "outer",
3352
ignore_index: bool = False,
@@ -37,6 +56,21 @@ def concat(
3756
verify_integrity: bool = False,
3857
sort: bool = False,
3958
copy: bool = True,
59+
) -> Union["DataFrame", "Series"]:
60+
...
61+
62+
63+
def concat(
64+
objs: Union[Sequence[FrameOrSeriesUnion], Mapping[str, FrameOrSeriesUnion]],
65+
axis=0,
66+
join="outer",
67+
ignore_index: bool = False,
68+
keys=None,
69+
levels=None,
70+
names=None,
71+
verify_integrity: bool = False,
72+
sort: bool = False,
73+
copy: bool = True,
4074
) -> Union["DataFrame", "Series"]:
4175
"""
4276
Concatenate pandas objects along a particular axis with optional set logic

pandas/core/reshape/pivot.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, Callable, Dict, Tuple, Union
1+
from typing import TYPE_CHECKING, Callable, Dict, List, Tuple, Union
22

33
import numpy as np
44

@@ -40,7 +40,7 @@ def pivot_table(
4040
columns = _convert_by(columns)
4141

4242
if isinstance(aggfunc, list):
43-
pieces = []
43+
pieces: List[DataFrame] = []
4444
keys = []
4545
for func in aggfunc:
4646
table = pivot_table(
@@ -58,9 +58,7 @@ def pivot_table(
5858
pieces.append(table)
5959
keys.append(getattr(func, "__name__", func))
6060

61-
result = concat(pieces, keys=keys, axis=1)
62-
assert isinstance(result, DataFrame)
63-
return result
61+
return concat(pieces, keys=keys, axis=1)
6462

6563
keys = index + columns
6664

pandas/core/reshape/reshape.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -922,9 +922,7 @@ def check_len(item, name):
922922
dtype=dtype,
923923
)
924924
with_dummies.append(dummy)
925-
concatted = concat(with_dummies, axis=1)
926-
assert isinstance(concatted, DataFrame)
927-
result = concatted
925+
result = concat(with_dummies, axis=1)
928926
else:
929927
result = _get_dummies_1d(
930928
data,

pandas/io/formats/format.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def __init__(
260260
self._chk_truncate()
261261

262262
def _chk_truncate(self) -> None:
263+
from pandas.core.series import Series
263264
from pandas.core.reshape.concat import concat
264265

265266
self.tr_row_num: Optional[int]
@@ -678,11 +679,9 @@ def _chk_truncate(self) -> None:
678679
col_num = max_cols
679680
else:
680681
col_num = max_cols_adj // 2
681-
concatted = concat(
682+
frame = concat(
682683
(frame.iloc[:, :col_num], frame.iloc[:, -col_num:]), axis=1
683684
)
684-
assert isinstance(concatted, DataFrame)
685-
frame = concatted
686685
# truncate formatter
687686
if isinstance(self.formatters, (list, tuple)):
688687
truncate_fmt = self.formatters
@@ -699,9 +698,7 @@ def _chk_truncate(self) -> None:
699698
frame = frame.iloc[:max_rows, :]
700699
else:
701700
row_num = max_rows_adj // 2
702-
concatted = concat((frame.iloc[:row_num, :], frame.iloc[-row_num:, :]))
703-
assert isinstance(concatted, DataFrame)
704-
frame = concatted
701+
frame = concat((frame.iloc[:row_num, :], frame.iloc[-row_num:, :]))
705702
self.tr_row_num = row_num
706703
else:
707704
self.tr_row_num = None

pandas/io/pytables.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4432,9 +4432,7 @@ def read(
44324432
if len(frames) == 1:
44334433
df = frames[0]
44344434
else:
4435-
concatted = concat(frames, axis=1)
4436-
assert isinstance(concatted, DataFrame)
4437-
df = concatted
4435+
df = concat(frames, axis=1)
44384436

44394437
selection = Selection(self, where=where, start=start, stop=stop)
44404438
# apply the selection filters & axis orderings

0 commit comments

Comments
 (0)