2013-08-16 51 views
1

我在探索Python,特别是带有HDF5的Pandas是否是适合做一些时间序列建模的环境...其结果是我没有任何经验(还没有!)在任何这些,请原谅任何愚蠢的问题。Put操作挂起

要切换到追逐我一直有一些问题,即使最基本的插入虚拟数据到HDF5文件。我按照another post提供的代码,但是当我以storer格式编写时,代码执行会挂起。我还没有尝试过表格格式,我想先让它工作。我正在运行以下文件。

test_put.py:

from IPython.core.debugger import Tracer; debugStart = Tracer() 
import pandas as pd 
import numpy as np 
import tables 

print "Pandas version: " + pd.__version__ # 0.11.0 
print "NumPy version: " + np.__version__ # 1.7.1 
print "Tables version: " + tables.__version__ # 2.4.0 

df = pd.DataFrame(np.random.randn(1000 * 1000, 100), 
        index=range(int(1000 * 1000)), 
        columns=['E%03d' % i for i in xrange(100)]) 

for x in range(20): 
    df['String%03d' % x] = 'string%03d' % x 

def test_storer_put(): 
    store = pd.HDFStore('test_put.h5','w') 
    debugStart() 
    store['df'] = df 
    store.close() 

def test_table_put(): 
    store = pd.HDFStore('test_put.h5','w') 
    store.put('df',df,table=True) 
    store.close() 

test_storer_put() 

在IPython中使用IPDB我有一个调用堆栈的吊线,如下粘贴。这行是调用cPickle,我认为它是某种编译库。我无法进一步进入这一行(使用's'),因此出于对问题的看法。

~/test_put.py(20)test_storer_put() 
    18  store = pd.HDFStore('test_put.h5','w') 
    19  debugStart() 
---> 20  store['df'] = df 
    21  store.close() 
    22 

    ~/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py(241)__setitem__() 
    239 
    240  def __setitem__(self, key, value): 
--> 241   self.put(key, value) 
    242 
    243  def __delitem__(self, key): 

    ~/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py(536)put() 
    534    table 
    535   """ 
--> 536   self._write_to_group(key, value, table=table, append=append, **kwargs) 
    537 
    538  def remove(self, key, where=None, start=None, stop=None): 

    ~/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py(871)_write_to_group() 
    869    raise ValueError('Compression not supported on non-table') 
    870 
--> 871   s.write(obj = value, append=append, complib=complib, **kwargs) 
    872   if s.is_table and index: 
    873    s.create_index(columns = index) 

    ~/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py(2005)write() 
    2003    blk = data.blocks[i] 
    2004    # I have no idea why, but writing values before items fixed #2299 
-> 2005    self.write_array('block%d_values' % i, blk.values) 
    2006    self.write_index('block%d_items' % i, blk.items) 
    2007 

    ~/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py(1799)write_array() 
    1797    vlarr = self._handle.createVLArray(self.group, key, 
    1798            _tables().ObjectAtom()) 
-> 1799    vlarr.append(value) 
    1800   elif value.dtype.type == np.datetime64: 
    1801    self._handle.createArray(self.group, key, value.view('i8')) 

    ~/anaconda/lib/python2.7/site-packages/tables/vlarray.py(462)append() 
    460   atom = self.atom 
    461   if not hasattr(atom, 'size'): # it is a pseudo-atom 
--> 462    sequence = atom.toarray(sequence) 
    463    statom = atom.base 
    464   else: 

    ~/anaconda/lib/python2.7/site-packages/tables/atom.py(1000)toarray() 
    998 
    999  def toarray(self, object_): 
-> 1000   buffer_ = self._tobuffer(object_) 
    1001   array = numpy.ndarray(buffer=buffer_, dtype=self.base.dtype, 
    1002        shape=len(buffer_)) 

> ~/anaconda/lib/python2.7/site-packages/tables/atom.py(1112)_tobuffer() 
    1110 
    1111  def _tobuffer(self, object_): 
-> 1112   return cPickle.dumps(object_, cPickle.HIGHEST_PROTOCOL) 
    1113 
    1114  def fromarray(self, array): 

在挂线范围的参数有:

ipdb> a 
self = ObjectAtom() 
object_ = [['string000' 'string001' 'string002' ..., 'string017' 'string018' 
    'string019'] 
['string000' 'string001' 'string002' ..., 'string017' 'string018' 
    'string019'] 
['string000' 'string001' 'string002' ..., 'string017' 'string018' 
    'string019'] 
..., 
['string000' 'string001' 'string002' ..., 'string017' 'string018' 
    'string019'] 
['string000' 'string001' 'string002' ..., 'string017' 'string018' 
    'string019'] 
['string000' 'string001' 'string002' ..., 'string017' 'string018' 
    'string019']] 

在逐步执行代码我注意到,BlockManagerStorer.write()方法,它是半当中上面的调用栈,是循环通过2组数据块(2002至2006年的线)。第一个循环运行正常,并且是第二个循环挂起。此外,在下一个堆栈中调用的GenericStorer.write_array()方法在第一遍中有value.dtype.type == 'numpy.float64',但在第二次中value.dtype.type == 'numpy.object'导致在io/pytables.py的第1785行中发生不同的分支。 编辑:第一遍是写一个〜800兆文件,所以它似乎是预期的输出文件的大部分。

最后如果这是架构/软件风味相关。我运行了以下内容:

:虚拟机,1个CPU,4GB内存,64位
OS:红帽企业Linux 6(64位)
软件:Python中,大熊猫,PyTables等是在几天前通过anaconda安装的。希望在上面的脚本中打印相关的版本号(作为注释!),但让我知道其他人是否合适。

TIA的任何帮助 詹姆斯

回答

1

我测试您的具体配置,我使用Debian /外挤

OS: Linux 2.6.32-5-amd64 #1 SMP Sun Sep 23 10:07:46 UTC 2012 x86_64 
In [4]: print "Pandas version: " + pd.__version__ # 0.11.0 
Pandas version: 0.11.0 

In [5]: print "NumPy version: " + np.__version__ # 1.7.1 
NumPy version: 1.7.1 

In [6]: print "Tables version: " + tables.__version__ # 2.4.0 
Tables version: 2.4.0 

在一个storer,绳状物体(如索引/列的索引)被腌渍(与tables相反,其中类型被确定并且以原生格式写入)。你的回溯表明它在腌菜中失败了,这很奇怪;可能在red hat linux上有一些限制,可能是PyTables 2.4(或熊猫)中的一个bug。我无法重现这一点。

我会尝试升级到熊猫0.12,PyTables 3.0.0,看看它是否存在。

在任何情况下,Table格式应该只是罚款为你在任何情况下提供了许多的优点,看到here

+0

感谢您的快速反应。你说得对,表格格式在我的简单例子中起作用,所以步骤1解决了。 –

+0

有关如何更新到最新版本的任何说明? RHEL版本库甚至不支持熊猫(我可以看到),这就是为什么我使用Anaconda –

+0

另外,我在编写具有多索引和其他更复杂示例的数据框时遇到了其他问题。我还有其他的工作要参加,所以会在另一个帖子中提出......最有可能的下周 –