Skip to content

Commit

Permalink
BUG: merge cross with Series (pandas-dev#54087)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Jul 11, 2023
1 parent ea56ebb commit 12c886a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 8 additions & 6 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ def merge(
indicator: str | bool = False,
validate: str | None = None,
) -> DataFrame:
left_df = _validate_operand(left)
right_df = _validate_operand(right)
if how == "cross":
return _cross_merge(
left,
right,
left_df,
right_df,
on=on,
left_on=left_on,
right_on=right_on,
Expand All @@ -162,8 +164,8 @@ def merge(
)
else:
op = _MergeOperation(
left,
right,
left_df,
right_df,
how=how,
on=on,
left_on=left_on,
Expand All @@ -179,8 +181,8 @@ def merge(


def _cross_merge(
left: DataFrame | Series,
right: DataFrame | Series,
left: DataFrame,
right: DataFrame,
on: IndexLabel | None = None,
left_on: IndexLabel | None = None,
right_on: IndexLabel | None = None,
Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/reshape/merge/test_merge_cross.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from pandas import DataFrame
from pandas import (
DataFrame,
Series,
)
import pandas._testing as tm
from pandas.core.reshape.merge import (
MergeError,
Expand Down Expand Up @@ -96,3 +99,13 @@ def test_join_cross_error_reporting():
)
with pytest.raises(MergeError, match=msg):
left.join(right, how="cross", on="a")


def test_merge_cross_series():
# GH#54055
ls = Series([1, 2, 3, 4], index=[1, 2, 3, 4], name="left")
rs = Series([3, 4, 5, 6], index=[3, 4, 5, 6], name="right")
res = merge(ls, rs, how="cross")

expected = merge(ls.to_frame(), rs.to_frame(), how="cross")
tm.assert_frame_equal(res, expected)

0 comments on commit 12c886a

Please sign in to comment.