Skip to content

fix: warn when obs/var column names differ only by case - #2668

Open
kokhlo wants to merge 3 commits into
scverse:mainfrom
kokhlo:fix/zarr-case-columns-2576
Open

kokhlo wants to merge 3 commits into
scverse:mainfrom
kokhlo:fix/zarr-case-columns-2576

Conversation

@kokhlo

@kokhlo kokhlo commented Sep 29, 2026

Copy link
Copy Markdown

Closes #2576.

Diagnosis

write_zarr stores every obs/var column as a zarr member named after the column. When two column names differ only by case (Study and study), that only works on a case-sensitive filesystem. On a case-insensitive one — macOS' default APFS, Windows, exFAT — both names address the same member, so the second write silently overwrites the first. The consolidated metadata then only records the name that won, while column-order still lists both, and read_zarr dies inside zarr while resolving the missing name:

KeyError: "'obs/study/categories' not found in consolidated metadata."

I reproduced this on both sides of the filesystem boundary to confirm the mechanism, so this is not the pandas concat / axis-name collision the traceback suggests:

store obs/foo in consolidated metadata read_zarr
case-insensitive APFS absent KeyError: "'foo' not found in consolidated metadata."
case-sensitive APFS present OK, both columns roundtrip

Same result across zarr 3.1.3, 3.2.1, 3.3.0 and 3.4.0, so no zarr version fixes it — the collision happens in the store, below zarr's view. consolidate_metadata=False only masks it: the metadata is no longer consulted, so the surviving column reads back fine while the overwritten one is silently gone. .h5ad is unaffected in both directions, since HDF5 keys are not filesystem paths.

Fix

write_dataframe now warns when the columns it is about to write collide under str.casefold(), for zarr groups only:

Found column names in 'obs' that differ only by case: [['study', 'Study']]. Writing to a
case-insensitive filesystem will make one overwrite the other, and the resulting store cannot
be read back. Rename the columns, or write to a case-sensitive filesystem.

A warning rather than an error, because the destination decides whether the names actually collide: the same write_zarr call is correct on ext4 or in an S3-backed store, and only becomes lossy on a case-insensitive volume. Failing there would break working code, and the data is already unrecoverable at that point, so the useful moment to say something is on write.

Tests

test_dataframe_columns_differing_by_case covers both formats: the zarr case asserts the warning and that the frame still roundtrips, the h5ad case asserts silence, since only the zarr member layout is at risk. test_dataframe_columns_differing_by_case_dont_warn guards against over-firing on names that are merely a prefix of one another (study / StudyID).

@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2668      +/-   ##
==========================================
- Coverage   87.62%   85.81%   -1.82%     
==========================================
  Files          49       49              
  Lines        8129     8140      +11     
==========================================
- Hits         7123     6985     -138     
- Misses       1006     1155     +149     
Files with missing lines Coverage Δ
src/anndata/_io/specs/methods.py 91.80% <100.00%> (-0.19%) ⬇️

... and 7 files with indirect coverage changes

@kokhlo kokhlo changed the title Fix read_zarr with obs columns differing only by case fix: warn when obs/var column names differ only by case Sep 30, 2026
@cvduffy

cvduffy commented Oct 9, 2026

Copy link
Copy Markdown

if I understand correctly, the issue raised in #2576 also occurs on case-sensitive filesystems - see https://github.com/d-v-b/zarr-python/pull/273

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read_zarr fails with obs columns that differ only by case

2 participants