【问题标题】:Does rpy2 support millisecond timestamps and timestamps with timezones?rpy2 是否支持毫秒时间戳和带时区的时间戳?
【发布时间】:2015-07-26 03:07:03
【问题描述】:

我处理大量时间序列刻度数据,并尝试使用rpy2 将这些数据从 Python 获取到 R。具体来说,我想传输包含毫秒时间戳(关键)和时间的时间序列区域信息(值得拥有)。

为了测试传输时间戳,我尝试了:

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)

但这给了:

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'>'

好像 rpy2 不支持 datetime 对象...那么如何将时间序列传输到 R 中?

【问题讨论】:

    标签: python r timestamp rpy2


    【解决方案1】:

    R中有几种日期/时间对象。

    例如:

    robjects.POSIXct([time])
    

    知道了就可以快速添加转换规则到rpy2

    【讨论】:

    • 不幸的是,当 Python datetime 转换为 R 的 POSIXct 时,rpy2 会丢弃毫秒数。具体来说,robjects.POSIXct([datetime(2015,1,1,0,0,0,0)]).r_repr() == robjects.POSIXct([datetime(2015,1,1,0,0,0,1)]).r_repr()True
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    相关资源
    最近更新 更多