Skip to content

Commit f4d288f

Browse files
Merge pull request #10075 from jorisvandenbossche/doc-rpy2-porting
DOC/DEPR: port pandas.rpy to rpy2 guide
2 parents 0517eac + e879138 commit f4d288f

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

doc/source/r_interface.rst

+63-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,69 @@ rpy2 / R interface
1515

1616
.. warning::
1717

18-
In v0.16.0, the ``pandas.rpy`` interface has been **deprecated and will be removed in a future version**. Similar functionaility can be accessed thru the `rpy2 <http://rpy.sourceforge.net/>`_ project.
18+
In v0.16.0, the ``pandas.rpy`` interface has been **deprecated and will be
19+
removed in a future version**. Similar functionality can be accessed
20+
through the `rpy2 <http://rpy.sourceforge.net/>`_ project.
21+
See the :ref:`updating <rpy.updating>` section for a guide to port your
22+
code from the ``pandas.rpy`` to ``rpy2`` functions.
23+
24+
25+
.. _rpy.updating:
26+
27+
Updating your code to use rpy2 functions
28+
----------------------------------------
29+
30+
In v0.16.0, the ``pandas.rpy`` module has been **deprecated** and users are
31+
pointed to the similar functionality in ``rpy2`` itself (rpy2 >= 2.4).
32+
33+
Instead of importing ``import pandas.rpy.common as com``, the following imports
34+
should be done to activate the pandas conversion support in rpy2::
35+
36+
from rpy2.robjects import pandas2ri
37+
pandas2ri.activate()
38+
39+
Converting data frames back and forth between rpy2 and pandas should be largely
40+
automated (no need to convert explicitly, it will be done on the fly in most
41+
rpy2 functions).
42+
43+
To convert explicitly, the functions are ``pandas2ri.py2ri()`` and
44+
``pandas2ri.ri2py()``. So these functions can be used to replace the existing
45+
functions in pandas:
46+
47+
- ``com.convert_to_r_dataframe(df)`` should be replaced with ``pandas2ri.py2ri(df)``
48+
- ``com.convert_robj(rdf)`` should be replaced with ``pandas2ri.ri2py(rdf)``
49+
50+
Note: these functions are for the latest version (rpy2 2.5.x) and were called
51+
``pandas2ri.pandas2ri()`` and ``pandas2ri.ri2pandas()`` previously.
52+
53+
Some of the other functionality in `pandas.rpy` can be replaced easily as well.
54+
For example to load R data as done with the ``load_data`` function, the
55+
current method::
56+
57+
df_iris = com.load_data('iris')
58+
59+
can be replaced with::
60+
61+
from rpy2.robjects import r
62+
r.data('iris')
63+
df_iris = pandas2ri.ri2py(r[name])
64+
65+
The ``convert_to_r_matrix`` function can be replaced by the normal
66+
``pandas2ri.py2ri`` to convert dataframes, with a subsequent call to R
67+
``as.matrix`` function.
68+
69+
.. warning::
70+
71+
Not all conversion functions in rpy2 are working exactly the same as the
72+
current methods in pandas. If you experience problems or limitations in
73+
comparison to the ones in pandas, please report this at the
74+
`issue tracker <https://github.com/pydata/pandas/issues>`_.
75+
76+
See also the documentation of the `rpy2 <http://rpy.sourceforge.net/>`_ project.
77+
78+
79+
R interface with rpy2
80+
---------------------
1981

2082
If your computer has R and rpy2 (> 2.2) installed (which will be left to the
2183
reader), you will be able to leverage the below functionality. On Windows,

pandas/rpy/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import warnings
66
warnings.warn("The pandas.rpy module is deprecated and will be "
77
"removed in a future version. We refer to external packages "
8-
"like rpy2, found here: http://rpy.sourceforge.net", FutureWarning)
8+
"like rpy2. "
9+
"\nSee here for a guide on how to port your code to rpy2: "
10+
"http://pandas.pydata.org/pandas-docs/stable/r_interface.html",
11+
FutureWarning)
912

1013
try:
1114
from .common import importr, r, load_data

0 commit comments

Comments
 (0)