datetime64 [ns]熊猫系列出现rpy2错误

时间:2020-06-26 17:25:58

标签: pandas numpy rpy2

如何将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)。

1 个答案:

答案 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')