Skip to content

Commit 0b4064d

Browse files
Backport PR #48987 on branch 1.5.x (REG: Fix regression in assert_frame_equal for categorical and check_like) (#48997)
Backport PR #48987: REG: Fix regression in assert_frame_equal for categorical and check_like Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 20ee5ec commit 0b4064d

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Fixed regressions
7979
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)
8080
- 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`)
8181
- Fixed regression in :func:`to_datetime` when ``arg`` was a date string with nanosecond and ``format`` contained ``%f`` would raise a ``ValueError`` (:issue:`48767`)
82+
- Fixed regression in :func:`assert_frame_equal` raising for :class:`MultiIndex` with :class:`Categorical` and ``check_like=True`` (:issue:`48975`)
8283
- Fixed regression in :meth:`DataFrame.fillna` replacing wrong values for ``datetime64[ns]`` dtype and ``inplace=True`` (:issue:`48863`)
8384
- Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`)
8485
- Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`)

pandas/core/indexes/api.py

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ def safe_sort_index(index: Index) -> Index:
189189
except TypeError:
190190
pass
191191
else:
192+
if isinstance(array_sorted, MultiIndex):
193+
return array_sorted
194+
192195
array_sorted = cast(np.ndarray, array_sorted)
193196
if isinstance(index, MultiIndex):
194197
index = MultiIndex.from_tuples(array_sorted, names=index.names)

pandas/tests/util/test_assert_frame_equal.py

+23
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,26 @@ def test_assert_frame_equal_checking_allow_dups_flag():
342342

343343
with pytest.raises(AssertionError, match="allows_duplicate_labels"):
344344
tm.assert_frame_equal(left, right, check_flags=True)
345+
346+
347+
def test_assert_frame_equal_check_like_categorical_midx():
348+
# GH#48975
349+
left = DataFrame(
350+
[[1], [2], [3]],
351+
index=pd.MultiIndex.from_arrays(
352+
[
353+
pd.Categorical(["a", "b", "c"]),
354+
pd.Categorical(["a", "b", "c"]),
355+
]
356+
),
357+
)
358+
right = DataFrame(
359+
[[3], [2], [1]],
360+
index=pd.MultiIndex.from_arrays(
361+
[
362+
pd.Categorical(["c", "b", "a"]),
363+
pd.Categorical(["c", "b", "a"]),
364+
]
365+
),
366+
)
367+
tm.assert_frame_equal(left, right, check_like=True)

0 commit comments

Comments
 (0)