簡體   English   中英

如何在現有HDF5數據集中插入/編輯列

[英]How to insert/edit a column in an existing HDF5 dataset

我有一個HDF5文件,如下所示。 我想編輯索引列並創建一個新的時間戳索引。 有什么辦法嗎?

在此處輸入圖片說明

除非您首先具有用於創建HDF5文件的方案/規范,否則這是不可能的。

如果您嘗試使用HDF5文件(如電子表格)(甚至通過h5py),可能會出錯。 例如:

  • 塊形狀,壓縮,數據類型不一致。
  • 同類數據變得非同類。

您可以做的是將列表作為屬性添加到數據集。 實際上,這可能是正確的事情。 下面的示例代碼,輸入為字典。 當您讀入數據時,會將屬性鏈接到同類數據(按行,列或其他標識符)。

def add_attributes(hdf_file, attributes, path='/'):

    """Add or change attributes in path provided.
    Default path is root group.
    """

    assert os.path.isfile(hdf_file), "File Not Found Exception '{0}'.".format(hdf_file)
    assert isinstance(attributes, dict), "attributes argument must be a key: value dictionary: {0}".format(type(attributes))

    with h5py.File(hdf_file, 'r+') as hdf:
        for k, v in attributes.items():
            hdf[path].attrs[k] = v

    return "The following attributes have been added or updated: {0}".format(list(attributes.keys()))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM