10
10
11
11
import pandas as pd
12
12
import pandas .core .common as com
13
- import pandas .util .testing as _test
14
13
15
14
from rpy2 .robjects .packages import importr
16
- from rpy2 .robjects import r
17
15
import rpy2 .robjects as robj
18
16
19
17
import itertools as IT
20
18
21
19
22
20
__all__ = ['convert_robj' , 'load_data' , 'convert_to_r_dataframe' ,
23
- 'convert_to_r_matrix' ]
21
+ 'convert_to_r_matrix' , 'r' ]
22
+
23
+
24
+ def _assign (attr , obj ):
25
+ if isinstance (obj , (pd .DataFrame , pd .Series )):
26
+ obj = convert_to_r_dataframe (obj )
27
+ return robj .r .assign (attr , obj )
28
+
29
+
30
+ # Unable to subclass robjects.R because
31
+ # it has special creation process using rinterface
32
+ class _RPandas (object ):
33
+
34
+ def __getattribute__ (self , attr ):
35
+ if attr == 'assign' :
36
+ return _assign
37
+ return getattr (robj .r , attr )
38
+
39
+ def __getitem__ (self , item ):
40
+ result = robj .r [item ]
41
+ try :
42
+ result = convert_robj (result )
43
+ except TypeError :
44
+ pass
45
+ return result
46
+
47
+ def __str__ (self ):
48
+ return str (robj .r )
49
+
50
+ def __call__ (self , string ):
51
+ return robj .r (string )
52
+
53
+
54
+ r = _RPandas ()
24
55
25
56
26
57
def load_data (name , package = None , convert = True ):
27
58
if package :
28
59
importr (package )
29
60
30
- r .data (name )
61
+ robj . r .data (name )
31
62
32
- robj = r [name ]
63
+ r_obj = robj . r [name ]
33
64
34
65
if convert :
35
- return convert_robj (robj )
66
+ return convert_robj (r_obj )
36
67
else :
37
- return robj
68
+ return r_obj
38
69
39
70
40
71
def _rclass (obj ):
41
72
"""
42
73
Return R class name for input object
43
74
"""
44
- return r ['class' ](obj )[0 ]
75
+ return robj . r ['class' ](obj )[0 ]
45
76
46
77
47
78
def _is_null (obj ):
@@ -54,12 +85,12 @@ def _convert_list(obj):
54
85
"""
55
86
try :
56
87
values = [convert_robj (x ) for x in obj ]
57
- keys = r ['names' ](obj )
88
+ keys = robj . r ['names' ](obj )
58
89
return dict (zip (keys , values ))
59
90
except TypeError :
60
91
# For state.division and state.region
61
- factors = list (r ['factor' ](obj ))
62
- level = list (r ['levels' ](obj ))
92
+ factors = list (robj . r ['factor' ](obj ))
93
+ level = list (robj . r ['levels' ](obj ))
63
94
result = [level [index - 1 ] for index in factors ]
64
95
return result
65
96
@@ -77,9 +108,9 @@ def _list(item):
77
108
# For iris3, HairEyeColor, UCBAdmissions, Titanic
78
109
dim = list (obj .dim )
79
110
values = np .array (list (obj ))
80
- names = r ['dimnames' ](obj )
111
+ names = robj . r ['dimnames' ](obj )
81
112
try :
82
- columns = list (r ['names' ](names ))[::- 1 ]
113
+ columns = list (robj . r ['names' ](names ))[::- 1 ]
83
114
except TypeError :
84
115
columns = ['X{:d}' .format (i ) for i in range (len (names ))][::- 1 ]
85
116
columns .append ('value' )
@@ -98,18 +129,18 @@ def _convert_vector(obj):
98
129
# Check if the vector has extra information attached to it that can be used
99
130
# as an index
100
131
try :
101
- attributes = set (r ['attributes' ](obj ).names )
132
+ attributes = set (robj . r ['attributes' ](obj ).names )
102
133
except AttributeError :
103
134
return list (obj )
104
135
if 'names' in attributes :
105
- return pd .Series (list (obj ), index = r ['names' ](obj ))
136
+ return pd .Series (list (obj ), index = robj . r ['names' ](obj ))
106
137
elif 'tsp' in attributes :
107
- return pd .Series (list (obj ), index = r ['time' ](obj ))
138
+ return pd .Series (list (obj ), index = robj . r ['time' ](obj ))
108
139
elif 'labels' in attributes :
109
- return pd .Series (list (obj ), index = r ['labels' ](obj ))
140
+ return pd .Series (list (obj ), index = robj . r ['labels' ](obj ))
110
141
if _rclass (obj ) == 'dist' :
111
142
# For 'eurodist'. WARNING: This results in a DataFrame, not a Series or list.
112
- matrix = r ['as.matrix' ](obj )
143
+ matrix = robj . r ['as.matrix' ](obj )
113
144
return convert_robj (matrix )
114
145
else :
115
146
return list (obj )
@@ -167,7 +198,7 @@ def _convert_Matrix(mat):
167
198
rows = mat .rownames
168
199
169
200
columns = None if _is_null (columns ) else list (columns )
170
- index = r ['time' ](mat ) if _is_null (rows ) else list (rows )
201
+ index = robj . r ['time' ](mat ) if _is_null (rows ) else list (rows )
171
202
return pd .DataFrame (np .array (mat ), index = _check_int (index ),
172
203
columns = columns )
173
204
@@ -310,6 +341,10 @@ def convert_to_r_dataframe(df, strings_as_factors=False):
310
341
311
342
columns = rlc .OrdDict ()
312
343
344
+ if isinstance (df , pd .Series ):
345
+ name = df .name or 'X0'
346
+ df = pd .DataFrame (df , columns = [name ])
347
+
313
348
# FIXME: This doesn't handle MultiIndex
314
349
315
350
for column in df :
@@ -365,5 +400,6 @@ def convert_to_r_matrix(df, strings_as_factors=False):
365
400
366
401
return r_matrix
367
402
403
+
368
404
if __name__ == '__main__' :
369
405
pass
0 commit comments