Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Labels
BugNeeds TriageIssue that has not been reviewed by a pandas team member
@ButteryPaws

Description

@ButteryPaws

Pandas version checks

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

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

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

Reproducible Example

importpandasaspdimportnumpyasnpleft_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 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

  1. To avoid this warning, I tried to usepd.DataFrame.join andpd.merge as well but they all return the same warning (given in the code)
  2. I tried out the same thing with aRangeIndex instead of aDatetimeIndex 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 usingleft_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.
  4. I tried various combinations of types in theMultiIndex 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 ofconcat,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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp