[Numpy-discussion] copying array to itself

Stéfan van der Walt stefan at sun.ac.za
Wed May 2 15:58:41 EDT 2012


On Wed, May 2, 2012 at 9:03 AM, Henry Gomersall <heng at cantab.net> wrote:
> Is this some nuance of the way numpy does things? Or am I missing some
> stupid bug in my code?

Try playing with the parameters of the following code:

sz = 10000
N = 10

import numpy as np

x = np.arange(sz)
y = x.copy()
x[:-N] = x[N:]

np.testing.assert_equal(x[:-N], y[N:])


For small values of sz this typically works, but as soon as numpy
needs to buffer strange things happen because you are reading from
memory locations that you've already written to.

Stéfan



More information about the NumPy-Discussion mailing list