Skip to content

fix: make_index_unique with a categorical index - #2686

Open
raashish1601 wants to merge 4 commits into
scverse:mainfrom
raashish1601:fix/1670-make-index-unique-categorical
Open

raashish1601 wants to merge 4 commits into
scverse:mainfrom
raashish1601:fix/1670-make-index-unique-categorical

Conversation

@raashish1601

@raashish1601 raashish1601 commented Oct 7, 2026 •

Copy link
Copy Markdown

make_index_unique (and so var_names_make_unique / obs_names_make_unique) fails when the index is a CategoricalIndex with duplicates, because the new names are written into a Categorical that does not have them as categories:

>>> ad.utils.make_index_unique(pd.CategoricalIndex(["a", "b", "a", "c", "b"]))
TypeError: Cannot setitem on a Categorical with a new category (a-1), set the categories first

Still reproduces with anndata 0.12.19. The index is now cast to the dtype of its categories first, so it gets the same treatment as a plain string index (['a', 'b', 'a-1', 'c', 'b-1'], name kept). A categorical index with no duplicates is still returned unchanged.

Added test_make_index_unique_categorical to tests/test_utils.py; it fails without the change. tests/test_utils.py passes locally, and ruff check / format are clean. Release note added in docs/release-notes/2686.fix.md.

@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.83%. Comparing base (e90df01) to head (fd83cbf).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2686      +/-   ##
==========================================
- Coverage   87.65%   85.83%   -1.83%     
==========================================
  Files          49       49              
  Lines        8167     8169       +2     
==========================================
- Hits         7159     7012     -147     
- Misses       1008     1157     +149     
Files with missing lines Coverage Δ
src/anndata/utils.py 84.61% <100.00%> (-0.66%) ⬇️

... and 7 files with indirect coverage changes

@raashish1601

Copy link
Copy Markdown
Author

The test jobs pass. The benchmark job fails in asv with ValueError: invalid option: reset(base=...), which also happens in the Benchmark run on main from 2026-10-05, so it looks unrelated. The two Triage checks are waiting for labels/milestone.

@ilan-gold ilan-gold added this to the 0.13.5 milestone Oct 17, 2026

@ilan-gold ilan-gold 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.

Hi!

  1. Can you please migrate to our new PR template and fill it out correctly so the CI passes?
  2. Can you post a reproducer, which the original issue does not have?

@flying-sheep flying-sheep modified the milestones: 0.13.5, 0.14.1 Dec 4, 2026

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicates in index throw error during make_index_unique

3 participants