Skip to content

BUG: Pandas concat raises RuntimeWarning: '<' not supported between instances of 'int' and 'tuple', sort order is undefined for incomparable objects with multilevel columns #61477

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
3 tasks done
ButteryPaws opened this issue May 22, 2025 · 0 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@ButteryPaws
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

left_data = np.random.rand(1000, 5)
left_index = pd.date_range(start='20240101 09:00:00', periods=1000, freq='min')
left_columns = pd.MultiIndex.from_tuples([
    ('price', 'A'),                   # Tuple[str, str]
    ('price', 'B'),                   # Tuple[str, str]
    ('price', 'C'),                   # Tuple[str, str]
    ('diff', ('high', 'low')),        # Tuple[str, Tuple[str, str]]
    ('diff', ('open', 'close'))       # Tuple[str, Tuple[str, str]]
])
left_df = pd.DataFrame(data=left_data, index=left_index, columns=left_columns)

right_data = np.random.rand(990, 3)
right_index = pd.date_range(start='20240101 12:00:00', periods=990, freq='min')
right_columns = pd.MultiIndex.from_tuples([
    ('X', 1),
    ('X', 2),
    ('X', 3),
])
right_df = pd.DataFrame(data=right_data, columns=right_columns, index=right_index)

df1 = pd.concat([left_df, right_df], axis=1, sort=False)
print(df1)
# df2 = pd.merge(left_df, right_df, left_index=True, right_index=True)
# print(df2)
# df3 = left_df.join(right_df)
# print(df3)

Issue Description

Let's say we have two dataframes, left_df and right_df both of which have multilevel columns, or columns of type pandas.MultiIndex. The columns are of different types, in particular, one of the dataframes has a column which is of type Tuple[str, Tuple[str, str]] and the other has a column of type Tuple[str, int]. My goal is to concatenate these two dataframes along the columns. To this, I tried out using pd.concat with the axis=1 argument and experimented around with some other arguments as well.

Further experiments

  1. To avoid this warning, I tried to use pd.DataFrame.join and pd.merge as well but they all return the same warning (given in the code)
  2. I tried out the same thing with a RangeIndex instead of a DatetimeIndex and I get the same error. The error doesn't seem to depend on the indices of the dataframes.
  3. I tried the same thing with single-level columns, or using left_columns=pd.Index([('A', 'A'), 'B', 'high', 'low']) and right_columns=pd.Index([('X', 'X'), 1, 'a']) to see if this happens only when we have multilevel columns. I do not get any warnings in this case, which confirms that this is an issue only with MultiIndex columns and not regular columns.
  4. I tried various combinations of types in the MultiIndex columns and this issue doesn't arise when columns are just of type Tuple[str, str] and Tuple[str, int] or Tuple[str, Tuple[str, str]]. It might happen with other pairs of data types as well, I haven't checked all combinations.

Expected Behavior

It is hard to understand why is any kind of a sorting operations performed in this case. There should not be any instance of comparison. There is some common low level function being called in all 3 of concat, merge and join which is comparing the column values. It is expected that no warning be thrown in this case. To reproduce the output one gets (handling the correct type of join) without a warning, the code which can be run is:

new_index = pd.MultiIndex.from_tuples(np.concatenate([left_df.keys(), right_df.keys()]))
df = pd.concat([left_df, right_df], axis=1, sort=False, ignore_index=True).reindex(new_index, axis=1)

Source

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.3
python-bits : 64
OS : Linux
OS-release : 6.8.0-1026-aws
Version : #28-Ubuntu SMP Mon Mar 24 19:32:19 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 2.1.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 25.1.1
Cython : None
sphinx : None
IPython : 8.29.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.9.2
numba : 0.61.0
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : 1.4.6
pyarrow : 19.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : 2.0.36
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@ButteryPaws ButteryPaws added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant