[Numpy-discussion] array not appending

Sebastian Haase haase@msg.ucsf....
Thu Dec 18 07:00:09 CST 2008


On Thu, Dec 18, 2008 at 11:44 AM, David Cournapeau
<david@ar.media.kyoto-u.ac.jp> wrote:
> Prashant Saxena wrote:
>>
>> ST = np.empty((), dtype=np.float32)
>> ST = np.append(ST, 10.0)
>>
>> This works, is it proper way to do so?
>>
>> One more prob
>>
>> ST.size returns 2.
>>
>> Why? I have added only one element.
>
> You added one element to an array which as already one element. Empty
> does not mean that the array has no items (which is not possible AFAIK),
> but that the values are 'empty' (more exactly, they are undefined
> values, since the memory emplacement has not been initialized).
>
> David

So the question remains: how to create an array of "empty" (i.e. 0) size ?
I guess, setting the first argument in empty (i.e. shape) to ()
produces a scalar value - which is probably what the one the OP saw.
I get:
>>> np.empty(()).shape
()
>>> len(np.empty(()))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: len() of unsized object
>>> np.empty((0,)).shape
(0)
>>> len(np.empty((0,)))
0

Seems all correct. Now, however,  to which axis is "append" appending
?  Especially if the array doesn't have any axis (i.e. shape = () ).
I would argue that append should throw an exception here, rather than
"implicitely" changing shape to (1,) before appending -- this is most
likely not what the user intended, see OP.

Cheers,
Sebastian Haase


More information about the Numpy-discussion mailing list