pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
42.58k stars 17.57k forks source link

BUG: joining dataframes with multi-index and None index label results in AssertionError #58721

Open nemausus opened 1 month ago

nemausus commented 1 month ago

Pandas version checks

Reproducible Example

import pandas as pd
df1 = pd.DataFrame({"A": [1]}, index=pd.MultiIndex.from_tuples([(3, 3)], names=["X", None]))
df2 = pd.DataFrame({"B": [1]}, index=pd.MultiIndex.from_tuples([(3, 3)], names=[None, "X"]))
df1.join(df2)

Issue Description

Fails with error: AssertionError: Length of order must be same as number of levels (2), got 1

Expected Behavior

Should not result in AssertionError

Installed Versions

INSTALLED VERSIONS ------------------ commit : bdc79c146c2e32f2cab629be240f01658cfb6cc2 python : 3.9.18.final.0 python-bits : 64 OS : Darwin OS-release : 23.4.0 Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.2.1 numpy : 1.26.4 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 68.2.2 pip : 23.3.1 Cython : None pytest : 7.4.4 hypothesis : None sphinx : 5.0.2 blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.3 IPython : 8.15.0 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.12.2 bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : 15.0.1 pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.13.0 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2024.1 qtpy : 2.4.1 pyqt5 : None
SiddheshBangar commented 1 month ago

Hey @nemausus, I just looked into your issue and the problem that i found is that the join method is trying to align the MultiIndex levels of the two DataFrames, but the names of the index levels are not aligned correctly.

>>> df1
     A
X     
3 3  1
>>> df2
     B
  X   
3 3  1

So it gets confused because the index levels names do not match, even though the structure of the indices is the same.

So if the Data frames have the same names for their multiindex levels this issue can be fixed is what I think, as I tried to test it this is what I found:

>>> df2 = pd.DataFrame({"B": [1]}, index=pd.MultiIndex.from_tuples([(3, 3)], names=[None, "X"]))
>>> df1 = pd.DataFrame({"A": [1]}, index=pd.MultiIndex.from_tuples([(3, 3)], names=[None, "X"]))
>>> df1.join(df2)
     A  B
  X      
3 3  1  1

Let me know if this solves your issue or I can look more further into it, thanks.

rhshadrach commented 1 month ago

@SiddheshBangar - this raises an AssertionError - those shouldn't be exposed to the user (only things like ValueError, TypeError, etc). So this definitely looks like a bug. Also, if you replace None with "Y", then the operation is successful.

SiddheshBangar commented 1 month ago

Hey @rhshadrach, so you mean to say that the error message should not be prompt or either error message can be fixed somehow and replace it with assertion error message.

rhshadrach commented 1 month ago

At a glance, it seems to me that this should not raise an error. If implementation difficulties make that difficult, then perhaps we can look into other possibilities. Further investigations are welcome!

SiddheshBangar commented 1 month ago

Okay, cool thanks @rhshadrach this looks a bit confusing to me at this point but I will try to figure out what the best can I do, if you have any extra information to share feel free to drop down that can help me to resolve this issue, Thanks

matthewacalder commented 1 month ago

It looks like the issue is at https://github.com/pandas-dev/pandas/blob/b162331554d7c7f6fd46ddde1ff3908f2dc8bcce/pandas/core/indexes/base.py#L4585

# maintain the order of the index levels
if how == "right":
    level_order = other_names_list + ldrop_names
else:
    level_order = self_names_list + rdrop_names
multi_join_idx = multi_join_idx.reorder_levels(level_order)

Null index names are removed from other_names_list and self_names_list for upstream operations and then also as MultiIndex.reorder_levels() accepts a list of str or int (not None). Therefore, length of level_order becomes one less than index length. Note that if the above lines are removed (as per Pandas==2.2.2) then this issue no longer exists.
Perhaps MultiIndex.reorder_levels() could be extended to accept None type also, and level_order can be updated to include None if existing?