@@ -33,21 +33,28 @@ See also the documentation of the `rpy2 <http://rpy2.bitbucket.org/>`__ project:
3333
3434In the remainder of this page, a few examples of explicit conversion is given. The pandas conversion of rpy2 needs first to be activated:
3535
36- .. ipython :: python
36+ .. code-block :: python
3737
38- from rpy2.robjects import r, pandas2ri
39- pandas2ri.activate()
38+ >> > from rpy2.robjects import pandas2ri # doctest: +SKIP
39+ >> > pandas2ri.activate() # doctest: +SKIP
4040
4141 Transferring R data sets into Python
4242------------------------------------
4343
4444Once the pandas conversion is activated (``pandas2ri.activate() ``), many conversions
4545of R to pandas objects will be done automatically. For example, to obtain the 'iris' dataset as a pandas DataFrame:
4646
47- .. ipython :: python
47+ .. code-block :: python
4848
49- r.data(' iris' )
50- r[' iris' ].head()
49+ >> > from rpy2.robjects import r # doctest: +SKIP
50+ >> > r.data(' iris' ) # doctest: +SKIP
51+ >> > r[' iris' ].head() # doctest: +SKIP
52+ Sepal.Length Sepal.Width Petal.Length Petal.Width Species
53+ 0 5.1 3.5 1.4 0.2 setosa
54+ 1 4.9 3.0 1.4 0.2 setosa
55+ 2 4.7 3.2 1.3 0.2 setosa
56+ 3 4.6 3.1 1.5 0.2 setosa
57+ 4 5.0 3.6 1.4 0.2 setosa
5158
5259 If the pandas conversion was not activated, the above could also be accomplished
5360by explicitly converting it with the ``pandas2ri.ri2py `` function
@@ -59,13 +66,19 @@ Converting DataFrames into R objects
5966The ``pandas2ri.py2ri `` function support the reverse operation to convert
6067DataFrames into the equivalent R object (that is, **data.frame **):
6168
62- .. ipython :: python
69+ .. code-block :: python
70+
71+ >> > df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ], ' C' : [7 , 8 , 9 ]},
72+ ... index = [" one" , " two" , " three" ]) # doctest: +SKIP
73+ >> > r_dataframe = pandas2ri.py2ri(df) # doctest: +SKIP
74+ >> > print (type (r_dataframe)) # doctest: +SKIP
75+ < class ' rpy2.robjects.vectors.DataFrame' >
76+ >> > print (r_dataframe) # doctest: +SKIP
77+ A B C
78+ one 1 4 7
79+ two 2 5 8
80+ three 3 6 9
6381
64- df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ], ' C' :[7 ,8 ,9 ]},
65- index = [" one" , " two" , " three" ])
66- r_dataframe = pandas2ri.py2ri(df)
67- print (type (r_dataframe))
68- print (r_dataframe)
6982
7083 The DataFrame's index is stored as the ``rownames `` attribute of the
7184data.frame instance.
0 commit comments