Skip to content

Commit 236cedf

Browse files
nsarangphofljorisvandenbossche
authored
REGR: fix error caused by deprecation warning in pd.merge when joining two Series (#47946)
* Fix bug in warning * add test * add note to whatsnew * Add gh ref * catch warning in the test Co-authored-by: Patrick Hoefler <[email protected]> Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 838b04f commit 236cedf

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)
2020
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
2121
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
22+
- Fixed regression in :func:`merge` throwing an error when passing a :class:`Series` with a multi-level name
2223
- Fixed regression in :meth:`DataFrame.eval` creating a copy when updating inplace (:issue:`47449`)
2324
-
2425

pandas/core/reshape/merge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ def __init__(
676676
if _left.columns.nlevels != _right.columns.nlevels:
677677
msg = (
678678
"merging between different levels is deprecated and will be removed "
679-
f"in a future version. ({left.columns.nlevels} levels on the left, "
680-
f"{right.columns.nlevels} on the right)"
679+
f"in a future version. ({_left.columns.nlevels} levels on the left, "
680+
f"{_right.columns.nlevels} on the right)"
681681
)
682682
# stacklevel chosen to be correct when this is reached via pd.merge
683683
# (and not DataFrame.join)

pandas/tests/reshape/merge/test_merge.py

+20
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,26 @@ def test_merge_series(on, left_on, right_on, left_index, right_index, nm):
21952195
)
21962196

21972197

2198+
def test_merge_series_multilevel():
2199+
# GH#47946
2200+
a = DataFrame(
2201+
{"A": [1, 2, 3, 4]},
2202+
index=MultiIndex.from_product([["a", "b"], [0, 1]], names=["outer", "inner"]),
2203+
)
2204+
b = Series(
2205+
[1, 2, 3, 4],
2206+
index=MultiIndex.from_product([["a", "b"], [1, 2]], names=["outer", "inner"]),
2207+
name=("B", "C"),
2208+
)
2209+
expected = DataFrame(
2210+
{"A": [2, 4], ("B", "C"): [1, 3]},
2211+
index=MultiIndex.from_product([["a", "b"], [1]], names=["outer", "inner"]),
2212+
)
2213+
with tm.assert_produces_warning(FutureWarning):
2214+
result = merge(a, b, on=["outer", "inner"])
2215+
tm.assert_frame_equal(result, expected)
2216+
2217+
21982218
@pytest.mark.parametrize(
21992219
"col1, col2, kwargs, expected_cols",
22002220
[

0 commit comments

Comments
 (0)