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?
1 Answer 1
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)
-
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.pv.– pv.2011年05月31日 08:33:22 +00:00Commented May 31, 2011 at 8:33
-
1Is there a difference in performance? Is it possible to do vectorized computation in C with object arrays like those for numeric arrays?Fırat Kıyak– Fırat Kıyak2022年01月21日 15:01:20 +00:00Commented Jan 21, 2022 at 15:01