【问题标题】:Put operation hangs放置操作挂起
【发布时间】:2013-08-18 20:07:28
【问题描述】:

我正在探索 Python,特别是带有 HDF5 的 Pandas,是否适合进行一些时间序列建模......其结果是我几乎没有任何经验(还没有!)这些,所以请原谅任何愚蠢的问题。

切入正题,即使将最基本的虚拟数据插入 HDF5 文件,我也遇到了一些问题。我正在关注another post 中提供的代码,但是当我开始以存储格式编写代码时,代码执行会挂起。我还没有尝试过表格格式,我想先让它工作。我正在运行以下文件。

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 Meg 的文件,所以它似乎是大部分预期的输出文件。

最后,如果这与架构/软件风格相关。我正在运行以下命令:

机器:虚拟机,1 个 CPU,4Gb RAM,64 位
操作系统:Red Hat Enterprise Linux 6(64 位)
软件:几天前通过 anaconda 安装的 Python、Pandas、PyTables 等。希望上面的脚本中打印了相关的版本号(作为 cmets!),但如果其他版本合适,请告诉我。

TIA 寻求帮助 詹姆斯

【问题讨论】:

    标签: pandas hdf5 pytables pickle


    【解决方案1】:

    我测试了你的确切配置,除了我使用 debian/squeeze

    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 不同,tables 确定类型并以本机格式编写)。您的回溯表明它在泡菜中失败了,这很奇怪;可能对 red hat linux 有一些限制,可能是 PyTables 2.4(或 pandas)中的错误。我无法重现。

    我会尝试升级到 pandas 0.12、PyTables 3.0.0,看看它是否仍然存在。

    在任何情况下,Table 格式应该适合您,并且在任何情况下都具有许多优势,请参阅here

    【讨论】:

    • 感谢您的快速回复。没错,表格格式在我的简单示例中确实有效,因此第 1 步解决了。
    • 关于如何更新到最新版本的任何说明? RHEL 存储库甚至不支持 pandas(我可以看到),这就是我使用 Anaconda 的原因
    • 另外,我在使用多索引和其他更复杂的示例编写数据帧时遇到了其他问题。我还有其他工作要处理,所以会在另一个帖子中提出……很可能下周
    • pip install pandas==0.12; pip install tables==3.0.0
    • 哇,这很容易......但我在公司防火墙后面,可以阻止任何移动的东西。我需要在它上面打一个洞。我说 pip 正在尝试连接到 pypi.python.org/simple 是否正确?
    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多