Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating numpy array of custom objects gives error "SystemError: error return without exception set"

Tags:

python

numpy

I am trying to use numpy to store some custom objects I've made. The following is a simplified version of my program

import numpy as np

class Element:
    def __init__(self): pass

a = Element()
periodicTable = np.array(range(7*32)).reshape((7,32))
periodicTable[0][0] = a

However when I run this I get

Traceback (most recent call last):
  File "C:/Users/Dan/Desktop/a.py", line 9, in <module>
    periodicTable[0][0] = a
SystemError: error return without exception set

I'm not really sure what I'm doing wrong - as far as I can tell everything I've done should be legal. The cryptic error message itself isn't very helpful - I believe that is a numpy issue however I've been unable to identify my problem.

like image 956
Dan Oberlam Avatar asked Dec 09 '13 23:12

Dan Oberlam


People also ask

What does [: :] mean on NumPy arrays?

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])

What is __ Array_interface __?

__array_interface__ A dictionary of items (3 required and 5 optional). The optional keys in the dictionary have implied defaults if they are not provided. The keys are: shape (required) Tuple whose elements are the array size in each dimension.

What does .all do in NumPy?

all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.


1 Answers

@user2357112 identified the problem: you are assigning an Element instance to a numpy array that holds integers. This is what I get when I try something similar:

>>> import numpy as np
>>> np.__version__
'1.7.1'
>>> p = np.array([1,2,3])
>>> class Foo:
...     pass
... 
>>> p[0] = Foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: error return without exception set
>>> 

It is not surprising that this is not allowed. The cryptic error message, however, is almost certainly a numpy bug.

One way to fix the issue is to use an array of type object. Change this line:

    periodicTable = np.array(range(7*32)).reshape((7,32))

to this:

    periodicTable = np.empty((7,32), dtype=object)

Update

In numpy 1.10.1, the error message is still a bit cryptic:

>>> import numpy as np
>>> np.__version__
'1.10.1'    
>>> p = np.array([1, 2, 3])
>>> class Foo:
...     pass
...  
>>> p[0] = Foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute '__trunc__'

Update 2

The error message is better is later versions of numpy:

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.12.1'

In [3]: class Foo:
   ...:     pass
   ...: 

In [4]: p = np.array([1, 2, 3])

In [5]: p[0] = Foo()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-739d5e5f795b> in <module>()
----> 1 p[0] = Foo()

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Foo'
like image 121
Warren Weckesser Avatar answered Oct 16 '22 05:10

Warren Weckesser