使用后出现以下错误pandas.HDFStore().append()
ValueError: Trying to store a string with len [150] in [values_block_0] column but this column has a limit of [127]!
Consider using min_itemsize to preset the sizes on these columns
我正在创建一个 pandas DataFrame 并将其附加到 HDF5 文件中,如下所示:
import pandas as pd
store = pd.HDFStore("test1.h5", mode='w')
hdf_key = "one_key"
columns = ["col1", "col2", ... ]
df = pd.Dataframe(...)
df.col1 = df.col1.astype(str)
df.col2 = df.col2astype(int)
df.col3 = df.col3astype(str)
....
store.append(hdf_key, df, data_column=columns, index=False)
我收到上面的错误:“ValueError: Trying to store a string with len [150] in [values_block_0] column but this column has a limit of [127]!”
之后,我执行代码:
store.get_storer(hdf_key).table.description
哪个输出
{
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": StringCol(itemsize=127, shape=(5,), dflt=b'', pos=1),
"values_block_1": Int64Col(shape=(5,), dflt=0, pos=2),
"col1": StringCol(itemsize=20, shape=(), dflt=b'', pos=3),
"col2": StringCol(itemsize=39, shape=(), dflt=b'', pos=4)}
values_block_0
和是什么values_block_1
?
所以,按照这个 StackOverflow Pandas pytable: how to specify min_itemsize of the elements of a MultiIndex,我试过了
store.append(hdf_key, df, data_column=columns, index=False, min_itemsize={"values_block_0":250})
这不起作用——现在我收到这个错误:
ValueError: Trying to store a string with len [250] in [values_block_0] column but this column has a limit of [127]!
Consider using min_itemsize to preset the sizes on these columns
我究竟做错了什么?
编辑:此代码产生ValueError: min_itemsize has the key [values_block_0] which is not an axis or data_column
错误filename.py
import pandas as pd
store = pd.HDFStore("test1.h5", mode='w')
hdf_key = "one_key"
my_columns = ["col1", "col2", ... ]
df = pd.Dataframe(...)
df.col1 = df.col1.astype(str)
df.col2 = df.col2astype(int)
df.col3 = df.col3astype(str)
....
store.append(hdf_key, df, data_column=my_columns, index=False, min_itemsize={"values_block_0":350})
这是完整的错误:
(python-3) -bash:1008 $ python filename.py
Traceback (most recent call last):
File "filename.py", line 50, in <module>
store.append(hdf_key, dicts_into_df, data_column=my_columns, index=False, min_itemsize={'values_block_0':350})
File "/path/lib/python-3/lib/python3.5/site-packages/pandas/io/pytables.py", line 970, in append
**kwargs)
File "/path/lib/python-3/lib/python3.5/site-packages/pandas/io/pytables.py", line 1315, in _write_to_group
s.write(obj=value, append=append, complib=complib, **kwargs)
File "/path/lib/python-3/lib/python3.5/site-packages/pandas/io/pytables.py", line 4263, in write
obj=obj, data_columns=data_columns, **kwargs)
File "/path/lib/python-3/lib/python3.5/site-packages/pandas/io/pytables.py", line 3853, in write
**kwargs)
File "/path/lib/python-3/lib/python3.5/site-packages/pandas/io/pytables.py", line 3535, in create_axes
self.validate_min_itemsize(min_itemsize)
File "/path/lib/python-3/lib/python3.5/site-packages/pandas/io/pytables.py", line 3174, in validate_min_itemsize
"data_column" % k)
ValueError: min_itemsize has the key [values_block_0] which is not an axis or data_column