Fix GH-61477: Prevent spurious sort warning in concat with unorderable MultiIndex #61490
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix GH-61477: Stop Spurious Warning When
concat(..., sort=False)
on Mixed-TypeMultiIndex
Overview
When you do something like:
and your two DataFrames have MultiIndex columns that mix tuples and integers, pandas used to try to sort those labels under the hood. Since Python cannot compare tuple < int, you’d see:
This warning is confusing, and worse, you explicitly asked not to sort (sort=False), so pandas should never even try.
What Changed
Before: Even with sort=False, pandas would call its normal union logic, which might attempt to compare labels.
Now: If you pass sort=False, we simply concatenate the two index arrays with:
and wrap that in a new Index. No comparisons, no warnings, and your original order is preserved.
Before: pandas would call
result.sort_values()
when sort wasn’t False, and if labels were unorderable it would warn you.Now: We only call
sort_values()
when sort is truthy (True), and we wrap it in atry/except
TypeError that silently falls back to the existing order on failure. No warning is emitted.A pytest test reproduces the original bug scenario, concatenating two small DataFrames with mixed-type MultiIndex columns and
sort=False.
The test asserts:No RuntimeWarning is raised
Column order is exactly “first DataFrame’s columns, then second DataFrame’s columns”
Respects sort=False: If a user explicitly disables sorting, pandas won’t try.
Silences spurious warnings: No more confusing messages about comparing tuples to ints.
Keeps existing behavior for sort=True: You still get a sort or a real error if the labels truly can’t be ordered.
For testing we can try