Skip to content

Closes #5137: bug in MultiIndex.ak.collect - #5154

Merged
ajpotts merged 1 commit into
Bears-R-Us:mainfrom
ajpotts:5137_bug_in_MultiIndex.ak.collect
Dec 18, 2025
Merged

ajpotts merged 1 commit into
Bears-R-Us:mainfrom
ajpotts:5137_bug_in_MultiIndex.ak.collect

Conversation

@ajpotts

@ajpotts ajpotts commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

PR Description: Fix IndexAccessor.collect for MultiIndex and Add Tests

This pull request improves the behavior of ArkoudaIndexAccessor.collect() when
handling pandas.MultiIndex objects, especially those with Arkouda‑backed levels.

Summary of Changes

1. Correct MultiIndex Collection Logic

Previously, collect() attempted to rebuild a MultiIndex by iterating over
idx.levels, which fails to preserve row ordering and duplicates and can break
for certain Arkouda‑backed level types.

This PR replaces that logic with:

tuples = list(idx.to_list())
return pd.MultiIndex.from_tuples(tuples, names=list(idx.names))

This ensures:

  • Correct preservation of all rows, including duplicates.
  • Correct ordering semantics.
  • Compatibility with both plain pandas and Arkouda‑backed MultiIndex levels.
  • No reliance on internal _data attributes of extension arrays.

2. New Test: test_collect_multiindex_preserves_all_rows

A parametrized test verifies behavior for both:

  • arkouda_backed = False (plain pandas MultiIndex)
  • arkouda_backed = True (Arkouda-backed MultiIndex)

The test asserts:

  • Result is always a plain pandas MultiIndex.
  • Length, names, and row tuples match exactly.
  • .equals() holds for the plain-pandas case.

3. Minor Test Cleanup

  • Adds missing pytest import.
  • Uses explicit .to_list() comparison for row-level correctness.

Motivation

The old behavior caused incorrect reconstruction of MultiIndex objects,
particularly losing duplicate rows or failing to preserve row ordering when
levels were Arkouda-backed. This surfaced as failures in new tests validating
faithful round‑trip conversion.

Impact

  • Improves correctness and stability of MultiIndex operations under Arkouda.
  • Ensures .collect() behaves predictably regardless of backend.
  • No API breakage; existing callers receive more correct results.

File Changes

  • arkouda/pandas/extension/_index_accessor.py
  • tests/pandas/extension/index_accessor.py

Closes #5137: bug in MultiIndex.ak.collect

@ajpotts
ajpotts force-pushed the 5137_bug_in_MultiIndex.ak.collect branch from c9d4482 to f70e3bc Compare December 12, 2025 17:24
@ajpotts
ajpotts marked this pull request as ready for review December 12, 2025 18:10

@drculhane drculhane 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.

Confirmed that it fixes the bug. Nicely done.

@1RyanK 1RyanK 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.

Looks good!

@ajpotts
ajpotts added this pull request to the merge queue Dec 18, 2025
Merged via the queue into Bears-R-Us:main with commit f641b85 Dec 18, 2025
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.

bug in MultiIndex.ak.collect

4 participants