【发布时间】:2014-03-28 22:12:24
【问题描述】:
我正在从图像文件中读取数据,并且我想将此数据附加到单个 HDF 文件中。这是我的代码:
datafile = pd.HDFStore(os.path.join(path,'imageData.h5'))
for file in fileList:
data = {'X Position' : pd.Series(xpos, index=index1),
'Y Position' : pd.Series(ypos, index=index1),
'Major Axis Length' : pd.Series(major, index=index1),
'Minor Axis Length' : pd.Series(minor, index=index1),
'X Velocity' : pd.Series(xVelocity, index=index1),
'Y Velocity' : pd.Series(yVelocity, index=index1) }
df = pd.DataFrame(data)
datafile['df'] = df
datafile.close()
这显然是不正确的,因为每次循环运行时它都会用新数据覆盖每组数据。
如果不是datafile['df'] = df,我使用
datafile.append('df',df)
或
df.to_hdf(os.path.join(path,'imageData.h5'), 'df', append=True, format = 'table')
我得到错误:
ValueError: Can only append to Tables
我已经提到了documentation 和其他SO questions,但无济于事。
所以,我希望有人能解释为什么这不起作用,以及如何成功地将所有数据附加到一个文件中。如有必要,我愿意使用不同的方法(可能是 pyTables)。
任何帮助将不胜感激。
【问题讨论】:
-
第二种方式(
df.to_hdf(..., format="table", append=True))其实是正确的。您是否尝试过将它(没有所有HDFStore的东西)与新文件一起使用? -
@filmor 你的意思是删除我创建空 HDF 文件的行?试过了,同样的错误。也许问题在于数据在 DataFrame 中而不是在表中?
-
否,错误消息是指使用的内部 HDF5 表格式。旧版 pandas 中的 IIRC(顺便说一句,您使用的是哪一个?)
HDFStore默认使用不允许附加的fixed格式。table格式是 PyTables 使用的格式。 -
版本 0.11.0 - 我在一个新文件上尝试过,它可以工作,但没有 for 循环。
-
如果你使用
format="table"to_hdf应该允许在内部使用 PyTables 进行追加,不需要自己做。不过,您可能想要更新 pandas。 “并且成功了”是什么意思?你会更新问题吗?
标签: python pandas hdf5 pytables