scvelo: Errors with pandas 1.4

Hey!

I updated my pandas version to the latest 1.4 and started getting errors with scvelo plotting and velocity functions. The same plotting functions work fine in scanpy and rolling back to pandas 1.3.5 also solves the issues.

import scanpy as sc
import scvelo as scv

adata = scv.datasets.pancreas()
scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=2000)
scv.pp.moments(adata, n_pcs=30, n_neighbors=30)

sc.pl.umap(adata, color = 'clusters') #Works as it should
scv.pl.umap(adata, color = 'clusters') #Results in AttributeError: 'Series' object has no attribute 'categories'

scv.tl.velocity(adata) #Results in AttributeError: 'Series' object has no attribute 'categories'
Error output
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [7], in <module>
----> 1 scv.tl.velocity(adata)

File ~/anaconda3/envs/scvelo2/lib/python3.8/site-packages/scvelo/tools/velocity.py:314, in velocity(data, vkey, mode, fit_offset, fit_offset2, filter_genes, groups, groupby, groups_for_fit, constrain_ratio, use_raw, use_latent_time, perc, min_r2, min_likelihood, r2_adjusted, use_highly_variable, diff_kinetics, copy, **kwargs)
    310     moments(adata)
    312 logg.info("computing velocities", r=True)
--> 314 strings_to_categoricals(adata)
    316 if mode is None or (mode == "dynamical" and "fit_alpha" not in adata.var.keys()):
    317     mode = "stochastic"

File ~/anaconda3/envs/scvelo2/lib/python3.8/site-packages/scvelo/tools/utils.py:301, in strings_to_categoricals(adata)
    296     return (
    297         is_string_dtype(values) or is_integer_dtype(values) or is_bool_dtype(values)
    298     )
    300 df = adata.obs
--> 301 df_keys = [key for key in df.columns if is_valid_dtype(df[key])]
    302 for key in df_keys:
    303     c = df[key]

File ~/anaconda3/envs/scvelo2/lib/python3.8/site-packages/scvelo/tools/utils.py:301, in <listcomp>(.0)
    296     return (
    297         is_string_dtype(values) or is_integer_dtype(values) or is_bool_dtype(values)
    298     )
    300 df = adata.obs
--> 301 df_keys = [key for key in df.columns if is_valid_dtype(df[key])]
    302 for key in df_keys:
    303     c = df[key]

File ~/anaconda3/envs/scvelo2/lib/python3.8/site-packages/scvelo/tools/utils.py:297, in strings_to_categoricals.<locals>.is_valid_dtype(values)
    295 def is_valid_dtype(values):
    296     return (
--> 297         is_string_dtype(values) or is_integer_dtype(values) or is_bool_dtype(values)
    298     )

File ~/anaconda3/envs/scvelo2/lib/python3.8/site-packages/pandas/core/dtypes/common.py:1322, in is_bool_dtype(arr_or_dtype)
   1319     return False
   1321 if isinstance(dtype, CategoricalDtype):
-> 1322     arr_or_dtype = arr_or_dtype.categories
   1323     # now we use the special definition for Index
   1325 if isinstance(arr_or_dtype, ABCIndex):
   1326     # Allow Index[object] that is all-bools or Index["boolean"]

File ~/anaconda3/envs/scvelo2/lib/python3.8/site-packages/pandas/core/generic.py:5583, in NDFrame.__getattr__(self, name)
   5576 if (
   5577     name not in self._internal_names_set
   5578     and name not in self._metadata
   5579     and name not in self._accessors
   5580     and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5581 ):
   5582     return self[name]
-> 5583 return object.__getattribute__(self, name)

AttributeError: 'Series' object has no attribute 'categories'
Versions
scvelo==0.2.4  scanpy==1.8.2  anndata==0.7.8  loompy==3.0.6  numpy==1.21.5  scipy==1.7.3  matplotlib==3.5.1  sklearn==1.0.2  pandas==1.4.0 

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Hi,

I deleted this line from the utils.py file corresponding to the functino get_legend() and now it works. “obs_vals.cat.categories = obs_vals.cat.categories.astype(str)”

Not need to downgrade pandas.

Just updated pandas to 1.4.1 and it works.

@stela2502, @karlann for the time being, you can downgrade to pandas==1.3.5.

Hi I had a similar error. When trying to use velocity_embedding_stream, I got the following error:

Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/storage/home/hcoda1/6/ggruenhagen3/p-js585-0/George/rich_project_pb1/conda_envs/velo3/lib/python3.11/site-packages/scvelo/plotting/velocity_embedding_stream.py”, line 252, in velocity_embedding_stream ax = scatter( ^^^^^^^^ File “/storage/home/hcoda1/6/ggruenhagen3/p-js585-0/George/rich_project_pb1/conda_envs/velo3/lib/python3.11/site-packages/scvelo/plotting/scatter.py”, line 668, in scatter set_legend( File “/storage/home/hcoda1/6/ggruenhagen3/p-js585-0/George/rich_project_pb1/conda_envs/velo3/lib/python3.11/site-packages/scvelo/plotting/utils.py”, line 555, in set_legend obs_vals.cat.categories = obs_vals.cat.categories.astype(str) ^^^^^^^^^^^^^^^^^^^^^^^ File “/storage/home/hcoda1/6/ggruenhagen3/p-js585-0/George/rich_project_pb1/conda_envs/velo3/lib/python3.11/site-packages/pandas/core/base.py”, line 178, in setattr object.setattr(self, key, value) File “/storage/home/hcoda1/6/ggruenhagen3/p-js585-0/George/rich_project_pb1/conda_envs/velo3/lib/python3.11/site-packages/pandas/core/accessor.py”, line 99, in _setter return self._delegate_property_set(name, new_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/storage/home/hcoda1/6/ggruenhagen3/p-js585-0/George/rich_project_pb1/conda_envs/velo3/lib/python3.11/site-packages/pandas/core/arrays/categorical.py”, line 2460, in _delegate_property_set return setattr(self._parent, name, new_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: property ‘categories’ of ‘Categorical’ object has no setter

I found the source code for the current verison of scvelo and changed the offending line of the version I had. From: obs_vals.cat.categories = obs_vals.cat.categories.astype(str) To: str_cats = obs_vals.cat.categories.astype(str) obs_vals = obs_vals.cat.set_categories(str_cats, rename=True)

I don’t know why I had this error in the first place since I’m using a verison of scvelo where this bug was fixed.

Versions: python==3.11.3, scvelo==0.2.5, scanpy==1.9.3, pandas=2.0.2, numpy==1.23.5 (I had to downgrade my numpy version, maybe that has something to do with it #1077)