rpy2 / rpy2

Interface to use R from Python
https://rpy2.github.io
GNU General Public License v2.0
552 stars 73 forks source link

Error in Colab, rmagic and pandas 2.x #1105

Closed juanramonua closed 5 months ago

juanramonua commented 5 months ago

The error simply occurs when passing a Pandas variable from Python to R as a parameter into a rmagic command. I have tried different versions of both rpy2 (3.4.x and 3.5.x), Pandas (1.x and 2.x).

What seems to work is the combination of rpy2 with older versions of Pandas 1.x but the modern 2.x which is already required in some data analysis packages and will be the standard in the future, does not work.

Code in Colab (Jupyter notebook)

Cell 1:

!pip install pandas -U -q # 2.2.2
!pip install rpy2 -U -q # 3.5.16

Cell1 2:

%reload_ext rpy2.ipython
%R as.data.frame(R.Version())

Cell 3:

import numpy as np

df = pd.DataFrame({'Name':['one','two'], 'Value':[1,2]})
%R -i df

Expected behavior Conversion of dataframe variable from Python to R.

Error

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
[<ipython-input-7-ebe6f0002262>](https://localhost:8080/#) in <cell line: 1>()
----> 1 get_ipython().run_line_magic('R', '-i df')

8 frames
<decorator-gen-122> in R(self, line, cell, local_ns)

[/usr/local/lib/python3.10/dist-packages/rpy2/robjects/conversion.py](https://localhost:8080/#) in _raise_missingconverter(obj)
    382     Check rpy2's documentation about conversions.
    383     """
--> 384     raise NotImplementedError(_missingconverter_msg)
    385 
    386 

NotImplementedError: 
    Conversion rules for `rpy2.robjects` appear to be missing. Those
    rules are in a Python `contextvars.ContextVar`. This could be caused
    by multithreading code not passing context to the thread.
    Check rpy2's documentation about conversions.
lgautier commented 5 months ago

Duplicate of #1074

juanramonua commented 4 months ago

I have tried to follow the discussions in indicator #1074 and its other threads but there is no example of how to use it correctly, e.g. in Colab. There are only references to instructions to use a special -c parameter and the use of converters. In previous versions the usual data types (int, float, string) of a DataFrame of Pandas were automatically converted. I would like if possible to know how to modify the code I attached in the first post to make it work.

Best regards and thanks.