Skip to content

REG: Fix regression in assert_frame_equal for categorical and check_like #48987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)