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

25 篇文章 0 订阅
18 篇文章 3 订阅

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

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

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

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

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

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

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

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


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

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

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

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

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

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

看到结果是:


user_id                               object
version                               object
country                               object
dtype: object

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

 

2019.8.12日补充:

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

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏小败在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值