Registry Mirror
Skip to content

Conversation

@headtr1ck

@headtr1ck headtr1ck commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Description

PandasMultiIndex.stack passed sortorder=0 to pandas, which claims that the index is not lexsorted at all. Pandas trusts it, so slicing a stacked dimension with tuples failed:

da.stack(z=["x", "y"]).sel(z=slice((0, "b"), (1, "a")))  # UnsortedIndexError
  • Don't pass sortorder, so that pandas computes the lexsort depth when needed
  • This also avoids pandas verifying sortorder eagerly when stacking, which copied all codes to int64

Why it was 0: sortorder=0 was added in #985 (fixing #980) to a helper that built the codes with np.meshgrid, without an explanation in the code or the PR. #5692 moved it into PandasMultiIndex.stack and #9522 copied it to the new from_product branch. In pandas, sortorder is the lexsort depth, the number of leading levels the codes are sorted by, so 0 means "not sorted" rather than e.g. "sorted from level 0". Stacked codes are always fully lexsorted, also for decreasing coordinates (unsorted levels, the case from #980), and pandas slices those correctly by their codes, which the new test checks.

Follow-up of #11688 (comment).

Checklist

  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code

[This is Claude Code on behalf of Michael Niklas]

🤖 Generated with Claude Code

PandasMultiIndex.stack passed sortorder=0 to pandas, i.e. that the codes are
lexsorted by none of the levels. Pandas trusts it instead of computing the
lexsort depth, so selecting a slice of tuples from a stacked dimension raised
an UnsortedIndexError. Don't pass sortorder, so that pandas computes it when
needed.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck headtr1ck added the run-benchmark Run the ASV benchmark workflow label Oct 8, 2026
@headtr1ck

Copy link
Copy Markdown
Collaborator Author

I enabled the benchmark just to be sure. But basically what I expect to happen is that instead of pandas verifying the sortorder during stack it is now lazily assumed and will instead be evaluated during e.g. the first call to sel. So faster in some cases and the same in others.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Some benchmarks got better. To infinity and beyond!

Change Before [cb43409] After [36009ff] Ratio Benchmark (Parameter)
🚀 2.29±0.06ms 1.39±0.03ms 0.61 unstacking.UnstackingExtraDim.time_unstack_fast
🚀 414±100μs 269±7μs 0.65 unstacking.UnstackingSparse.time_unstack_to_sparse_2d

🚀 better · 🐌 worse · 💥 now failing · 🩹 fixed · 🤷 not comparable

Comparing 36009ff9 (this PR merged into the base branch) with cb434090 (base branch).
Only changes by more than a factor of 1.5 are shown, see the workflow run for all results.

@headtr1ck headtr1ck added the plan to merge Final call for comments label Oct 8, 2026
@dcherian
dcherian enabled auto-merge (squash) October 8, 2026 21:49
@dcherian
dcherian merged commit d4bce83 into pydata:main Oct 8, 2026
46 checks passed
@headtr1ck
headtr1ck deleted the stack-sortorder branch October 9, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments run-benchmark Run the ASV benchmark workflow topic-indexing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants