Skip to content

Commit 973784a

Browse files
phoflsimonjayhawkins
authored andcommitted
Backport PR pandas-dev#39204: BUG: assert_frame_equal raising TypeError with check_like and mixed dtype in Index or columns
1 parent 23e2405 commit 973784a

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.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
Series,
5858
bdate_range,
5959
)
60-
from pandas.core.algorithms import take_1d
60+
from pandas.core.algorithms import safe_sort, take_1d
6161
from pandas.core.arrays import (
6262
DatetimeArray,
6363
ExtensionArray,
@@ -804,8 +804,8 @@ def _get_ilevel_values(index, level):
804804

805805
# If order doesn't matter then sort the index entries
806806
if not check_order:
807-
left = left.sort_values()
808-
right = right.sort_values()
807+
left = Index(safe_sort(left))
808+
right = Index(safe_sort(right))
809809

810810
# MultiIndex special comparison for little-friendly error messages
811811
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)