Converting Python Objects for Rpy2

Converting python objects for rpy2

You need to add

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

See more in rpy2 documentation numpy section (here for the older 2.x version)

Prior to 2.2.x the import alone was sufficient.

That import alone is sufficient to
switch an automatic conversion of
numpy objects into rpy2 objects.

Why make this an optional import,
while it could have been included in
the function py2ri() (as done in the
original patch submitted for that
function) ?

Although both are valid and reasonable
options, the design decision was taken
in order to decouple rpy2 from numpy
the most, and do not assume that
having numpy installed automatically
meant that a programmer wanted to use
it.

Conversion python dataframe character column to r with rpy2

Your problem has nothing to do with rpy2, you are just parsing dates incorrectly in pandas. See:

from pandas import DataFrame, to_datetime

df = DataFrame(dict(event_time=['2019-10-11', '2020-01-01']))

df.event_time = to_datetime(df.event_time)

print(list(df.event_time))
# [Timestamp('2019-10-11 00:00:00'), Timestamp('2020-01-01 00:00:00')]

# you using dt.strftime you was just converting them back to strings, see:
print(list(df.event_time.dt.strftime("%Y-%m-%d")))
# ['2019-10-11', '2020-01-01', '2019-11-15']

# now you could extract date object (but don't! timestamps are fine for rpy2)
print(list(df.event_time.dt.date))
# [datetime.date(2019, 10, 11), datetime.date(2020, 1, 1)]

Now in rpy2 you simply do:

from rpy2.robjects import conversion, default_converter, pandas2ri
from rpy2.robjects.conversion import localconverter

with localconverter(default_converter + pandas2ri.converter):
df_r = conversion.py2rpy(df)

print(repr(df_r.rx2('event_time')))
# R object with classes: ('POSIXct', 'POSIXt') mapped to:
# [2019-10-11, 2020-01-01]

Now you can have fun with handling the dates on the R side, see dates. Also, if you happen to use Jupyter notebooks, conversion is much more handy using cell magics.

Converting an RPy2 ListVector to a Python dictionary

I think to get a r vector into a dictionary does not have to be so involving, how about this:

In [290]:

dict(zip(a.names, list(a)))
Out[290]:
{'fizz': <FloatVector - Python:0x08AD50A8 / R:0x10A67DE8>
[123.000000],
'foo': <StrVector - Python:0x08AD5030 / R:0x10B72458>
['barbat']}
In [291]:

dict(zip(a.names, map(list,list(a))))
Out[291]:
{'fizz': [123.0], 'foo': ['barbat']}

And of course, if you don't mind using pandas, it is even easier. The result will have numpy.array instead of list, but that will be OK in most cases:

In [294]:

import pandas.rpy.common as com
com.convert_robj(a)
Out[294]:
{'fizz': [123.0], 'foo': array(['barbat'], dtype=object)}

How to convert None to R NULL

The Converter.register() method accepts type of an object to convert as the first argument, but you passed an instance. None is an instance of NoneType:

NoneType = type(None)
NoneType() is None # True

and you need to define conversion for NoneType, so you can just use:

none_converter.py2rpy.register(type(None), _none2null)


Related Topics



Leave a reply



Submit