From 65768e1b73c4105171ab5201809925046baeca89 Mon Sep 17 00:00:00 2001 From: Marius Lange Date: Tue, 9 Jun 2026 16:36:58 +0200 Subject: [PATCH] Fix read-only error in NaN reorder test under pandas >=3 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) --- tests/model/test_obs_beautifier.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/model/test_obs_beautifier.py b/tests/model/test_obs_beautifier.py index 8f8faa8..7e21879 100644 --- a/tests/model/test_obs_beautifier.py +++ b/tests/model/test_obs_beautifier.py @@ -69,9 +69,14 @@ def test_reorder_categories_with_nan_values(self, cell_annotator_single): adata = cell_annotator.adata # Set up initial annotations with a NaN value - adata.obs["leiden"] = adata.obs["leiden"].map({"0": "B cells", "1": "T cells"}).astype("category") - # Add a NaN value - adata.obs.loc[adata.obs.index[0], "leiden"] = np.nan + leiden = adata.obs["leiden"].map({"0": "B cells", "1": "T cells"}).astype("category") + # Inject a NaN via a writable copy + full-column reassignment rather than an in-place + # `.loc` write. anndata subsetting (here sc.pp.filter_cells in the fixture) leaves obs + # backing arrays read-only, and pandas >=3 refuses in-place mutation of them + # ("ValueError: assignment destination is read-only"). Reassigning the column avoids it. + leiden = leiden.copy() + leiden.iloc[0] = np.nan + adata.obs["leiden"] = leiden nan_count_before = adata.obs["leiden"].isna().sum() assert nan_count_before > 0