/usr/lib/python2.7/dist-packages/rpy2/robjects/pandas2ri.py is in python-rpy2 2.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import rpy2.robjects as ro
import rpy2.robjects.conversion as conversion
import rpy2.rinterface as rinterface
from rpy2.rinterface import SexpVector, INTSXP
from pandas.core.frame import DataFrame as PandasDataFrame
from pandas.core.series import Series as PandasSeries
from pandas.core.index import Index as PandasIndex
from numpy import recarray
from collections import OrderedDict
from rpy2.robjects.vectors import DataFrame, Vector, ListVector, StrVector, IntVector, POSIXct
# pandas is requiring numpy. We add the numpy conversion will be
# activate in the function activate() below
import rpy2.robjects.numpy2ri as numpy2ri
ISOdatetime = rinterface.baseenv['ISOdatetime']
def pandas2ri(obj):
if isinstance(obj, PandasDataFrame):
od = OrderedDict()
for name, values in obj.iteritems():
if values.dtype.kind == 'O':
od[name] = StrVector(values)
else:
od[name] = pandas2ri(values)
return DataFrame(od)
elif isinstance(obj, PandasIndex):
if obj.dtype.kind == 'O':
return StrVector(obj)
else:
# only other alternative to 'O' is integer, I think,
# which goes straight to the numpy converter.
return numpy2ri.numpy2ri(obj)
elif isinstance(obj, PandasSeries):
if obj.dtype == '<M8[ns]':
# time series
d = [IntVector([x.year for x in obj]),
IntVector([x.month for x in obj]),
IntVector([x.day for x in obj]),
IntVector([x.hour for x in obj]),
IntVector([x.minute for x in obj]),
IntVector([x.second for x in obj])]
res = ISOdatetime(*d)
#FIXME: can the POSIXct be created from the POSIXct constructor ?
# (is '<M8[ns]' mapping to Python datetime.datetime ?)
res = POSIXct(res)
else:
# converted as a numpy array
res = numpy2ri.numpy2ri(obj.values)
# "index" is equivalent to "names" in R
if obj.ndim == 1:
res.do_slot_assign('names', ListVector({'x': pandas2ri(obj.index)}))
else:
res.do_slot_assign('dimnames', ListVector(pandas2ri(obj.index)))
return res
else:
# pandas2ri should definitely not have to know which paths remain to be
# converted by numpy2ri
return numpy2ri.numpy2ri(obj)
def ri2pandas(o):
# use the numpy converter first
res = numpy2ri.ri2numpy(o)
if isinstance(res, recarray):
res = PandasDataFrame.from_records(res)
return res
def activate():
'''Set conversion paths back to pandas2ri versions
This will straightforwardly override an existing numpy2ri.activate()
'''
conversion.py2ri = pandas2ri
conversion.ri2ro = ri2pandas
conversion.py2ro = numpy2ri.numpy2ro
def deactivate():
'''Set conversion paths back to robjects defaults
Note that this will also revert, e.g., numpy2ri.activate()
'''
conversion.py2ri = conversion.default_py2ri
conversion.ri2ro = conversion.default_ri2ro
conversion.py2ro = conversion.default_py2ro
|