无法在Python中加载HDF5文件

时间:2019-01-18 00:25:52

标签: python pandas numpy hdf5

我在读取hdf5文件时遇到了一个奇怪的问题。这是最新版本的Python和Numpy,以及其他软件包。我在Ubuntu 18.0.4上。我可以写hdf5文件,但是当我尝试读取hdf5文件时,它会引发此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-1-f757457e8111> in <module>
      2 df = pd.DataFrame(data={'d': ['a', 'b', 'c']})
      3 df.to_hdf('test.hdf', key='data')
----> 4 df = pd.read_hdf('test.hdf')

~/numpy_bug/lib/python3.7/site-packages/pandas/io/pytables.py in read_hdf(path_or_buf, key, mode, **kwargs)
    392                                      'contains multiple datasets.')
    393             key = candidate_only_group._v_pathname
--> 394         return store.select(key, auto_close=auto_close, **kwargs)
    395     except:
    396         # if there is an error, close the store

~/numpy_bug/lib/python3.7/site-packages/pandas/io/pytables.py in select(self, key, where, start, stop, columns, iterator, chunksize, auto_close, **kwargs)
    739                            chunksize=chunksize, auto_close=auto_close)
    740 
--> 741         return it.get_result()
    742 
    743     def select_as_coordinates(

~/numpy_bug/lib/python3.7/site-packages/pandas/io/pytables.py in get_result(self, coordinates)
   1481 
   1482         # directly return the result
-> 1483         results = self.func(self.start, self.stop, where)
   1484         self.close()
   1485         return results

~/numpy_bug/lib/python3.7/site-packages/pandas/io/pytables.py in func(_start, _stop, _where)
    732             return s.read(start=_start, stop=_stop,
    733                           where=_where,
--> 734                           columns=columns)
    735 
    736         # create the iterator

~/numpy_bug/lib/python3.7/site-packages/pandas/io/pytables.py in read(self, start, stop, **kwargs)
   2935             blk_items = self.read_index('block%d_items' % i)
   2936             values = self.read_array('block%d_values' % i,
-> 2937                                      start=_start, stop=_stop)
   2938             blk = make_block(values,
   2939                              placement=items.get_indexer(blk_items))

~/numpy_bug/lib/python3.7/site-packages/pandas/io/pytables.py in read_array(self, key, start, stop)
   2487 
   2488         if isinstance(node, tables.VLArray):
-> 2489             ret = node[0][start:stop]
   2490         else:
   2491             dtype = getattr(attrs, 'value_type', None)

~/numpy_bug/lib/python3.7/site-packages/tables/vlarray.py in __getitem__(self, key)
    679                 key += self.nrows
    680             (start, stop, step) = self._process_range(key, key + 1, 1)
--> 681             return self.read(start, stop, step)[0]
    682         elif isinstance(key, slice):
    683             start, stop, step = self._process_range(

~/numpy_bug/lib/python3.7/site-packages/tables/vlarray.py in read(self, start, stop, step)
    819             listarr = []
    820         else:
--> 821             listarr = self._read_array(start, stop, step)
    822 
    823         atom = self.atom

tables/hdf5extension.pyx in tables.hdf5extension.VLArray._read_array()

ValueError: cannot set WRITEABLE flag to True of this array

这似乎是_read_array()函数here中的2021行。

最小复制代码:

import pandas as pd
df = pd.DataFrame(data={'d': ['a', 'b', 'c']})
df.to_hdf('test.hdf', key='data')
df = pd.read_hdf('test.hdf')

1 个答案:

答案 0 :(得分:0)

事实证明这是表库中的错误,此错误已得到修复:https://github.com/PyTables/PyTables/issues/719

使用config.tomlconda update pytables升级pytables库,应该很好。

在新版本发布之前修复它的旧方法以供参考:

一旦有新版本,就应该修复。在此之前,您必须执行numpy 1.15.4版本:

pip --upgrade pytables