程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Python-Pandas学习之HDFStore存储数据警告(your performance may suffer as PyTables will pickle....)

发布于2019-08-28 11:41     阅读(5487)     评论(0)     点赞(27)     收藏(0)


这是一个类似数据表字典的格式,可以将很多的数据帧(dataframe)保存在一个对象里面。

每一个数据帧,都标有一个key,然后通过key来访问数据帧的数据。

但是,在使用HDF的时候,如果不指定格式,那么我们数据中存在string类型的数据,就会报以下警告:

  1. PerformanceWarning:
  2. your performance may suffer as PyTables will pickle object types that it cannot
  3. map directly to c-types [inferred_type->mixed,key->block3_values] [items->['user_id', 'version', 'country']]

我的这几列都是string类型的,虽然是警告,也能存进去,但是如果要读取出来就会报错:

  1. Traceback (most recent call last):
  2. File "/Users/guojicheng/Desktop/Python/3/Projects/CsvToDatabase4/maintest-hdf.py", line 28, in <module>
  3. df = hstore.get('eos_20190612')
  4. File "/anaconda3/lib/python3.7/site-packages/pandas/io/pytables.py", line 695, in get
  5. return self._read_group(group)
  6. File "/anaconda3/lib/python3.7/site-packages/pandas/io/pytables.py", line 1423, in _read_group
  7. return s.read(**kwargs)
  8. File "/anaconda3/lib/python3.7/site-packages/pandas/io/pytables.py", line 2995, in read
  9. start=_start, stop=_stop)
  10. File "/anaconda3/lib/python3.7/site-packages/pandas/io/pytables.py", line 2540, in read_array
  11. ret = node[0][start:stop]
  12. File "/anaconda3/lib/python3.7/site-packages/tables/vlarray.py", line 681, in __getitem__
  13. return self.read(start, stop, step)[0]
  14. File "/anaconda3/lib/python3.7/site-packages/tables/vlarray.py", line 821, in read
  15. listarr = self._read_array(start, stop, step)
  16. File "tables/hdf5extension.pyx", line 2155, in tables.hdf5extension.VLArray._read_array
  17. ValueError: cannot set WRITEABLE flag to True of this array
  18. Closing remaining open files:eos_data.h5...done

这个其实就是因为之前的警告引起的,那为什么string类型就不行呢?

因为我们的数据,默认都是unicode编码格式,而这个hdf对unicode的支持并不好,因此,如果有字符串的列,我们需要去转换一次:

  1. df['user_id'] = df['user_id'].str.decode('utf-8')
  2. df['version'] = df['version'].str.decode('utf-8')
  3. df['country'] = df['country'].str.decode('utf-8')

这么转了之后,就不会出现警告了。但是当我读取出来的时候数据都变成了 float64,还需要转成int,在转化成string,也就是encode,很是麻烦。

下面还有一个方式,我们在存入数据的时候,指定下一存储格式,这种方式不需要做转换就可以:

  1. hstore = pd.HDFStore('eos_data.h5', mode='w')
  2. df = pd.read_csv(
  3. 'testdata.csv',
  4. )
  5. print(df.dtypes)
  6. hstore.put('eos_20190612', df, format='table', append=False) # 指定了 format 为 table
  7. hstore.close()

上面我指定格式为 table,这也没有警告。下面我们在读取出来看看,数据的类型有没有变:

  1. hstore = pd.HDFStore('eos_data.h5', mode='r')
  2. df = hstore.get('eos_20190612')
  3. print(df.dtypes)
  4. hstore.close()

看到结果是:

  1. user_id object
  2. version object
  3. country object
  4. dtype: object

所以需要只需要设置 format = ‘table’ 就可以了~

 

2019.8.12日补充:

最近我把pandas升级到最新的之后,也可以使用fixed模式了。但是fixed模式只是和读取和存储,不能做查询之类的操作,而且也不能使用append。这也是由于的写入的格式的二进制的,没法直接操作。所以fixed的模式,读写速度超级快,差不多是table的四到五倍,数据越大越多速度差异越明显~



所属网站分类: 技术文章 > 博客

作者:听爸爸的话

链接:https://www.pythonheidong.com/blog/article/65781/e15c7860c93d0d2bd0fb/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)