1

I deal with a lot of time series tick data, and am trying to use rpy2 to get this data from Python into R. Specifically, I'd like to transfer time series that contains millisecond timestamps (critical) and time zone information (nice-to-have).

To test transferring a timestamp, I tried:

from pytz import timezone
from datetime import datetime
import rpy2.robjects as robjects

# 1 microsecond into 2015 in US Eastern time
time = datetime(2015,1,1,0,0,0,1, tzinfo = timezone('US/Eastern'))
robjects.r.assign('time', time)

But this gave:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python\2.7\lib\site-packages\rpy2\robjects\functions.py", line 170, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "C:\Python\2.7\lib\site-packages\rpy2\robjects\functions.py", line 96, in __call__
    new_args = [conversion.py2ri(a) for a in args]
  File "C:\Python\2.7\lib\site-packages\singledispatch.py", line 210, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "C:\Python\2.7\lib\site-packages\rpy2\robjects\conversion.py", line 39, in py2ri
    raise NotImplementedError("Conversion 'py2ri' not defined for objects of type '%s'" % str(type(obj)))
NotImplementedError: Conversion 'py2ri' not defined for objects of type '<type 'datetime.datetime'>'

Seems like rpy2 does not support datetime objects... So how can I transfer time series into R?

mchen
  • 9,808
  • 17
  • 72
  • 125

1 Answers1

1

There are several kinds of date/time objects in R.

For example:

robjects.POSIXct([time])

Once you know, you can quickly add a conversion rule to rpy2

lgautier
  • 11,363
  • 29
  • 42
  • 1
    Unfortunately, the milliseconds are dropped by rpy2 when Python `datetime` is converted to R's `POSIXct`. Specifically, `robjects.POSIXct([datetime(2015,1,1,0,0,0,0)]).r_repr() == robjects.POSIXct([datetime(2015,1,1,0,0,0,1)]).r_repr()` is `True` – mchen May 18 '15 at 11:19
  • Would this help ?http://stackoverflow.com/questions/2150138/how-to-parse-milliseconds-in-r – lgautier Jun 05 '15 at 20:43