Skip to content

DOC: Change code-block to ipython in r_interface.rst #23906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2018
Merged
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
38 changes: 24 additions & 14 deletions doc/source/r_interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,27 @@ 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:

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

>>> from rpy2.robjects import pandas2ri # doctest: +SKIP
>>> pandas2ri.activate() # doctest: +SKIP
In [1]: from rpy2.robjects import pandas2ri
...: pandas2ri.activate()

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:

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

>>> from rpy2.robjects import r # doctest: +SKIP
>>> r.data('iris') # doctest: +SKIP
>>> r['iris'].head() # doctest: +SKIP
In [2]: from rpy2.robjects import r

In [3]: r.data('iris')

In [4]: r['iris'].head()
Out[4]:
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
Expand All @@ -66,14 +71,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**):

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

In [5]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]},
...: index=["one", "two", "three"])

In [6]: r_dataframe = pandas2ri.py2ri(df)

In [7]: print(type(r_dataframe))
Out[7]: <class 'rpy2.robjects.vectors.DataFrame'>

>>> 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
In [8]: print(r_dataframe)
Out[8]:
A B C
one 1 4 7
two 2 5 8
Expand Down