2

Python で使用したい R のデータ セットがあります。互換性のために、データを R で hdf5 形式で保存してから、 を使用して python でロードするのが最善のようですpandas.io.pytables.HDFStore。以下は、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ファイルをロードすると、完全に正常に機能します。

パンダでのファイルの読み込みを修正する (簡単な) 方法はありますか? あるいは、R オブジェクトをパンダに転送できるソリューションであれば問題ありません。


編集:以下のファイルの詳細

$ 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
4

0 に答える 0