Skip to content

Commit

Permalink
WARN: sort_values for midx warns unnecessarily if nan is in level (#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Oct 4, 2023
1 parent eb6317a commit 6a8055d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ def _sort_levels_monotonic(self, raise_if_incomparable: bool = False) -> MultiIn
# indexer to reorder the level codes
indexer = ensure_platform_int(indexer)
ri = lib.get_reverse_indexer(indexer, len(indexer))
level_codes = algos.take_nd(ri, level_codes)
level_codes = algos.take_nd(ri, level_codes, fill_value=-1)

new_levels.append(lev)
new_codes.append(level_codes)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/multi/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,12 @@ def test_sort_values_with_na_na_position(dtype, na_position):
]
expected = MultiIndex.from_arrays(arrays)
tm.assert_index_equal(result, expected)


def test_sort_unnecessary_warning():
# GH#55386
midx = MultiIndex.from_tuples([(1.5, 2), (3.5, 3), (0, 1)])
midx = midx.set_levels([2.5, np.nan, 1], level=0)
result = midx.sort_values()
expected = MultiIndex.from_tuples([(1, 3), (2.5, 1), (np.nan, 2)])
tm.assert_index_equal(result, expected)

0 comments on commit 6a8055d

Please sign in to comment.