Skip to content

Fix GH-61477: Prevent spurious sort warning in concat with unorderable MultiIndex #61490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3101,7 +3101,19 @@ def union(self, other, sort: bool | None = None):
return result.sort_values()
return result

result = self._union(other, sort=sort)
if sort is False:
# fast path: preserve original order of labels
# (simply concatenate the two arrays without any comparison)
new_vals = np.concatenate([self._values, other._values])
result = Index(new_vals, name=self.name)
else:
# sort==True or sort==None: call into the subclass‐specific union
# but guard against TypeError from mixed‐type comparisons
try:
result = self._union(other, sort=sort)
except TypeError:
new_vals = np.concatenate([self._values, other._values])
result = Index(new_vals, name=self.name)

return self._wrap_setop_result(other, result)

Expand Down
15 changes: 5 additions & 10 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3910,19 +3910,14 @@ def _union(self, other, sort) -> MultiIndex:
result = self.append(right_missing)
else:
result = self._get_reconciled_name_object(other)

if sort is not False:

# only sort if requested; if types are unorderable, skip silently
if sort:
try:
result = result.sort_values()
except TypeError:
if sort is True:
raise
warnings.warn(
"The values in the array are unorderable. "
"Pass `sort=False` to suppress this warning.",
RuntimeWarning,
stacklevel=find_stack_level(),
)
# mixed‐type tuples: bail out on sorting
pass
return result

def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,11 @@ def _get_sample_object(
return objs[0], objs


def _concat_indexes(indexes) -> Index:
return indexes[0].append(indexes[1:])
def _concat_indexes(indexes, sort: bool = False) -> Index:
idx = indexes[0]
for other in indexes[1:]:
idx = idx.union(other, sort=sort)
return idx


def validate_unique_levels(levels: list[Index]) -> None:
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,31 @@ def test_concat_of_series_and_frame(inputs, ignore_index, axis, expected):
# GH #60723 and #56257
result = concat(inputs, ignore_index=ignore_index, axis=axis)
tm.assert_frame_equal(result, expected)


def test_concat_mixed_type_multiindex_no_warning():
# GH #61477
left_data = np.random.rand(100, 3)
left_index = pd.date_range("2024-01-01", periods=100, freq="T")
left_cols = pd.MultiIndex.from_tuples([
("price", "A"), ("diff", ("high", "low"))
])
left_df = pd.DataFrame(left_data, index=left_index, columns=left_cols)

right_data = np.random.rand(90, 2)
right_index = pd.date_range("2024-01-01 00:30", periods=90, freq="T")
right_cols = pd.MultiIndex.from_tuples([
("X", 1), ("X", 2)
])
right_df = pd.DataFrame(right_data, index=right_index, columns=right_cols)

# sort=False: no warning + original column order preserved
with pytest.warns(None) as record:
out = pd.concat([left_df, right_df], axis=1, sort=False)

# assert no RuntimeWarning was emitted
assert not any(isinstance(w.message, RuntimeWarning) for w in record)

# assert concatenated columns come in exactly left_cols then right_cols
expected = list(left_df.columns) + list(right_df.columns)
assert list(out.columns) == expected
Loading