Skip to content

Bug: MultiIndex .lookup() attempts illegal dtype cast for tuple keys #5155

Closed

Description

@ajpotts

Bug: MultiIndex .lookup() attempts illegal dtype cast for tuple keys

Summary

Calling .ak.lookup() on an Arkouda-backed MultiIndex with a tuple key fails with a server-side IllegalArgumentError. The current lookup implementation incorrectly treats tuple keys as homogeneous and attempts to cast every component to the same dtype, causing invalid conversions (e.g., "red" → int64).

This is a correctness bug: tuple keys must be matched level-wise without cross-dtype casting.


Steps to Reproduce

arrays = [[1, 1, 2, 3], ["red", "blue", "red", "red"]]
midx = pd.MultiIndex.from_arrays(arrays, names=["num", "color"])
ak_midx = midx.ak.to_ak()

mask = ak_midx.ak.lookup((1, "red"))

Observed Behavior

The lookup call triggers:

RuntimeError: Error executing command castStringsTo<int64>:
IllegalArgumentError: Error computing StringToNumericStrict on string or segment

Trace shows the lookup path doing:

key = [akcast(array([x]), dt) for x in key]

This causes "red" to be passed through castStringsTo<int64>.


Expected Behavior

For a MultiIndex with mixed dtypes:

  • Tuple keys should be compared per-level, not cast into a single dtype.
  • Lookup should return a boolean mask matching rows where each level equals the corresponding tuple component.

Given:

[(1, "red"), (1, "blue"), (2, "red"), (3, "red")]

lookup((1, "red")) should return:

[True, False, False, False]

Root Cause

Index.lookup assumes a single dtype applies to all components of the key. This is correct for Index but invalid for MultiIndex.

MultiIndex requires level-wise dtype handling.


Proposed Fix

  • Add a MultiIndex.lookup implementation that:
    • Accepts tuple keys of length nlevels.
    • Compares each tuple component to its corresponding level array using ==.
    • Returns the combined boolean mask.
  • Adjust Index.lookup to dispatch to MultiIndex.lookup when self is a MultiIndex.

This avoids all calls to akcast for MultiIndex tuple lookups.


Test Coverage Needed

Add tests for:

  • Mixed-dtype tuple lookup returning the correct mask.
  • Tuple of incorrect length → ValueError.
  • Non-tuple key → TypeError.

Activity

  1. added
    bug
    on Dec 12, 2025
  2. self-assigned this
    on Dec 12, 2025
  3. added 4 commits that reference this issue on Dec 12, 2025
    5cc4415
    0c0232a
    2303c7b
    11f3fe1
  4. added 2 commits that reference this issue on Jan 15, 2026
    a05d13e
    086ce45
  5. added a commit that references this issue on Jan 23, 2026
    9bedaa2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions