【问题标题】:How to ensure a numeric matrix is retrieved from R如何确保从 R 中检索数字矩阵
【发布时间】:2021-04-17 17:29:43
【问题描述】:

在使用default_converterpandas2ri.converter 本地转换器建立的本地转换上下文中,我获得了一个rpy2.robjects.vectors.BoolMatrix 对象(分配给变量res):

<rpy2.robjects.vectors.BoolMatrix object at 0x7fa48d531040> [RTYPES.LGLSXP]
R classes: ('matrix', 'array')
[NA, NA, NA]

我需要从这个对象构建一个 pandas 数据框。但是,转换给出:

pd.DataFrame(res)
            0
0 -2147483648
1 -2147483648
2 -2147483648

如何确保将这种矩阵转换为nan 值?

【问题讨论】:

    标签: python rpy2


    【解决方案1】:

    这可能是 rpy2 方面的转换 buglet。推荐的方法是 1)在 Github 上的 rpy2 跟踪器上打开一个问题,2)在修复之前有以下解决方法:

    import math
    import rpy2.robjects as ro
    
    # Let dataf_pd be the pandas DataFrame resulting from the conversion from
    # an R data.frame and "x" be the name of the column of booleans with NAs.
    dataf_pd.loc[dataf_pd.x == ro.NA_Logical] = math.nan
    

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多