python3_pandas.HDFStore_h5py_HDF5_的笔记

python3_pandas.HDFStore_h5py_HDF5_的笔记

转载注明来源: 本文链接 来自osnosn的博客,写于 2020-03-26.


h5py 的部分笔记

  • hh5py 读出属性(attrs),请参考 python3_h5py_hdf5_遍历_查看文件结构
  • h5py 写入属性(attrs) hdf=h5py.File('xxx.hdf5','w')
    • 在root创建属性 hdf.attrs.create('myAttrsName',data='xxxxxx',dtype='S64')
      • dtype 用'S32'=32字节字符串,'f8'=float64, 'i4'=int32, ...
    • 在其他节点创建属性dnode=hdf.create_dataset(..) , gnode=hdf.create_group(..)
      • dnode.attrs.create('myAttrsName',data='xxxxxx',dtype='S64')
      • gnode.attrs.create('myAttrsName',data='xxxxxx',dtype='S64')
    • ...attrs.create(..) 中,data=的值中含有dtype,则不用另外指定dtype。否则建议指定 dtype 为numpy支持的类型。
    • h5py 的文档有详细描述 h5py.Attributes

pandas.HDFStore 的部分笔记

  • HDFStore 可以保存Series,DataFrame。
    • 保存格式 fixed,不能添加(append),只能覆盖(重写)
    • 保存格式 table,可以添加(append),可以覆盖(重写)
  • HDFStore 打开mode='w'时。会truncate文件,从零开始。
  • HDFStore 打开mode='a'时。为修改模式。
    • 用put覆盖fixed表(内容不变),文件也会增大。增大的比table表多。
    • 用put覆盖table表(内容不变),文件也会增大。增大的比fixed表少。
  • HDFStore 读出属性(attrs) hdf=pandas.HDFStore('xxx.hdf5','r') 以下任意一款都能用。
    • aa=hdf.root.group2._v_attrs.MyAttr
    • aa=hdf.get_node('/group2')._v_attrs.MyAttr
    • aa=hdf.get_node('/group2')._v_attrs['MyAttr']
  • HDFStore 写入属性(attrs) hdf=pandas.HDFStore('xxx.hdf5','w') 以下任意一款都能用。
    • hdf.root.group2._v_attrs.MyAttr='中文文字'
    • hdf.get_node('/group2')._v_attrs.MyAttr=u'中文文字'
    • hdf.get_node('/group2')._v_attrs['MyAttr']='english text'
    • HDFStore 写入属性的路径如果不存在,需要先行创建。否则会出错。
      • hdf._handle 似乎就是 pytable 的底层接口,hdf._handle.create_group()就是pytable的函数。
#在root上创建group。
if 'mygroup' not in hdf.root._v_groups.keys():
    hdf._handle.create_group('/','mygroup')
#在其他路径上创建group。
if 'mygroup' not in hdf.get_node('/othergroup')._v_groups.keys():
    hdf._handle.create_group('/othergroup','mygroup')
  • HDFStore 的属性(attrs), 支持保存 list, tuple, class object, str, int, float
    • bool 保存到 attrs 中是 Bitfield, h5py读不出.
  • 属性可以加在任意一层GROUP上,也可以加在root('/')上。
  • 属性的读取/写入在pandas-1.0.3,tables-3.6.1 测试过。
  • pandas.HDFStore 的文档写的很简单。属性的操作要看pytables的文档,可是pytables的文档也写的不详细。

转载注明来源: 本文链接 来自osnosn的博客.

原文地址:https://www.cnblogs.com/osnosn/p/12910791.html