Skip to content

Commit c5b4272

Browse files
authored
BUG: assert_frame_equal raising TypeError with check_like and mixed dtype in Index or columns (#39204)
1 parent f51547c commit c5b4272

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

doc/source/whatsnew/v1.2.1.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929
- Fixed regression in :meth:`DataFrameGroupBy.diff` raising for ``int8`` and ``int16`` columns (:issue:`39050`)
3030
- Fixed regression in :meth:`Series.fillna` that raised ``RecursionError`` with ``datetime64[ns, UTC]`` dtype (:issue:`38851`)
3131
- Fixed regression that raised ``AttributeError`` with PyArrow versions [0.16.0, 1.0.0) (:issue:`38801`)
32+
- Fixed regression in :func:`pandas.testing.assert_frame_equal` raising ``TypeError`` with ``check_like=True`` when :class:`Index` or columns have mixed dtype (:issue:`39168`)
3233
- Fixed regression in :meth:`DataFrame.groupby` when aggregating an :class:`ExtensionDType` that could fail for non-numeric values (:issue:`38980`)
3334
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``KeyError`` with :class:`MultiIndex` and list-like columns indexer enlarging :class:`DataFrame` (:issue:`39147`)
3435
- Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`)
@@ -42,7 +43,7 @@ Bug fixes
4243

4344
- Bug in :meth:`read_csv` with ``float_precision="high"`` caused segfault or wrong parsing of long exponent strings. This resulted in a regression in some cases as the default for ``float_precision`` was changed in pandas 1.2.0 (:issue:`38753`)
4445
- Bug in :func:`read_csv` not closing an opened file handle when a ``csv.Error`` or ``UnicodeDecodeError`` occurred while initializing (:issue:`39024`)
45-
-
46+
- Bug in :func:`pandas.testing.assert_index_equal` raising ``TypeError`` with ``check_order=False`` when :class:`Index` has mixed dtype (:issue:`39168`)
4647

4748
.. ---------------------------------------------------------------------------
4849

pandas/_testing/asserters.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
Series,
3030
TimedeltaIndex,
3131
)
32-
from pandas.core.algorithms import take_1d
32+
from pandas.core.algorithms import safe_sort, take_1d
3333
from pandas.core.arrays import (
3434
DatetimeArray,
3535
ExtensionArray,
@@ -344,8 +344,8 @@ def _get_ilevel_values(index, level):
344344

345345
# If order doesn't matter then sort the index entries
346346
if not check_order:
347-
left = left.sort_values()
348-
right = right.sort_values()
347+
left = Index(safe_sort(left))
348+
right = Index(safe_sort(right))
349349

350350
# MultiIndex special comparison for little-friendly error messages
351351
if left.nlevels > 1:

pandas/tests/util/test_assert_frame_equal.py

+6
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,9 @@ def test_allows_duplicate_labels():
299299

300300
with pytest.raises(AssertionError, match="<Flags"):
301301
tm.assert_frame_equal(left, right)
302+
303+
304+
def test_assert_frame_equal_columns_mixed_dtype():
305+
# GH#39168
306+
df = DataFrame([[0, 1, 2]], columns=["foo", "bar", 42], index=[1, "test", 2])
307+
tm.assert_frame_equal(df, df, check_like=True)

pandas/tests/util/test_assert_index_equal.py

+6
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,9 @@ def test_index_equal_category_mismatch(check_categorical):
192192
tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical)
193193
else:
194194
tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical)
195+
196+
197+
def test_assert_index_equal_mixed_dtype():
198+
# GH#39168
199+
idx = Index(["foo", "bar", 42])
200+
tm.assert_index_equal(idx, idx, check_order=False)

0 commit comments

Comments
 (0)