如何将datetime64 [ns]数据从rpy2传递给R?以下是简单的情况:
import pandas as pd
import rpy2.robjects as robjs
from rpy2.robjects import pandas2ri
pandas2ri.activate()
dti = pd.date_range('2018-01-01', periods=3, freq='H')
robjs.r.summary(dti)
失败,出现ValueError:ValueError: Unknown numpy array type "datetime64[ns]".
(rpy2版本3.3.4和熊猫1.0.5)。
答案 0 :(得分:0)
您不必像这样做那样将DatetimeIndex
传递给R
:
dti = pd.date_range('2018-01-01', periods=3, freq='H')
type(dti)
pandas.core.indexes.datetimes.DatetimeIndex
尝试将数据包装到DataFrame
对象中
dti = pd.DataFrame({"DatetimeIndex": pd.date_range('2018-01-01', periods=3, freq='H')})
type(dti)
pandas.core.frame.DataFrame
结果是:
robjs.r.summary(dti)
array(['Min. :2018-01-01 00:00:00 ', '1st Qu.:2018-01-01 00:30:00 ',
'Median :2018-01-01 01:00:00 ', 'Mean :2018-01-01 01:00:00 ',
'3rd Qu.:2018-01-01 01:30:00 ', 'Max. :2018-01-01 02:00:00 '],
dtype='<U29')