Skip to content

Fix read-only error in NaN reorder test under pandas >=3#85

Merged
Marius1311 merged 1 commit into
mainfrom
Marius1311/fix-nan-test-readonly-pandas3
Jun 9, 2026
Merged

Fix read-only error in NaN reorder test under pandas >=3#85
Marius1311 merged 1 commit into
mainfrom
Marius1311/fix-nan-test-readonly-pandas3

Conversation

@Marius1311

Copy link
Copy Markdown
Member

What

Fix test_reorder_categories_with_nan_values, which fails only in the py3.14-pre (PRE-RELEASE DEPENDENCIES) CI job with:

ValueError: assignment destination is read-only

Root cause

The test injected a NaN with an in-place write:

adata.obs.loc[adata.obs.index[0], "leiden"] = np.nan

The fixture (get_example_data) runs sc.pp.filter_cells, which subsets the AnnData; anndata leaves the resulting obs backing arrays read-only. pandas >= 3 strictly refuses in-place mutation of read-only arrays, so the assignment raises. (On pandas 2.x the in-place write silently copied, so it passed — hence this only surfaced once a pandas 3 pre-release entered the pre-release matrix.)

This is a real forward-compatibility issue with the test's mutation pattern, not a masking workaround. The package code itself is unaffected — reorder_categories reassigns whole columns via cat.set_categories.

Fix

Inject the NaN on a writable copy and reassign the whole column:

leiden = adata.obs["leiden"].map({"0": "B cells", "1": "T cells"}).astype("category")
leiden = leiden.copy()
leiden.iloc[0] = np.nan
adata.obs["leiden"] = leiden

Test intent (reorder must preserve NaNs) is unchanged.

Verification

Reproduced faithfully (AnnData + sc.pp.filter_cells + the assignment) on pandas 3.0.3: the old pattern raises assignment destination is read-only; the new pattern succeeds (nan_count == 1). CI on this PR exercises the real py3.14-pre job.

🤖 Generated with Claude Code

test_reorder_categories_with_nan_values injected a NaN via an in-place
`adata.obs.loc[idx, "leiden"] = np.nan`. The fixture builds its AnnData
and runs sc.pp.filter_cells, which subsets the object and leaves obs
backing arrays read-only. pandas >=3 strictly refuses in-place mutation
of read-only arrays, so the assignment raised
"ValueError: assignment destination is read-only" (seen only in the
py3.14-pre / pre-release-deps CI job).

Inject the NaN on a writable copy and reassign the whole column, which
avoids the in-place write. The test's intent (reorder_categories must
preserve NaNs) is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.75%. Comparing base (a8872da) to head (65768e1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #85      +/-   ##
==========================================
- Coverage   80.83%   80.75%   -0.08%     
==========================================
  Files          15       15              
  Lines        1372     1372              
==========================================
- Hits         1109     1108       -1     
- Misses        263      264       +1     

see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Marius1311 Marius1311 merged commit 9258b17 into main Jun 9, 2026
9 checks passed
@Marius1311 Marius1311 deleted the Marius1311/fix-nan-test-readonly-pandas3 branch June 9, 2026 14:46
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.

1 participant