Skip to content

GH-38983: [Python] Bug in pyarrow.from_pandas() when input has MultiIndex index columns having non-string names - #55485

Open
thisisnic wants to merge 2 commits into
apache:mainfrom
thisisnic:GH-38983-multiindex-int-names
Open

thisisnic wants to merge 2 commits into
apache:mainfrom
thisisnic:GH-38983-multiindex-int-names

Conversation

@thisisnic

@thisisnic thisisnic commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

pa.Table.from_pandas() on a DataFrame whose MultiIndex has integer level names returned the wrong data for some index levels. For example, with names=[1, 2] both index columns were named "1" and both contained the values of the first level, so the second level's data was lost.

What changes are included in this PR?

_get_index_level_values() in pandas_compat.py called index.get_level_values(i) with a position, but pandas matches an integer against level names before treating it as a position. The levels are now looked up by position regardless of their names.

Are these changes tested?

Yes

Are there any user-facing changes?

No

Was AI used for this PR?

Used Claude, asked lots of questions about why etc, did an adversarial review with roborev too

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Copilot AI balanced review requested due to automatic review settings November 10, 2026 19:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the integer-named MultiIndex conversion fix.

No open issues 🎉

What changed in this PR

Fixes incorrect index data in PyArrow’s pandas conversion when integer MultiIndex level names are mistaken for positional lookups.

Changes:

  • Makes MultiIndex level extraction positional while retaining each level’s original name.
  • Adds regression coverage for distinct level values, stringified field names, expected warnings, and pandas round-tripping.
File Description
python/​pyarrow/​tests/​test_pandas.py Adds a regression test for integer-named MultiIndex levels.
python/​pyarrow/​pandas_compat.py Uses temporary positional names to avoid ambiguous level lookups.

Review effort: Balanced

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #38983 has been automatically assigned in GitHub to PR creator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants