scverse / scverse/scanpy

Plotting error after creating new category: 'Float64Index' object has no attribute 'add_categories'

Open
#1,975 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

  • [x ] I have checked that this issue has not already been reported.
  • [x ] I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the master branch of scanpy.

When updating a category or creating a new one, it is not possible to plot the associated umap with the new category.

pbmc = sc.datasets.pbmc68k_reduced()
sc.pl.umap(pbmc, color = 'phase')
new_cluster_names = [
    'A', 'B', 'C']
pbmc.obs['cell_types'] = pbmc.obs['phase']
pbmc.obs['cell_types'].cat.categories = new_cluster_names
sc.pl.umap(pbmc, color=['cell_types'])

pbmc.rename_categories('phase', new_cluster_names)
sc.pl.umap(pbmc, color=['phase'])


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-82-890b788bf078> in <module>
----> 1 sc.pl.umap(pbmc, color=['phase'])

~/miniconda3/envs/single_cell_181/lib/python3.7/site-packages/scanpy/plotting/_tools/scatterplots.py in umap(adata, **kwargs)
    657     tl.umap
    658     """
--> 659     return embedding(adata, 'umap', **kwargs)
    660 
    661 

~/miniconda3/envs/single_cell_181/lib/python3.7/site-packages/scanpy/plotting/_tools/scatterplots.py in embedding(adata, basis, color, gene_symbols, use_raw, sort_order, edges, edges_width, edges_color, neighbors_key, arrows, arrows_kwds, groups, components, layer, projection, scale_factor, color_map, cmap, palette, na_color, na_in_legend, size, frameon, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, vmax, vmin, vcenter, norm, add_outline, outline_width, outline_color, ncols, hspace, wspace, title, show, save, ax, return_fig, **kwargs)
    255             color_source_vector,
    256             palette=palette,
--> 257             na_color=na_color,
    258         )
    259 

~/miniconda3/envs/single_cell_181/lib/python3.7/site-packages/scanpy/plotting/_tools/scatterplots.py in _color_vector(adata, values_key, values, palette, na_color)
   1275         # Set color to 'missing color' for all missing values
   1276         if color_vector.isna().any():
-> 1277             color_vector = color_vector.add_categories([to_hex(na_color)])
   1278             color_vector = color_vector.fillna(to_hex(na_color))
   1279         return color_vector, True

AttributeError: 'Float64Index' object has no attribute 'add_categories'
Versions

scanpy==1.8.1 anndata==0.7.6 umap==0.5.1 numpy==1.21.1 scipy==1.7.0 pandas==1.3.1 scikit-learn==0.24.2 statsmodels==0.12.2 python-igraph==0.9.6 pynndescent==0.5.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in scanpy/plotting/_tools/scatterplots.py, at _color_vector where the traceback calls add_categories on the plotting color vector. Run the provided UMAP reproduction with the listed scanpy and pandas versions. Done means plotting the renamed or newly created category completes without the Float64Index AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.