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

9

我对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电脑。我一直在寻找解决方法,但却找不到。有谁可以指点一下吗?


1
enums.DataPoints是什么? - Jaime
您的回溯中的代码与您发布的代码不匹配。在回溯中,指定了“dtype”(正如@Jaime所询问的那样),但在您在顶部发布的代码中未指定。如果您进行了更改,请确保保存(并重新加载,如果是交互式的)源文件。 - askewchan
我添加了enums.DataPoints,谢谢。 - Ben Taliadoros
这导致了错误:_p = np.empty(1, dtype=[('p', np.uint16), ('pr', np.uint16)]) 然后 _p[:]=1 但不是这个:_p = np.empty(2, dtype=[('p', np.uint16), ('pr', np.uint16)]) 然后 _p[:]=1,看起来像是一个 bug(1.8.2)。 - dashesy
2个回答

5

不知道enums.DataPoints数据类型对象的具体情况,很难给出准确的回答。但是我会尝试解释你为什么会看到这个错误信息。

当您尝试将数组(或其元素)设置为与其数据类型不对齐的值时,就会看到此错误。以下是一个示例:

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对齐。

代码在我的同事们的机器上(Windows 7)运行良好。从askewchan的回答中,我不清楚为什么会出现这个问题,特别是我们正在使用numpy.zeros_like() --从文档中,这应该基本上复制先前的数组? - Ben Taliadoros
1
没错,在我的电脑上(Python 2.7,NumPy 1.7)也可以正常运行,现在我已经按照你的要求设置了“DataPoints”。你使用的是哪个版本的NumPy/Python? - askewchan
我正在使用numpy 1.6.1版本和python 2.7.2。 - Ben Taliadoros
谢谢您的帮助,我发现我的numpy版本过旧,我已经更新到1.8.0,错误已经消失了。谢谢。 - Ben Taliadoros
2
很好,@Ben。如果这回答了你的问题,请接受我的回答或者添加你自己的回答并接受它,以标记问题已解决。谢谢! - askewchan

1
这个问题的答案是我使用的Numpy版本与我的同事们使用的版本不同,正如@askewchan在他的回答评论中指出的那样。
  • 失败:numpy 1.6.1
  • 成功:numpy 1.7
  • 成功:numpy 1.8.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接