6

Since when did numpy allow you to define an array of python objects? Objects array with numpy.

(削除) Is there any fundamental difference between these arrays and a python list? (削除ここまで)

What is the difference between these arrays and say, a python tuple?

There are several handy numpy functions I would like to use, i.e. masks and element-wise operations, on an array of python objects and I would like to use them in my analysis, but I'm worried about using a feature I can't find documentation for anywhere. Is there any documentation for this 'object' datatype?

Was this feature was added in preparation for merging numpy into the standard library?

asked May 26, 2011 at 16:24

1 Answer 1

9

The "fundamental" difference is that a Numpy array is fixed-size, while a Python list is a dynamic array.

>>> class Foo:
... pass
... 
>>> x = numpy.array([Foo(), Foo()])
>>> x.append(Foo())
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'

(You can get around this with numpy.concatenate, but still Numpy arrays aren't meant as a drop-in replacement for list.)

Arrays of object are perfectly well documented, but be aware that you'll have to pass dtype=object sometimes:

>>> numpy.array(['hello', 'world!'])
array(['hello', 'world!'], 
 dtype='|S6')
>>> numpy.array(['hello', 'world!'], dtype=object)
array(['hello', 'world!'], dtype=object)
jamylak
134k30 gold badges238 silver badges239 bronze badges
answered May 26, 2011 at 16:29
2
  • More of the object arrays here: docs.scipy.org/doc/numpy/reference/arrays.scalars.html They have been a part of Numpy since the beginning. Commented May 31, 2011 at 8:33
  • 1
    Is there a difference in performance? Is it possible to do vectorized computation in C with object arrays like those for numeric arrays? Commented Jan 21, 2022 at 15:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.