将pandas数据附加到hdf存储,获取';TypeError:类型为'的对象;int';没有len()';错误

将pandas数据附加到hdf存储,获取';TypeError:类型为'的对象;int';没有len()';错误,pandas,hdf5,h5py,hdf,hdfstore,Pandas,Hdf5,H5py,Hdf,Hdfstore,动机: 我有大约3000万行数据,一列是索引值,另一列是512个int32数字的列表。我希望一次只能检索一千个左右的数据,因此我希望创建某种数据存储,可以通过索引查找数据,而将其余的数据保留在磁盘上 现在数据被分成184个文件,熊猫可以打开这些文件 这就是我的数据帧的样子 df.head() 先是索引,然后是列“numpyid”,它们是大小为512的numpy数组,包含int32 int 然后我试了一下: store = pd.HDFStore('/data2.h5') store.put('i

动机:

我有大约3000万行数据,一列是索引值,另一列是512个int32数字的列表。我希望一次只能检索一千个左右的数据,因此我希望创建某种数据存储,可以通过索引查找数据,而将其余的数据保留在磁盘上

现在数据被分成184个文件,熊猫可以打开这些文件

这就是我的数据帧的样子

df.head()

先是索引,然后是列“numpyid”,它们是大小为512的numpy数组,包含int32 int

然后我试了一下:

store = pd.HDFStore('/data2.h5')
store.put('index', df, format='table', append=True)
还有这个

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-05b956667991> in <module>()
----> 1 store.put('index', df, format='table', append=True, data_columns=True)
      2 store.close

4 frames
/usr/local/lib/python3.6/dist-packages/pandas/io/pytables.py in put(self, key, value, format, index, append, complib, complevel, min_itemsize, nan_rep, data_columns, encoding, errors)
   1040             data_columns=data_columns,
   1041             encoding=encoding,
-> 1042             errors=errors,
   1043         )
   1044 

/usr/local/lib/python3.6/dist-packages/pandas/io/pytables.py in _write_to_group(self, key, value, format, axes, index, append, complib, complevel, fletcher32, min_itemsize, chunksize, expectedrows, dropna, nan_rep, data_columns, encoding, errors)
   1707             dropna=dropna,
   1708             nan_rep=nan_rep,
-> 1709             data_columns=data_columns,
   1710         )
   1711 

/usr/local/lib/python3.6/dist-packages/pandas/io/pytables.py in write(self, obj, axes, append, complib, complevel, fletcher32, min_itemsize, chunksize, expectedrows, dropna, nan_rep, data_columns)
   4141             min_itemsize=min_itemsize,
   4142             nan_rep=nan_rep,
-> 4143             data_columns=data_columns,
   4144         )
   4145 

/usr/local/lib/python3.6/dist-packages/pandas/io/pytables.py in _create_axes(self, axes, obj, validate, nan_rep, data_columns, min_itemsize)
   3811                 nan_rep=nan_rep,
   3812                 encoding=self.encoding,
-> 3813                 errors=self.errors,
   3814             )
   3815             adj_name = _maybe_adjust_name(new_name, self.version)

/usr/local/lib/python3.6/dist-packages/pandas/io/pytables.py in _maybe_convert_for_string_atom(name, block, existing_col, min_itemsize, nan_rep, encoding, errors)
   4798         # we cannot serialize this data, so report an exception on a column
   4799         # by column basis
-> 4800         for i in range(len(block.shape[0])):
   4801 
   4802             col = block.iget(i)

TypeError: object of type 'int' has no len()
给我512维的向量作为21的索引

编辑:

我试着为每个数字创建一列,所以

  df[[str(i) for i in range(512)]] = pd.DataFrame(df.NumpyIds.to_numpy(), index=df.index) 
  df.drop(columns='NumpyIds', inplace=True)
  store.put('index', df, format='table', append=True)
  store.close
这是可行的,虽然我觉得这可能是一个黑客而不是一个理想的解决办法。但现在的问题是,我似乎无法从索引中获取这些值

store.select(key='index', start=2163410)
返回

    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  ... 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
IndexID                                                                                                                                                                                                                                                                                                                                 
0 rows × 512 columns
是列名,但不是该列中的数据。而且这种方法需要大量的内存。我想知道它是否一次加载所有数据,而不是只加载指定的索引

我正在尝试的另一种解决方法是直接在h5py中打开数据

df = pd.read_hdf(hdf_files[0])
df.set_index('IndexID', inplace=True)
df.to_hdf('testhdf.h5', key='df')
h = h5py.File('testhdf.h5')
但我似乎不知道如何通过索引从这个存储中检索数据

h['df'][2163410]

/usr/local/lib/python3.6/dist-packages/h5py/_hl/base.py in _e(self, name, lcpl)
    135         else:
    136             try:
--> 137                 name = name.encode('ascii')
    138                 coding = h5t.CSET_ASCII
    139             except UnicodeEncodeError:

AttributeError: 'int' object has no attribute 'encode'

据我所知,这是一个BUG。


我已经修好了。现在它显示相应的错误消息

df = pd.read_hdf(hdf_files[0])
df.set_index('IndexID', inplace=True)
df.to_hdf('testhdf.h5', key='df')
h = h5py.File('testhdf.h5')
h['df'][2163410]

/usr/local/lib/python3.6/dist-packages/h5py/_hl/base.py in _e(self, name, lcpl)
    135         else:
    136             try:
--> 137                 name = name.encode('ascii')
    138                 coding = h5t.CSET_ASCII
    139             except UnicodeEncodeError:

AttributeError: 'int' object has no attribute 'encode'