Plotting error after creating new category: 'Float64Index' object has no attribute 'add_categories'
See original GitHub issue- [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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Plotting error after creating new category: 'Float64Index' object ...
Plotting error after creating new category: 'Float64Index' object has no attribute 'add_categories' #1975.
Read more >Pandas error "AttributeError: 'DataFrame' object has no ...
The error occurs because pandas.Series.cat.add_categories is a Series method, and df[['embarked', 'sex', 'pclass']] is a DataFrame. Use pd.
Read more >pandas.CategoricalIndex.add_categories
Categorical with new categories added or None if inplace=True . Raises. ValueError. If the new categories include old categories or do not validate...
Read more >[Code]-Pandas error "AttributeError: 'DataFrame' object has no ...
... 'DataFrame' object has no attribute 'add_categories'" when trying to add ... error earlier 'ValueError: fill value must be in categories' when working ......
Read more >Source code for pandas.core.categorical
Setting assigns new values to each category (effectively a rename of each individual category). The assigned value has to be a list-like object....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This might be due to updates in pandas >1.3.0. The command
pbmc.rename_categories('phase', new_cluster_names)
seems to be deprecated. In particular, the “inplace” option is no longer valid, so it seems that one can only create a copy of the renamed categories and store it. Hence, the new command should bepbmc.obs['phase'] = pbmc.obs['phase'].cat.rename_categories(new_cluster_names)
. I checked that this works on a different data set, but haven’t checked for pbmc. If this fully fixes the problem, only the tutorial needs to be updated (the command for renaming the clusters) and scanpy doesn’t need to be modified.Reference: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.cat.rename_categories.html
I have verified that this change works in the scanpy clustering tutorial. The exact change I made was
adata.rename_categories('leiden', new_cluster_names)
becameadata.obs['leiden'] =adata.obs['leiden'].cat.rename_categories(new_cluster_names)