osnosn

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

python3_pandas.HDFStore_h5py_HDF5_的笔记

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


h5py 的部分笔记

  • h5py 读出属性(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
  • h5py 说 从2.4开始就有全局锁,实现了线程安全。

pandas.HDFStore 的部分笔记

  • HDFStore 可以保存Series,DataFrame。
    • 保存格式 fixed,不能添加(append),只能覆盖(重写)
    • 保存格式 table,可以添加(append),可以覆盖(重写)
    • 保存的 DataFrame 的 column 超过 8320 个列(具体限制没找到,也可能是 8448),就无法保存到hdf5中(会出错)。
      保存前,可以考虑先 T 变换一下。但 column 名如果不是数字,可能 T 变换会失败。
    • 保存格式 table, 写入需要多消耗 DataFrame 本身的2倍大小内存,读出要多消耗4倍内存。支持数据压缩保存。
      如果写入的 DataFrame 比较大,程序可能会 OOM 而意外退出。
    • 保存格式 fixed, 写入需要多消耗 DataFrame 本身的0.2倍大小内存,读出要多消耗1倍内存。不支持压缩保存。fixed 的文件比较大。
      如果写入的数据很大,而你不需要 append、search、select 特性。读出写入都是整体操作,还是保存为 fixed 比较好
      如果写入的数据不大,选 table 比较好。
  • 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的文档也写的不详细。
  • HDFStore是基于pytable。pytable说,可多个同时读,不支持多个写,不支持一个写的同时读。会出错。建议等写完了,再重写打开来读,否则会读的不完整,而出错。
    • 多进程, 建议自己创建个文件上锁/解锁。
    • 多线程, 用线程锁解决冲突。

vaex

  • 支持打开超过内存大小的hdf5文件,用内存映射。 打开csv没有效果。
  • centos8中,用 pip3.6 安装vaex失败。

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

posted on 2020-05-18 15:29  osnosn  阅读(2691)  评论(0编辑  收藏  举报