我有一个通过HDFStore存储的Pandas数据帧,该HDFStore基本上存储关于我正在进行的测试运行的摘要行。
每行中有几个字段包含可变长度的描述性字符串。
当我进行测试运行时,我将创建一个新的数据帧,其中包含一行:
def export_as_df(self):
return pd.DataFrame(data=[self._to_dict()], index=[datetime.datetime.now()])
然后调用HDFStore.append(string, DataFrame)
将新行添加到现有数据帧。
这很好,除了其中一个字符串列的内容大于现有的最长实例之外,因此我得到以下错误:
File "<ipython-input-302-a33c7955df4a>", line 516, in save_pytables
store.append('tests', test.export_as_df())
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/io/pytables.py", line 532, in append
self._write_to_group(key, value, table=True, append=True, **kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/io/pytables.py", line 788, in _write_to_group
s.write(obj = value, append=append, complib=complib, **kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/io/pytables.py", line 2491, in write
min_itemsize=min_itemsize, **kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/io/pytables.py", line 2254, in create_axes
raise Exception("cannot find the correct atom type -> [dtype->%s,items->%s] %s" % (b.dtype.name, b.items, str(detail)))
Exception: cannot find the correct atom type -> [dtype->object,items->Index([bp, id, inst, per, sp, st, title], dtype=object)] [values_block_3] column has a min_itemsize of [51] but itemsize [46] is required!
我找不到任何关于如何在创建数据帧时指定字符串长度的文档。这里的解决方案是什么?
更新:
失败的代码:
store = pd.HDFStore(pytables_store)
for test in self.backtests:
try:
min_itemsizes = { 'buy_pattern' : 60, 'sell_pattern': 60, 'strategy': 60, 'title': 60 }
store.append('tests', test.export_as_df(), min_itemsize = min_itemsizes)
以下是0.11rc1下的错误:
File "<ipython-input-110-492b7b6603d7>", line 522, in save_pytables
store.append('tests', test.export_as_df(), min_itemsize = min_itemsizes)
File "/Users/admin/dev/pandas/pandas-0.11.0rc1/pandas/io/pytables.py", line 610, in append
self._write_to_group(key, value, table=True, append=True, **kwargs)
File "/Users/admin/dev/pandas/pandas-0.11.0rc1/pandas/io/pytables.py", line 871, in _write_to_group
s.write(obj = value, append=append, complib=complib, **kwargs)
File "/Users/admin/dev/pandas/pandas-0.11.0rc1/pandas/io/pytables.py", line 2707, in write
min_itemsize=min_itemsize, **kwargs)
File "/Users/admin/dev/pandas/pandas-0.11.0rc1/pandas/io/pytables.py", line 2447, in create_axes
self.validate_min_itemsize(min_itemsize)
File "/Users/admin/dev/pandas/pandas-0.11.0rc1/pandas/io/pytables.py", line 2184, in validate_min_itemsize
raise ValueError("min_itemsize has [%s] which is not an axis or data_column" % k)
ValueError: min_itemsize has [buy_pattern] which is not an axis or data_column
数据样本:
all_day buy_pattern \
2013-04-14 12:11:44.377695 False Hammer() and LowerLow()
id instrument \
2013-04-14 12:11:44.377695 tafdcc96ba4eb11e2a86d14109fcecd49 EURUSD
open_margin periodicity sell_pattern strategy \
2013-04-14 12:11:44.377695 0.0001 1:00:00 Tsl()
title top_bottom wick_body
2013-04-14 12:11:44.377695 tsl 0.5 2
数据类型:
print prob_test.export_as_df().get_dtype_counts()
bool 1
float64 2
int64 1
object 7
dtype: int64
我每次都要删除h5文件,因为我想要干净的结果。想知道在第一次追加时h5中不存在df(因此也不存在任何列),是否有像它失败这样愚蠢的事情?
这是关于这个的新文档部分的链接:http://pandas.pydata.org/pandas-docs/stable/io.html#string-columns
此问题是,您在min_itemsize中指定的列不是数据列。简单的解决方法是将
data_columns=True
添加到append语句中,但我也更新了代码,以便在传递有效的列名时自动创建数据列。我认为这是有道理的,你想有一个最小的列大小,所以让它发生。还创建了一个新的doc节字符串列,以显示一个更完整的解释示例(文档将很快更新)。
相关问题 更多 >
编程相关推荐