Skip to content

BUG: Inconsistent behaviour when merging reset-ed MultiIndex dataframe #62150

@renkeven

Description

@renkeven

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

df = pd.DataFrame(data={("column_1", ""): [1,1], ("column_2", ""): [2,2]})
df.index = pd.MultiIndex.from_arrays([[1,1], ["metadata_1", "metadata_2"]], names=["index", "metadata"])

df2 = pd.DataFrame(data=[1,1], index=[1,1]).rename_axis("index", axis=0)
df2.columns = pd.MultiIndex.from_product([["new_data"], [""]])

df.reset_index().merge(df2.reset_index(), on="index")
# IndexError: Requested axis not found in manager

df.reset_index().merge(df2.reset_index(), on=[("index", "")])
# IndexError: Requested axis not found in manager

df.reset_index().columns
# MultiIndex([(   'index', ''),
#             ('metadata', ''),
#             ('column_1', ''),
#             ('column_2', '')],
#            )

df2.reset_index().columns
# MultiIndex([(   'index', ''),
#             ('new_data', '')],
#            )

## We have a workaround if we force df2 to start as a multi-index, then it works
df2 = pd.DataFrame(data=[1,1], index=[1,1]).rename_axis("index", axis=0)
df2.columns = pd.MultiIndex.from_product([["new_data"], [""]])
df2.index = pd.MultiIndex.from_arrays(
        [df2.index, ["dummy"] * len(df2)],
        names=["index", "dummy_index"],
    )

# Merge works
df.reset_index().merge(df2.reset_index(), on=[("index", "")])

# Extra empty multi-index column
df2.reset_index().columns
# MultiIndex([(   'index', ''),
#            ('dummy_index', ''),
#            ('new_data', '')],
#           )

Issue Description

I have a dataframe (main) with multi-indexed index and columns. I would like to do a merge with a second dataframe (side) that is single-index and single column.

I coerced (side) to give it the same levels in the columns, and do a pd.merge on (main).reset_index and side.reset_index.

The example works if you make (side) also a multi-index index.

Additionally, the first example above is fine on 2.2.3, but is broken on 2.3.1

Expected Behavior

Usual merge behaviour. Not sure why the initial dataframe must both have multi-index indexes when we use .reset_index() on both and merge over a single column

Installed Versions

INSTALLED VERSIONS

commit : c888af6
python : 3.12.11
python-bits : 64
OS : Linux
OS-release : 6.6.93+
Version : #1 SMP PREEMPT_DYNAMIC Fri Jun 27 09:03:39 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 8.36.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.3.2
html5lib : None
hypothesis : None
gcsfs : 2025.3.2
jinja2 : 3.1.6
lxml.etree : 6.0.0
matplotlib : 3.10.5
numba : 0.61.2
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : 2.9.9
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : 8.3.5
python-calamine : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.16.0
sqlalchemy : 2.0.42
tables : None
tabulate : 0.9.0
xarray : 2025.1.2
xlrd : 2.0.1
xlsxwriter : 3.2.3
zstandard : 0.23.0
tzdata : 2025.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