Skip to content

Key error in render_points with method=datashader when coloring by table.obs #479

@grst

Description

@grst

When attempting to plot points colored by an annotation column from table.obs, it works as expected with the matplotlib backend, but fails with a KeyError when using the datashader backend.

Steps to reproduce

import spatialdata as sd
import spatialdata_plot
import pandas as pd
import anndata as ad

sdata = sd.datasets.blobs()
adata = sd.models.TableModel.parse(
    ad.AnnData(
        obs = pd.DataFrame().assign(
            region=pd.Categorical(["blobs_points"] * 200), 
            foo = pd.Categorical(["a", "b"] * 100),
            instance = range(200)
        )
    )
)
adata.uns["spatialdata_attrs"] = {
    "region": "blobs_points",
    "region_key": "region", 
    "instance_key":  "instance"
}
sd.models.TableModel().validate(adata)
sdata.tables["table_for_points"] = adata

# works
sdata.pl.render_points("blobs_points", method="matplotlib", color="foo", table_name="table_for_points").pl.show()

# doesn't work
sdata.pl.render_points("blobs_points", method="datashader", color="foo", table_name="table_for_points").pl.show()

Stacktrace

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/pandas/core/indexes/base.py:3812, in Index.get_loc(self, key)
   3811 try:
-> [3812](https://vscode-remote+ssh-002dremote-002binh.vscode-resource.vscode-cdn.net/scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/pandas/core/indexes/base.py:3812)     return self._engine.get_loc(casted_key)
   3813 except KeyError as err:

File pandas/_libs/index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/index.pyx:196, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:7096, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'foo'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[33], [line 1](vscode-notebook-cell:?execution_count=33&line=1)
----> [1](vscode-notebook-cell:?execution_count=33&line=1) sdata.pl.render_points("blobs_points", method="datashader", color="foo", table_name="table_for_points").pl.show()

File /cfs/sturmgre/projects/scverse/spatialdata-plot/src/spatialdata_plot/pl/basic.py:953, in PlotAccessor.show(self, coordinate_systems, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, na_in_legend, colorbar, wspace, hspace, ncols, frameon, figsize, dpi, fig, title, share_extent, pad_extent, ax, return_ax, save)
    948     wanted_elements, wanted_points_on_this_cs, wants_points = _get_wanted_render_elements(
    949         sdata, wanted_elements, params_copy, cs, "points"
    950     )
    952     if wanted_points_on_this_cs:
--> [953](https://vscode-remote+ssh-002dremote-002binh.vscode-resource.vscode-cdn.net/cfs/sturmgre/projects/scverse/spatialdata-plot/src/spatialdata_plot/pl/basic.py:953)         _render_points(
    954             sdata=sdata,
    955             render_params=params_copy,
    956             coordinate_system=cs,
    957             ax=ax,
    958             fig_params=fig_params,
    959             scalebar_params=scalebar_params,
    960             legend_params=legend_params,
    961         )
    963 elif cmd == "render_labels" and has_labels:
    964     wanted_elements, wanted_labels_on_this_cs, wants_labels = _get_wanted_render_elements(
    965         sdata, wanted_elements, params_copy, cs, "labels"
    966     )

File /cfs/sturmgre/projects/scverse/spatialdata-plot/src/spatialdata_plot/pl/render.py:593, in _render_points(sdata, render_params, coordinate_system, ax, fig_params, scalebar_params, legend_params)
    590 # use datashader for the visualization of points
    591 cvs = ds.Canvas(plot_width=plot_width, plot_height=plot_height, x_range=x_ext, y_range=y_ext)
--> [593](https://vscode-remote+ssh-002dremote-002binh.vscode-resource.vscode-cdn.net/cfs/sturmgre/projects/scverse/spatialdata-plot/src/spatialdata_plot/pl/render.py:593) color_by_categorical = col_for_color is not None and transformed_element[col_for_color].values.dtype in (
    594     object,
    595     "categorical",
    596 )
    597 if color_by_categorical and transformed_element[col_for_color].values.dtype == object:
    598     transformed_element[col_for_color] = transformed_element[col_for_color].astype("category")

File /scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/dask/dataframe/core.py:4955, in DataFrame.__getitem__(self, key)
   4952         return self.loc[key]
   4954 # error is raised from pandas
-> [4955](https://vscode-remote+ssh-002dremote-002binh.vscode-resource.vscode-cdn.net/scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/dask/dataframe/core.py:4955) meta = self._meta[_extract_meta(key)]
   4956 dsk = partitionwise_graph(operator.getitem, name, self, key)
   4957 graph = HighLevelGraph.from_collections(name, dsk, dependencies=[self])

File /scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/pandas/core/frame.py:4107, in DataFrame.__getitem__(self, key)
   4105 if self.columns.nlevels > 1:
   4106     return self._getitem_multilevel(key)
-> [4107](https://vscode-remote+ssh-002dremote-002binh.vscode-resource.vscode-cdn.net/scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/pandas/core/frame.py:4107) indexer = self.columns.get_loc(key)
   4108 if is_integer(indexer):
   4109     indexer = [indexer]

File /scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/pandas/core/indexes/base.py:3819, in Index.get_loc(self, key)
   3814     if isinstance(casted_key, slice) or (
   3815         isinstance(casted_key, abc.Iterable)
   3816         and any(isinstance(x, slice) for x in casted_key)
   3817     ):
   3818         raise InvalidIndexError(key)
-> [3819](https://vscode-remote+ssh-002dremote-002binh.vscode-resource.vscode-cdn.net/scratch/sturmgre/projects/scverse/spatialdata-plot/.venv/lib/python3.13/site-packages/pandas/core/indexes/base.py:3819)     raise KeyError(key) from err
   3820 except TypeError:
   3821     # If we have a listlike key, _check_indexing_error will raise
   3822     #  InvalidIndexError. Otherwise we fall through and re-raise
   3823     #  the TypeError.
   3824     self._check_indexing_error(key)

KeyError: 'foo'

Session Info

Package Version
spatialdata 0.5.0
spatialdata-plot 0.2.12.dev0+g788eb2206.d20250827
pandas 2.3.2
anndata 0.12.2
dask 2024.11.2
Dependency Version
scikit-image 0.25.2
MarkupSafe 3.0.2
spatial_image 1.2.3
setuptools 80.9.0
executing 2.2.0
defusedxml 0.7.1
jaraco.functools 4.0.1
charset-normalizer 3.4.3
urllib3 2.5.0
Pygments 2.19.2
jaraco.text 3.12.1
pyct 0.5.0
toolz 1.0.0
matplotlib 3.10.5
jupyter_client 8.6.3
kiwisolver 1.4.9
platformdirs 4.4.0
pyarrow 21.0.0
typing_extensions 4.15.0
natsort 8.4.0
parso 0.8.5
psutil 7.0.0
ipywidgets 8.1.7
multiscale_spatial_image 2.0.3
multipledispatch 1.0.0 (0.6.0)
threadpoolctl 3.6.0
jedi 0.19.2
cycler 0.12.1
pure_eval 0.2.3
legacy-api-wrap 1.4.1
scanpy 1.11.4
pyparsing 3.2.3
pytz 2025.2
Jinja2 3.1.6
asciitree 0.3.3
ome-zarr 0.11.1
pyzmq 27.0.2
jaraco.collections 5.1.0
traitlets 5.14.3
param 2.2.1
rich 14.1.0
scikit-learn 1.7.1
requests 2.32.5
matplotlib-scalebar 0.9.0
idna 3.10
xarray 2025.8.0
numcodecs 0.15.1
dask-image 2024.5.3
cloudpickle 3.1.1
numpy 2.2.6
joblib 1.5.2
datashader 0.18.2
session-info2 0.2.1
cffi 1.17.1
wcwidth 0.2.13
wrapt 1.17.3
jupyter_core 5.8.1
pyproj 3.7.2
jaraco.context 5.3.0
asttokens 3.0.0
numba 0.61.2
scipy 1.16.1
comm 0.2.3
tornado 6.5.2
decorator 5.2.1
prompt_toolkit 3.0.51
Deprecated 1.2.18
h5py 3.14.0
xarray-dataclass 3.0.0
xarray-schema 0.0.3
geopandas 1.1.1
zarr 2.18.7
PyYAML 6.0.2
more-itertools 10.3.0
stack-data 0.6.3
pillow 11.3.0
matplotlib-inline 0.1.7
xarray-spatial 0.4.0
lazy_loader 0.4
python-dateutil 2.9.0.post0
debugpy 1.8.16
certifi 2025.8.3 (2025.08.03)
networkx 3.5
packaging 25.0
ipykernel 6.30.1
six 1.17.0
attrs 25.3.0
fsspec 2025.7.0
llvmlite 0.44.0
pycparser 2.22
ipython 9.4.0
shapely 2.1.1
Component Info
Python 3.13.1 (main, Jan 14 2025, 22:47:38) [Clang 19.1.6 ]
OS Linux-5.14.0-427.31.1.el9_4.x86_64-x86_64-with-glibc2.34
Updated 2025-08-27 13:59

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions