You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
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)
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.
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.
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:
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
Issue Description
Let's say we have two dataframes,
left_df
andright_df
both of which have multilevel columns, or columns of typepandas.MultiIndex
. The columns are of different types, in particular, one of the dataframes has a column which is of typeTuple[str, Tuple[str, str]]
and the other has a column of typeTuple[str, int]
. My goal is to concatenate these two dataframes along the columns. To this, I tried out usingpd.concat
with theaxis=1
argument and experimented around with some other arguments as well.Further experiments
pd.DataFrame.join
andpd.merge
as well but they all return the same warning (given in the code)RangeIndex
instead of aDatetimeIndex
and I get the same error. The error doesn't seem to depend on the indices of the dataframes.left_columns=pd.Index([('A', 'A'), 'B', 'high', 'low'])
andright_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 withMultiIndex
columns and not regular columns.MultiIndex
columns and this issue doesn't arise when columns are just of typeTuple[str, str]
andTuple[str, int]
orTuple[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
andjoin
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: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
The text was updated successfully, but these errors were encountered: