Skip to content

Commit 4efa440

Browse files
phoflsimonjayhawkins
authored andcommitted
BUG: assert_index_equal ignoring names when check_order is false (pandas-dev#47330)
* BUG: assert_index_equal ignoring names when check_order is false * Update doc/source/whatsnew/v1.5.0.rst Co-authored-by: Simon Hawkins <simonjayhawkins@gmail.com> * Move index creation to else block * Remove dtypes Co-authored-by: Simon Hawkins <simonjayhawkins@gmail.com>
1 parent 8094d86 commit 4efa440

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ Other
970970

971971
.. ***DO NOT USE THIS SECTION***
972972
973-
-
973+
- Bug in :func:`.assert_index_equal` with ``names=True`` and ``check_order=False`` not checking names (:issue:`47328`)
974974
-
975975

976976
.. ---------------------------------------------------------------------------

pandas/_testing/asserters.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@
4545
Series,
4646
TimedeltaIndex,
4747
)
48-
from pandas.core.algorithms import (
49-
safe_sort,
50-
take_nd,
51-
)
48+
from pandas.core.algorithms import take_nd
5249
from pandas.core.arrays import (
5350
DatetimeArray,
5451
ExtensionArray,
@@ -58,6 +55,7 @@
5855
)
5956
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
6057
from pandas.core.arrays.string_ import StringDtype
58+
from pandas.core.indexes.api import safe_sort_index
6159

6260
from pandas.io.formats.printing import pprint_thing
6361

@@ -367,8 +365,8 @@ def _get_ilevel_values(index, level):
367365

368366
# If order doesn't matter then sort the index entries
369367
if not check_order:
370-
left = Index(safe_sort(left), dtype=left.dtype)
371-
right = Index(safe_sort(right), dtype=right.dtype)
368+
left = safe_sort_index(left)
369+
right = safe_sort_index(right)
372370

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

pandas/core/indexes/api.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"get_unanimous_names",
7171
"all_indexes_same",
7272
"default_index",
73+
"safe_sort_index",
7374
]
7475

7576

@@ -157,23 +158,42 @@ def _get_combined_index(
157158
index = ensure_index(index)
158159

159160
if sort:
160-
try:
161-
array_sorted = safe_sort(index)
162-
array_sorted = cast(np.ndarray, array_sorted)
163-
if isinstance(index, MultiIndex):
164-
index = MultiIndex.from_tuples(array_sorted, names=index.names)
165-
else:
166-
index = Index(array_sorted, name=index.name)
167-
except TypeError:
168-
pass
169-
161+
index = safe_sort_index(index)
170162
# GH 29879
171163
if copy:
172164
index = index.copy()
173165

174166
return index
175167

176168

169+
def safe_sort_index(index: Index) -> Index:
170+
"""
171+
Returns the sorted index
172+
173+
We keep the dtypes and the name attributes.
174+
175+
Parameters
176+
----------
177+
index : an Index
178+
179+
Returns
180+
-------
181+
Index
182+
"""
183+
try:
184+
array_sorted = safe_sort(index)
185+
except TypeError:
186+
pass
187+
else:
188+
array_sorted = cast(np.ndarray, array_sorted)
189+
if isinstance(index, MultiIndex):
190+
index = MultiIndex.from_tuples(array_sorted, names=index.names)
191+
else:
192+
index = Index(array_sorted, name=index.name, dtype=index.dtype)
193+
194+
return index
195+
196+
177197
def union_indexes(indexes, sort: bool | None = True) -> Index:
178198
"""
179199
Return the union of indexes.

pandas/tests/util/test_assert_index_equal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ def test_index_equal_range_categories(check_categorical, exact):
238238
)
239239

240240

241+
def test_assert_index_equal_different_names_check_order_false():
242+
# GH#47328
243+
idx1 = Index([1, 3], name="a")
244+
idx2 = Index([3, 1], name="b")
245+
with pytest.raises(AssertionError, match='"names" are different'):
246+
tm.assert_index_equal(idx1, idx2, check_order=False, check_names=True)
247+
248+
241249
def test_assert_index_equal_mixed_dtype():
242250
# GH#39168
243251
idx = Index(["foo", "bar", 42])

0 commit comments

Comments
 (0)