Error while using Rpy2 : "ValueError: Converting pandas "Category" series to R factor is only possible when categories are strings."

34 views Asked by At

I am trying to move between R and Phyton in my single cell analysis. I have an anndata object from scanpy that I want to apply some analysis in R on. However when I try to start moving to R while writing this:

%%R -i adata_mon

I get the error:

ValueError: Converting pandas "Category" series to R factor is only possible when categories are strings.

In more details:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[20], line 1
----> 1 get_ipython().run_cell_magic('R', '-i adata_mon', '\n')

File ~/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2493, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2491 with self.builtin_trap:
   2492     args = (magic_arg_s, cell)
-> 2493     result = fn(*args, **kwargs)
   2495 # The code below prevents the output from being displayed
   2496 # when using magics with decorator @output_can_be_silenced
   2497 # when the last Python token in the expression is a ';'.
   2498 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/anaconda3/lib/python3.11/site-packages/rpy2/ipython/rmagic.py:916, in RMagics.R(self, line, cell, local_ns)
    914     with localconverter(converter) as cv:
    915         for arg in ','.join(args.input).split(','):
--> 916             self._import_name_into_r(arg, ro.globalenv, local_ns)
    918 if args.display:
    919     try:

File ~/anaconda3/lib/python3.11/site-packages/rpy2/ipython/rmagic.py:444, in RMagics._import_name_into_r(self, arg, env, local_ns)
    442         val = _find(rhs, self.shell.user_ns)
    443 if val is not None:
--> 444     env[lhs] = val

File ~/anaconda3/lib/python3.11/site-packages/rpy2/robjects/environments.py:35, in Environment.__setitem__(self, item, value)
     34 def __setitem__(self, item: str, value: typing.Any) -> None:
---> 35     robj = conversion.get_conversion().py2rpy(value)
     36     super(Environment, self).__setitem__(item, robj)

File ~/anaconda3/lib/python3.11/functools.py:909, in singledispatch.<locals>.wrapper(*args, **kw)
    905 if not args:
    906     raise TypeError(f'{funcname} requires at least '
    907                     '1 positional argument')
--> 909 return dispatch(args[0].__class__)(*args, **kw)

File ~/anaconda3/lib/python3.11/site-packages/anndata2ri/_py2r.py:74, in py2rpy_anndata(obj)
     71     row_args['row.names'] = pandas2ri.py2rpy(obj.var_names)
     72 row_data = s4v.DataFrame(**row_args)
---> 74 col_args = {k: pandas2ri.py2rpy(v) for k, v in obj.obs.items()}
     75 if check_no_dupes(obj.obs_names, 'obs_names'):
     76     col_args['row.names'] = pandas2ri.py2rpy(obj.obs_names)

File ~/anaconda3/lib/python3.11/site-packages/anndata2ri/_py2r.py:74, in <dictcomp>(.0)
     71     row_args['row.names'] = pandas2ri.py2rpy(obj.var_names)
     72 row_data = s4v.DataFrame(**row_args)
---> 74 col_args = {k: pandas2ri.py2rpy(v) for k, v in obj.obs.items()}
     75 if check_no_dupes(obj.obs_names, 'obs_names'):
     76     col_args['row.names'] = pandas2ri.py2rpy(obj.obs_names)

File ~/anaconda3/lib/python3.11/functools.py:909, in singledispatch.<locals>.wrapper(*args, **kw)
    905 if not args:
    906     raise TypeError(f'{funcname} requires at least '
    907                     '1 positional argument')
--> 909 return dispatch(args[0].__class__)(*args, **kw)

File ~/anaconda3/lib/python3.11/site-packages/rpy2/robjects/pandas2ri.py:175, in py2rpy_pandasseries(obj)
    173     res = StrVector(obj)
    174 elif obj.dtype.name == 'category':
--> 175     res = py2rpy_categorical(obj.cat)
    176     res = FactorVector(res)
    177 elif is_datetime64_any_dtype(obj.dtype):
    178     # time series

File ~/anaconda3/lib/python3.11/site-packages/rpy2/robjects/pandas2ri.py:93, in py2rpy_categorical(obj)
     91 for c in obj.categories:
     92     if not isinstance(c, str):
---> 93         raise ValueError('Converting pandas "Category" series to '
     94                          'R factor is only possible when categories '
     95                          'are strings.')
     96 res = IntSexpVector(list(rinterface.NA_Integer if x == -1 else x+1
     97                          for x in obj.codes))
     98 res.do_slot_assign('levels', StrSexpVector(obj.categories))

ValueError: Converting pandas "Category" series to R factor is only possible when categories are strings.

Does anyone have any idea what is going wrong in here?

Thank you so much for your time in advance

I could not find any solution...

0

There are 0 answers