Python:如何在Python中处理预期的可读缓冲区对象

时间:2013-11-12 16:32:11

标签: python numpy

我对python很新,我正在处理一个从我工作的人那里继承的python脚本,我收到了这个错误:

expected a readable buffer object

导致此问题的代码是:

self.y_NoShock_data = np.zeros((self.a_count+1,1,self.numberOfTags+1,lookback+forward,),dtype=enums.    
self.y_data = np.zeros((self.a_count+1,len(self.SCL)+1,self.numberOfTags+1,lookback+forward,),dtype=enums.DataPoints)

self.y_NoShock_cum_data = np.zeros_like(self.y_NoShock_data)
self.y_cum_data = np.zeros_like(self.y_data)

enums.DataPoints看起来像这样:

enums.DataPoints = dtype([
        ('Amount','float32'),
        ])

堆栈跟踪如下:

Internal Server 

Error: /smCore/entity/1/runScenarioManager/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/views/createScenario.py", line 445, in runScenarioManager
    a = ScenarioExecutionController(sEA)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecutionController.py", line 176, in __init__
    shockEventDataSet=[], lookback=self.lookback, forward=self.forward, period=self.period) #,
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 307, in buildSeedScenarioObject
    cls.updateScenarioParameters(shockContainerList,shockEventDataSet, shockEventDateList)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 130, in updateScenarioParameters
    self.initialiseResultArrays()
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 154, in initialiseResultArrays
    self.y_NoShock_cum_data = np.zeros_like(self.y_NoShock_data,dtype=enums.DataPoints)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/numeric.py", line 116, in zeros_like
    res.fill(0)
TypeError: expected a readable buffer object

他在电脑上工作,我在Mac上。 我一直在环顾四周,但无法找到解决方案,有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:5)

如果不知道enums.DataPoints dtype对象的样子,很难回复,但我会尝试解释您在哪里看到错误消息。

当您尝试将某个数组(元素)设置为某个与其dtype无法正确对齐的值时,您会看到这一点。这是一个例子:

In [133]: data = np.zeros((3,2), dtype="int, int")

In [134]: data
Out[134]: 
array([[(0, 0), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

In [135]: data[0, 0]
Out[135]: (0, 0)

In [136]: data[0, 0] = [1,2]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-399e78675be4> in <module>()
----> 1 data[0, 0] = [1,2]

TypeError: expected a readable buffer object

这会产生错误,因为它无法处理分配给数组的一个元素的两个值。你的dtype中有两个值,所以看起来似乎是合理的,但是数组需要一个由dtype给出的类型的对象:

In [137]: data[0,0] = (1,2)

In [138]: data
Out[138]: 
array([[(1, 2), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

阵列的相同形状的一组零可能不会与dtype对齐。

答案 1 :(得分:1)

对此的答案是,我没有在同事正在使用的Numpy版本下工作,正如@askewchan在回答他的评论中指出的那样。

  • 失败:numpy 1.6.1
  • 作品:numpy 1.7
  • 作品:numpy 1.8.0