Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ def _render_shapes(

color_key = (
[_hex_no_alpha(x) for x in color_vector.categories.values]
if (type(color_vector) is pd.core.arrays.categorical.Categorical)
and (len(color_vector.categories.values) > 1)
if isinstance(color_vector.dtype, pd.CategoricalDtype) and (len(color_vector.categories.values) > 1)
else None
)

Expand Down Expand Up @@ -854,8 +853,7 @@ def _render_points(

color_key: list[str] | None = (
list(color_vector.categories.values)
if (type(color_vector) is pd.core.arrays.categorical.Categorical)
and (len(color_vector.categories.values) > 1)
if isinstance(color_vector.dtype, pd.CategoricalDtype) and (len(color_vector.categories.values) > 1)
else None
)

Expand Down
7 changes: 5 additions & 2 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,10 @@ def _set_color_source_vec(
raise ValueError("Unable to create color palette.")

# do not rename categories, as colors need not be unique
color_vector = color_source_vector.map(color_mapping)
# pd.Categorical.map() demotes to object dtype when mapped values aren't unique
# (e.g. two categories share a color). Wrapping back in pd.Categorical ensures
# downstream consumers always receive a Categorical for categorical data.
color_vector = pd.Categorical(color_source_vector.map(color_mapping, na_action="ignore"))

return color_source_vector, color_vector, True

Expand All @@ -1146,7 +1149,7 @@ def _map_color_seg(
) -> ArrayLike:
cell_id = np.array(cell_id)

if pd.api.types.is_categorical_dtype(color_vector.dtype):
if isinstance(color_vector.dtype, pd.CategoricalDtype):
# Case A: users wants to plot a categorical column
if np.any(color_source_vector.isna()):
cell_id[color_source_vector.isna()] = 0
Expand Down