Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork18.5k
Description
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
- To avoid this warning, I tried to use
pd.DataFrame.join
andpd.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 aDatetimeIndex
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'])
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. - I tried various combinations of types in the
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 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)
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