Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions ci/deps/travis-36-doc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: pandas
channels:
- defaults
- conda-forge
- r
dependencies:
- beautifulsoup4
- bottleneck
Expand Down Expand Up @@ -31,14 +30,11 @@ dependencies:
- python-snappy
- python=3.6*
- pytz
- r
- rpy2
- scipy
- seaborn
- sphinx
- sqlalchemy
- statsmodels
- tzlocal
- xarray
- xlrd
- xlsxwriter
Expand Down
37 changes: 25 additions & 12 deletions doc/source/r_interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,28 @@ See also the documentation of the `rpy2 <http://rpy2.bitbucket.org/>`__ project:

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

.. ipython:: python
.. code-block:: python

from rpy2.robjects import r, pandas2ri
pandas2ri.activate()
>>> from rpy2.robjects import pandas2ri # doctest: +SKIP
>>> pandas2ri.activate() # doctest: +SKIP

Transferring R data sets into Python
------------------------------------

Once the pandas conversion is activated (``pandas2ri.activate()``), many conversions
of R to pandas objects will be done automatically. For example, to obtain the 'iris' dataset as a pandas DataFrame:

.. ipython:: python
.. code-block:: python

r.data('iris')
r['iris'].head()
>>> from rpy2.robjects import r # doctest: +SKIP
>>> r.data('iris') # doctest: +SKIP
>>> r['iris'].head() # doctest: +SKIP
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

If the pandas conversion was not activated, the above could also be accomplished
by explicitly converting it with the ``pandas2ri.ri2py`` function
Expand All @@ -59,13 +66,19 @@ Converting DataFrames into R objects
The ``pandas2ri.py2ri`` function support the reverse operation to convert
DataFrames into the equivalent R object (that is, **data.frame**):

.. ipython:: python
.. code-block:: python

>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]},
... index=["one", "two", "three"]) # doctest: +SKIP
>>> r_dataframe = pandas2ri.py2ri(df) # doctest: +SKIP
>>> print(type(r_dataframe)) # doctest: +SKIP
<class 'rpy2.robjects.vectors.DataFrame'>
>>> print(r_dataframe) # doctest: +SKIP
A B C
one 1 4 7
two 2 5 8
three 3 6 9

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]},
index=["one", "two", "three"])
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.
Expand Down