diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 4d7576c013fd6..addf1817fb4d6 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -79,6 +79,7 @@ Fixed regressions - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`) - Fixed regression causing an ``AttributeError`` during warning emitted if the provided table name in :meth:`DataFrame.to_sql` and the table name actually used in the database do not match (:issue:`48733`) - Fixed regression in :func:`to_datetime` when ``arg`` was a date string with nanosecond and ``format`` contained ``%f`` would raise a ``ValueError`` (:issue:`48767`) +- Fixed regression in :func:`assert_frame_equal` raising for :class:`MultiIndex` with :class:`Categorical` and ``check_like=True`` (:issue:`48975`) - Fixed regression in :meth:`DataFrame.fillna` replacing wrong values for ``datetime64[ns]`` dtype and ``inplace=True`` (:issue:`48863`) - Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`) - Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index 48d367b86b157..fd8fa50dab99e 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -190,6 +190,9 @@ def safe_sort_index(index: Index) -> Index: except TypeError: pass else: + if isinstance(array_sorted, MultiIndex): + return array_sorted + array_sorted = cast(np.ndarray, array_sorted) if isinstance(index, MultiIndex): index = MultiIndex.from_tuples(array_sorted, names=index.names) diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index 8139ffed78f7f..c66a93d2db651 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -343,3 +343,26 @@ def test_assert_frame_equal_checking_allow_dups_flag(): with pytest.raises(AssertionError, match="allows_duplicate_labels"): tm.assert_frame_equal(left, right, check_flags=True) + + +def test_assert_frame_equal_check_like_categorical_midx(): + # GH#48975 + left = DataFrame( + [[1], [2], [3]], + index=pd.MultiIndex.from_arrays( + [ + pd.Categorical(["a", "b", "c"]), + pd.Categorical(["a", "b", "c"]), + ] + ), + ) + right = DataFrame( + [[3], [2], [1]], + index=pd.MultiIndex.from_arrays( + [ + pd.Categorical(["c", "b", "a"]), + pd.Categorical(["c", "b", "a"]), + ] + ), + ) + tm.assert_frame_equal(left, right, check_like=True)