12

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.

1
  • 2
    Not a solution to your problem, but why are you trying to stick arbitrary Python objects into an array of dtype int32? Also, if you want to generate an empty array, there's numpy.zeros, ones, or empty. You don't need to create a Python list with range to initialize it. Commented Dec 9, 2013 at 23:19

1 Answer 1

9

@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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.