"sd.filter_by_table_query()" fails with "IndexError" on SpatialData 0.7.3 / AnnData 0.12.6

Hi,

I’m encountering an IndexError when using sd.filter_by_table_query() on a SpatialData object.

Versions

spatialdata: 0.7.3
anndata: 0.12.6
annsel: 0.1.2

Variables

print(type(keep_mask))
><class 'pandas.Series'>
print(getattr(keep_mask, "dtype", None))
>bool
print(getattr(keep_mask, "shape", None))
>(576096,)
keep_mask.index.equals(sdata.tables["table"].obs.index)
>True

Code

import annsel as an
import spatialdata as sd

sdata_filtered = sd.filter_by_table_query(
    sdata,
    table_name="table",
    obs_expr=keep_mask,
)

Error

File .../python3.14/site-packages/spatialdata/_core/query/relational_query.py:898, in filter_by_table_query
    filtered_table: AnnData = sdata_subset.tables[table_name].an.filter(
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obs=obs_expr, var=var_expr, x=x_expr, obs_names=obs_names_expr, var_names=var_names_expr, layer=layer
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File .../python3.14/site-packages/annsel/tl/anndata.py:80, in filter
    _adata = _filter(self._obj, obs, var, x, obs_names, var_names, obsm, varm, layer)
  File ...python3.14/site-packages/annsel/tl/_filter.py:84, in _filter
    return _construct_adata_from_indices(adata, final_obs_idx, final_var_idx)
  File .../python3.14/site-packages/annsel/core/utils.py:52, in _construct_adata_from_indices
    _adata = adata[obs_idx.to_series(), var_idx.to_series()]
             ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File .../python3.14/site-packages/anndata/_core/anndata.py:1049, in __getitem__
    oidx, vidx = self._normalize_indices(index)
                 ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File .../python3.14/site-packages/anndata/_core/anndata.py:1030, in _normalize_indices
    return _normalize_indices(index, self.obs_names, self.var_names)
  File .../python3.14/site-packages/anndata/_core/index.py:35, in _normalize_indices
    ax0 = _normalize_index(ax0, names0)
  File .../python3.14/site-packages/anndata/_core/index.py:114, in _normalize_index
    raise IndexError()
IndexError

Is this a known compatibility issue between annsel 0.1.2 and anndata 0.12.x, or is there something wrong with how I’m passing the boolean filter?

Hi @albi Which version of Pandas are you using? It might be a pandas 3 thing.

Ah right, I am using Pandas 3.0.3. I will try to downgrade

Sounds great. On my side will try to update annsel to support Pandas 3 so you can go back up to the newest version of pandas / anndata!

2 Likes

It is working with Pandas 2, thanks for the help!

1 Like

Thanks @srivarra! And great to hear that the earlier pandas solves your issue @albi.

1 Like