我在探索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解決了。 –
有關如何更新到最新版本的任何說明? RHEL版本庫甚至不支持熊貓(我可以看到),這就是爲什麼我使用Anaconda –
另外,我在編寫具有多索引和其他更復雜示例的數據框時遇到了其他問題。我還有其他的工作要參加,所以會在另一個帖子中提出......最有可能的下週 –