Skip to content

Commit

Permalink
CLN/DEPR: remove deprecated pandas.rpy module (GH9602)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Jan 22, 2017
1 parent 0e219d7 commit e9d2d10
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 766 deletions.
3 changes: 1 addition & 2 deletions ci/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ RET=0

if [ "$LINT" ]; then

# pandas/rpy is deprecated and will be removed.
# pandas/src is C code, so no need to search there.
echo "Linting *.py"
flake8 pandas --filename=*.py --exclude pandas/rpy,pandas/src
flake8 pandas --filename=*.py --exclude pandas/src
if [ $? -ne "0" ]; then
RET=1
fi
Expand Down
140 changes: 22 additions & 118 deletions doc/source/r_interface.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. currentmodule:: pandas.rpy

.. _rpy:

.. ipython:: python
Expand All @@ -15,157 +13,63 @@ rpy2 / R interface

.. warning::

In v0.16.0, the ``pandas.rpy`` interface has been **deprecated and will be
removed in a future version**. Similar functionality can be accessed
through the `rpy2 <https://rpy2.readthedocs.io/>`__ project.
See the :ref:`updating <rpy.updating>` section for a guide to port your
code from the ``pandas.rpy`` to ``rpy2`` functions.


.. _rpy.updating:

Updating your code to use rpy2 functions
----------------------------------------

In v0.16.0, the ``pandas.rpy`` module has been **deprecated** and users are
pointed to the similar functionality in ``rpy2`` itself (rpy2 >= 2.4).
Up to pandas 0.19, a ``pandas.rpy`` module existed with functionality to
convert between pandas and ``rpy2`` objects. This functionality now lives in
the `rpy2 <https://rpy2.readthedocs.io/>`__ project itself.
See the `updating section <http://pandas.pydata.org/pandas-docs/version/0.19.0/r_interface.html#updating-your-code-to-use-rpy2-functions>`__
of the previous documentation for a guide to port your code from the
removed ``pandas.rpy`` to ``rpy2`` functions.

Instead of importing ``import pandas.rpy.common as com``, the following imports
should be done to activate the pandas conversion support in rpy2::

from rpy2.robjects import pandas2ri
pandas2ri.activate()

`rpy2 <http://rpy2.bitbucket.org/>`__ is an interface to R running embedded in a Python process, and also includes functionality to deal with pandas DataFrames.
Converting data frames back and forth between rpy2 and pandas should be largely
automated (no need to convert explicitly, it will be done on the fly in most
rpy2 functions).

To convert explicitly, the functions are ``pandas2ri.py2ri()`` and
``pandas2ri.ri2py()``. So these functions can be used to replace the existing
functions in pandas:

- ``com.convert_to_r_dataframe(df)`` should be replaced with ``pandas2ri.py2ri(df)``
- ``com.convert_robj(rdf)`` should be replaced with ``pandas2ri.ri2py(rdf)``

Note: these functions are for the latest version (rpy2 2.5.x) and were called
``pandas2ri.pandas2ri()`` and ``pandas2ri.ri2pandas()`` previously.

Some of the other functionality in `pandas.rpy` can be replaced easily as well.
For example to load R data as done with the ``load_data`` function, the
current method::

df_iris = com.load_data('iris')

can be replaced with::

from rpy2.robjects import r
r.data('iris')
df_iris = pandas2ri.ri2py(r[name])

The ``convert_to_r_matrix`` function can be replaced by the normal
``pandas2ri.py2ri`` to convert dataframes, with a subsequent call to R
``as.matrix`` function.

.. warning::

Not all conversion functions in rpy2 are working exactly the same as the
current methods in pandas. If you experience problems or limitations in
comparison to the ones in pandas, please report this at the
`issue tracker <https://github.com/pandas-dev/pandas/issues>`_.

See also the documentation of the `rpy2 <http://rpy2.bitbucket.org/>`__ project.

``pandas2ri.ri2py()``.

R interface with rpy2
---------------------

