diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 1a11fffbf6b4e..f7463218096e5 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -601,6 +601,7 @@ Deprecations - Deprecated :meth:`.Styler.set_na_rep` and :meth:`.Styler.set_precision` in favour of :meth:`.Styler.format` with ``na_rep`` and ``precision`` as existing and new input arguments respectively (:issue:`40134`, :issue:`40425`) - Deprecated allowing partial failure in :meth:`Series.transform` and :meth:`DataFrame.transform` when ``func`` is list-like or dict-like and raises anything but ``TypeError``; ``func`` raising anything but a ``TypeError`` will raise in a future version (:issue:`40211`) - Deprecated support for ``np.ma.mrecords.MaskedRecords`` in the :class:`DataFrame` constructor, pass ``{name: data[name] for name in data.dtype.names}`` instead (:issue:`40363`) +- Deprecated using :func:`merge` or :func:`join` on a different number of levels (:issue:`34862`) - Deprecated the use of ``**kwargs`` in :class:`.ExcelWriter`; use the keyword argument ``engine_kwargs`` instead (:issue:`40430`) - Deprecated the ``level`` keyword for :class:`DataFrame` and :class:`Series` aggregations; use groupby instead (:issue:`39983`) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 8cee0dd2abb88..b2fe18bba43f4 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -669,11 +669,11 @@ def __init__( # warn user when merging between different levels if _left.columns.nlevels != _right.columns.nlevels: msg = ( - "merging between different levels can give an unintended " - f"result ({left.columns.nlevels} levels on the left," + "merging between different levels is deprecated and will be removed " + f"in a future version. ({left.columns.nlevels} levels on the left," f"{right.columns.nlevels} on the right)" ) - warnings.warn(msg, UserWarning) + warnings.warn(msg, FutureWarning, stacklevel=3) self._validate_specification() diff --git a/pandas/tests/frame/methods/test_join.py b/pandas/tests/frame/methods/test_join.py index 1c7f7e3ff674a..36fd5d399c300 100644 --- a/pandas/tests/frame/methods/test_join.py +++ b/pandas/tests/frame/methods/test_join.py @@ -338,14 +338,14 @@ def test_merge_join_different_levels(self): # merge columns = ["a", "b", ("c", "c1")] expected = DataFrame(columns=columns, data=[[1, 11, 33], [0, 22, 44]]) - with tm.assert_produces_warning(UserWarning): + with tm.assert_produces_warning(FutureWarning): result = pd.merge(df1, df2, on="a") tm.assert_frame_equal(result, expected) # join, see discussion in GH#12219 columns = ["a", "b", ("a", ""), ("c", "c1")] expected = DataFrame(columns=columns, data=[[1, 11, 0, 44], [0, 22, 1, 33]]) - with tm.assert_produces_warning(UserWarning): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): result = df1.join(df2, on="a") tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py index fb161e38c7155..2201da8ccb0d2 100644 --- a/pandas/tests/reshape/merge/test_join.py +++ b/pandas/tests/reshape/merge/test_join.py @@ -417,7 +417,7 @@ def test_join_hierarchical_mixed(self): other_df = DataFrame([(1, 2, 3), (7, 10, 6)], columns=["a", "b", "d"]) other_df.set_index("a", inplace=True) # GH 9455, 12219 - with tm.assert_produces_warning(UserWarning): + with tm.assert_produces_warning(FutureWarning): result = merge(new_df, other_df, left_index=True, right_index=True) assert ("b", "mean") in result assert "b" in result