[Numpy-discussion] Bug in resize method?

Charles R Harris charlesr.harris at gmail.com
Wed Aug 29 12:28:21 EDT 2007


On 8/29/07, Stefan van der Walt <stefan at sun.ac.za> wrote:
>
> Hi Charles
>
> On Wed, Aug 29, 2007 at 09:42:50AM -0600, Charles R Harris wrote:
> > Hi all,
> >
> > This looks like a bug to me.
> >
> > >>> a = arange(6).reshape(2,3)
> > >>> a.resize((3,3))
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > ValueError: cannot resize this array:  it does not own its data
>
> >From the docstring of a.resize:
>
>     Change size and shape of self inplace.  Array must own its own memory
> and
>     not be referenced by other arrays.    Returns None.


The documentation is bogus:

>>> a = arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> a.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False
>>> a.resize((3,2))
>>> a
array([[0, 1],
       [2, 3],
       [4, 5]])


The reshaped array is a view on the original data, hence it doesn't
> own it:
>
> In [15]: a = N.arange(6).reshape(2,3)
>
> In [16]: a.flags
> Out[16]:
>   C_CONTIGUOUS : True
>   F_CONTIGUOUS : False
>   OWNDATA : False
>   WRITEABLE : True
>   ALIGNED : True
>   UPDATEIFCOPY : False
>
> > >>> a = arange(6).resize((2,3))
> > >>> a
> >
> > `a` has no value and no error is raised.
>
> It is because `a` is now None.


This behaviour doesn't match documentation elsewhere, which is why I am
raising the question. What *should* the resize method do? It looks like it
is equivalent to assigning a shape tuple to a.shape, so why do we need it?
Apart from that, the reshape method looks like it would serve for most
cases.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070829/7a9adf1e/attachment.html>


More information about the NumPy-Discussion mailing list