If your computer has R and rpy2 (> 2.2) installed (which will be left to the
reader), you will be able to leverage the below functionality. On Windows,
doing this is quite an ordeal at the moment, but users on Unix-like systems
should find it quite easy. rpy2 evolves in time, and is currently reaching
its release 2.3, while the current interface is
designed for the 2.2.x series. We recommend to use 2.2.x over other series
unless you are prepared to fix parts of the code, yet the rpy2-2.3.0
introduces improvements such as a better R-Python bridge memory management
layer so it might be a good idea to bite the bullet and submit patches for
the few minor differences that need to be fixed.
See also the documentation of the `rpy2 <http://rpy2.bitbucket.org/>`__ project: https://rpy2.readthedocs.io.

In the remainder of this page, a few examples of explicit conversion is given. The pandas conversion of rpy2 needs first to be activated:

::

# if installing for the first time
hg clone http://bitbucket.org/lgautier/rpy2

cd rpy2
hg pull
hg update version_2.2.x
sudo python setup.py install

.. note::

To use R packages with this interface, you will need to install
them inside R yourself. At the moment it cannot install them for
you.
.. ipython:: python
Once you have done installed R and rpy2, you should be able to import
``pandas.rpy.common`` without a hitch.
from rpy2.robjects import r, pandas2ri
pandas2ri.activate()
Transferring R data sets into Python
------------------------------------

The **load_data** function retrieves an R data set and converts it to the
The ``pandas2ri.ri2py`` function retrieves an R data set and converts it to the
appropriate pandas object (most likely a DataFrame):


.. ipython:: python
:okwarning:
import pandas.rpy.common as com
infert = com.load_data('infert')
infert.head()
r.data('iris')
df_iris = pandas2ri.ri2py(r['iris'])
df_iris.head()
Converting DataFrames into R objects
------------------------------------

.. versionadded:: 0.8

Starting from pandas 0.8, there is **experimental** support to convert
The ``pandas2ri.py2ri`` function support the reverse operation to convert
DataFrames into the equivalent R object (that is, **data.frame**):

.. ipython:: python
import pandas.rpy.common as com
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]},
index=["one", "two", "three"])
r_dataframe = com.convert_to_r_dataframe(df)
r_dataframe = pandas2ri.py2ri(df)
print(type(r_dataframe))
print(r_dataframe)
The DataFrame's index is stored as the ``rownames`` attribute of the
data.frame instance.

You can also use **convert_to_r_matrix** to obtain a ``Matrix`` instance, but
bear in mind that it will only work with homogeneously-typed DataFrames (as
R matrices bear no information on the data type):


.. ipython:: python
import pandas.rpy.common as com
r_matrix = com.convert_to_r_matrix(df)
print(type(r_matrix))
print(r_matrix)
Calling R functions with pandas objects
---------------------------------------



High-level interface to R estimators
------------------------------------
..
Calling R functions with pandas objects
High-level interface to R estimators
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ Deprecations
Removal of prior version deprecations/changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- The ``pandas.rpy`` module is removed. Similar functionality can be accessed
through the `rpy2 <https://rpy2.readthedocs.io/>`__ project.
See the :ref:`R interfacing docs <rpy>` for more details.
- ``pd.to_datetime`` and ``pd.to_timedelta`` have dropped the ``coerce`` parameter in favor of ``errors`` (:issue:`13602`)


Expand Down
2 changes: 1 addition & 1 deletion pandas/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestPDApi(Base, tm.TestCase):

# these are optionally imported based on testing
# & need to be ignored
ignored = ['tests', 'rpy', 'locale']
ignored = ['tests', 'locale']

# top-level sub-packages
lib = ['api', 'compat', 'computation', 'core',
Expand Down
18 changes: 0 additions & 18 deletions pandas/rpy/__init__.py

This file was deleted.

14 changes: 0 additions & 14 deletions pandas/rpy/base.py

This file was deleted.

Loading

0 comments on commit e9d2d10

Please sign in to comment.