对 Pandas 和 HDF5 链接的额外研究揭示了一个有趣的发现:链接存在局限性(您可以在 Pandas 中创建它们,但 Pandas 无法访问链接数据)。换句话说,链接就在那里,并且可以与 HDFView、h5py 和 PyTables 一起正常工作。参考这些 GitHub 问题:
下面的代码显示了如何创建这两种链接类型。它还显示您在尝试访问链接数据时将收到的错误消息。 (错误消息是:KeyError: 'you cannot get attributes from this 'NoAttrs' instance。这是由于 HDF5 限制属性对链接的限制。HDFStore 节点具有一些必需的属性。当 Pandas 尝试读取属性时,结果是“NoAttrs”消息。
import pandas as pd
df1 = pd.DataFrame({ "a": [1,2,3,4], "b": [11,12,13,14] })
print(df1.to_string())
# Create file 1 with simple dataframe
f1 = "test_1.hdf"
with pd.HDFStore(f1, mode="w") as hdf1:
hdf1.put("/key1", df1)
# Create file 2 with external link
f2 = "test_extlink.hdf"
with pd.HDFStore(f2, mode="w") as hdf2:
hdf2._handle.create_external_link(hdf2._handle.root, "extlink_key1", f"{f1}:/key1")
print("Successful external link write")
with pd.HDFStore(f2, mode="r") as hdf2:
print(hdf2.keys()) # Notice that [] (no keys) is printed
# following lines will trigger the 'NoAttrs' error message
# df2test = pd.read_hdf(f2,key="extlink_key1")
# print(df2test.to_string())
print("End external link read")
# Create file 3 with simple dataframe and symbolic (soft) link
f3 = "test_symlink.hdf"
with pd.HDFStore(f3, mode="w") as hdf3:
hdf3.put("/key1", df1)
hdf3._handle.create_soft_link(hdf3._handle.root, "symlink_key1", "/key1")
print("Successful symbolic link write")
with pd.HDFStore(f3, mode="r") as hdf3:
print(hdf3.keys()) # Notice that only ['key1'] is printed
# following lines will trigger the 'NoAttrs' error message
# df3test = pd.read_hdf(f3,key="symlink_key1")
# print(df3test.to_string())
print("End symbolic link read")