From 6a4813085a0fe78ed4b5fd7eb1610f8ec904a356 Mon Sep 17 00:00:00 2001 From: Luca Marconato Date: Fri, 13 Feb 2026 15:02:48 +0100 Subject: [PATCH] fix table model instance_key can be categorical --- src/spatialdata/models/models.py | 3 ++- tests/models/test_models.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/spatialdata/models/models.py b/src/spatialdata/models/models.py index c8001a84..39b9a60e 100644 --- a/src/spatialdata/models/models.py +++ b/src/spatialdata/models/models.py @@ -1045,13 +1045,14 @@ def _validate_table_annotation_metadata(cls, data: AnnData) -> None: np.int64, np.uint64, "O", + "category", ] and not pd.api.types.is_string_dtype(data.obs[attr[cls.INSTANCE_KEY]]) or (dtype == "O" and (val_dtype := type(data.obs[attr[cls.INSTANCE_KEY]].iloc[0])) is not str) ): dtype = dtype if dtype != "O" else val_dtype raise TypeError( - f"Only int, np.int16, np.int32, np.int64, uint equivalents or string allowed as dtype for " + f"Only int, np.int16, np.int32, np.int64, uint equivalents, categorical or string allowed as dtype for " f"instance_key column in obs. Dtype found to be {dtype}" ) expected_regions = attr[cls.REGION_KEY] if isinstance(attr[cls.REGION_KEY], list) else [attr[cls.REGION_KEY]] diff --git a/tests/models/test_models.py b/tests/models/test_models.py index e2087ace..d404ea3a 100644 --- a/tests/models/test_models.py +++ b/tests/models/test_models.py @@ -417,6 +417,14 @@ def test_table_model( adata = AnnData(RNG.normal(size=(10, 2)), obs=obs) table = model.parse(adata, region=region, region_key=region_key, instance_key="A") assert region_key in table.obs + + # categorical instance_key should be accepted + obs_cat = obs.copy() + obs_cat["A"] = pd.Categorical(obs_cat["A"]) + adata_cat = AnnData(RNG.normal(size=(10, 2)), obs=obs_cat) + table_cat = model.parse(adata_cat, region=region, region_key=region_key, instance_key="A") + assert isinstance(table_cat.obs["A"].dtype, pd.CategoricalDtype) + assert isinstance(table.obs[region_key].dtype, pd.CategoricalDtype) assert table.obs[region_key].cat.categories.tolist() == np.unique(region).tolist() assert table.uns[TableModel.ATTRS_KEY][TableModel.REGION_KEY_KEY] == region_key