Skip to content

Commit 0ce2921

Browse files
Update join docs for other param (pandas-dev#46850)
* Update join docs for other param Update join docs regarding using multiple Series * Update type for _join_compat * Allow any iterable for join; test join for a list of series * Update type signature * Update pd.concat type, add cast() to make frame.join() work with mypy * Fix type union syntax * NDFrame * Remove cast * Fix mypy errors * Code review * Remove unnecessary assert * Add comment explaining the cast * Fix swapped order of cast comment * Remove full stop * Update pandas/core/frame.py Co-authored-by: Marc Garcia <[email protected]>
1 parent 060ce49 commit 0ce2921

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pandas/core/frame.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -9788,7 +9788,7 @@ def _append(
97889788

97899789
def join(
97909790
self,
9791-
other: DataFrame | Series,
9791+
other: DataFrame | Series | list[DataFrame | Series],
97929792
on: IndexLabel | None = None,
97939793
how: str = "left",
97949794
lsuffix: str = "",
@@ -9805,7 +9805,7 @@ def join(
98059805
98069806
Parameters
98079807
----------
9808-
other : DataFrame, Series, or list of DataFrame
9808+
other : DataFrame, Series, or a list containing any combination of them
98099809
Index should be similar to one of the columns in this one. If a
98109810
Series is passed, its name attribute must be set, and that will be
98119811
used as the column name in the resulting joined DataFrame.
@@ -9961,7 +9961,7 @@ def join(
99619961

99629962
def _join_compat(
99639963
self,
9964-
other: DataFrame | Series,
9964+
other: DataFrame | Series | Iterable[DataFrame | Series],
99659965
on: IndexLabel | None = None,
99669966
how: str = "left",
99679967
lsuffix: str = "",
@@ -10010,7 +10010,11 @@ def _join_compat(
1001010010
"Suffixes not supported when joining multiple DataFrames"
1001110011
)
1001210012

10013-
frames = [self] + list(other)
10013+
# Mypy thinks the RHS is a
10014+
# "Union[DataFrame, Series, Iterable[Union[DataFrame, Series]]]" whereas
10015+
# the LHS is an "Iterable[DataFrame]", but in reality both types are
10016+
# "Iterable[Union[DataFrame, Series]]" due to the if statements
10017+
frames = [cast("DataFrame | Series", self)] + list(other)
1001410018

1001510019
can_concat = all(df.index.is_unique for df in frames)
1001610020

pandas/tests/frame/methods/test_join.py

+9
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ def test_join_left_sequence_non_unique_index():
367367
tm.assert_frame_equal(joined, expected)
368368

369369

370+
def test_join_list_series(float_frame):
371+
# GH#46850
372+
# Join a DataFrame with a list containing both a Series and a DataFrame
373+
left = float_frame.A.to_frame()
374+
right = [float_frame.B, float_frame[["C", "D"]]]
375+
result = left.join(right)
376+
tm.assert_frame_equal(result, float_frame)
377+
378+
370379
@pytest.mark.parametrize("sort_kw", [True, False])
371380
def test_suppress_future_warning_with_sort_kw(sort_kw):
372381
a = DataFrame({"col1": [1, 2]}, index=["c", "a"])

0 commit comments

Comments
 (0)