使用hdf5将数据从R传输到pandas

时间:2013-09-24 09:56:06

标签: python r pandas hdf5

我在R中有一个我想在Python中使用的数据集。为了兼容性,最好将数据以hdf5格式保存在R中,然后使用pandas.io.pytables.HDFStore在python中加载它。下面是从R生成hdf5对象的代码。

library(rhdf5)
mat = matrix(data = rexp(200, rate = 10), nrow = 10, ncol = 10)
colnames(mat) = c(1:10)
rownames(mat)= c('A','B','C','D','E','F','G','H','I','K')
vec = c(1:10)
xx = list(mat=mat, vec=vec)
h5save(xx, file='xx.h5')

但是,当我尝试在pandas中加载它时,store.keys()为空并且直接访问包含的对象会引发错误:

from pandas.io.pytables import HDFStore
store = HDFStore('xx.h5')
store.keys() # returns []

当我尝试.get('xx')时,错误与我提供无效对象名称时的错误不同:

store.get('xx')
# TypeError: cannot create a storer if the object is not existing nor a value are passed
store.get('invalid')
# KeyError: 'No object named invalid in the file'

在R中加载hdf5文件非常合适。

有没有(简单)方法来修复pandas中的文件加载?或者,我也可以使用任何允许我将R对象转移到pandas的解决方案。


编辑以下文件详情

$ ptdump-2.7 -av xx.h5 
/ (RootGroup) ''
  /._v_attrs (AttributeSet), 0 attributes
/xx (Group) ''
  /xx._v_attrs (AttributeSet), 0 attributes
/xx/mat (CArray(10, 10), zlib(7)) ''
  atom := Float64Atom(shape=(), dflt=0.0)
  maindim := 0
  flavor := 'numpy'
  byteorder := 'little'
  chunkshape := (10, 10)
  /xx/mat._v_attrs (AttributeSet), 0 attributes
/xx/vec (CArray(10,), zlib(7)) ''
  atom := Int32Atom(shape=(), dflt=0)
  maindim := 0
  flavor := 'numpy'
  byteorder := 'little'
  chunkshape := (10,)
  /xx/vec._v_attrs (AttributeSet), 0 attributes

0 个答案:

没有答案