# [SciPy-user] How to "move" data within an array

lorenzo bolla lbolla@gmail....
Wed Jul 4 06:23:35 CDT 2007

```it's better to specify the axis, though:

In [20]: numpy.roll(x,1,axis = 1)
Out[20]:
array([[3, 1, 2],
[6, 4, 5]])

In [21]: numpy.roll(x,1)
Out[21]:
array([[6, 1, 2],
[3, 4, 5]])

lorenzo.

On 7/4/07, lorenzo bolla <lbolla@gmail.com> wrote:
>
> gotcha. you could use numpy.roll, then.
>
>
> In [10]: x = numpy.array([[1,2,3],[4,5,6]])
>
> In [11]: x
> Out[11]:
> array([[1, 2, 3],
>        [4, 5, 6]])
>
> In [12]: numpy.roll(x,1)
> Out[12]:
> array([[6, 1, 2],
>        [3, 4, 5]])
>
> In [13]: numpy.roll(x,2)
> Out[13]:
> array([[5, 6, 1],
>        [2, 3, 4]])
>
> In [14]: numpy.roll(x,3)
> Out[14]:
> array([[4, 5, 6],
>        [1, 2, 3]])
>
>
> lorenzo.
>
>
> On 7/4/07, Andrew Smart <subscriptions@smart-knowhow.de> wrote:
> >
> > Hi Lorenzo,
> >
> > thanks for your 2cents.
> >
> > The pointer method isn't practical for my purposes: I want to have the
> > ability to access the "historical" data within the engine on various
> > ways,
> > e.g. "price average of the last 3 periods", where the array itself
> > stores
> > still 5 periods. The pointer method would require to re-calculate the
> > time
> > axis and especially to manage the "wrap", like: current time row is 3,
> > so "3
> > periods back" would be rows 2, 1 and 5.
> >
> > I would like to use the numpy functions as sum(), avg() etc. on the
> > arrays,
> > so having single 1d arrays (one row = one array) does not really make
> > sense.
> >
> > But thanks for the idea,
> > Andrew
> > ________________________________
> >
> >        Von: scipy-user-bounces@scipy.org
> > [mailto:scipy-user-bounces@scipy.org] Im Auftrag von lorenzo bolla
> >        Gesendet: Mittwoch, 4. Juli 2007 11:33
> >        An: SciPy Users List
> >        Betreff: Re: [SciPy-user] How to "move" data within an array
> >
> >
> >        why not using a list of 1D arrays?
> >        but why do you want to physically move your rows? you can simply
> > use
> > an integer as a pointer to the row of the "current time": then you
> > update
> > this integer every timestep (+1), taking its "modulo 5" to cycle through
> > the
> > rows.
> >
> >        my two cents.
> >        lorenzo.
> >
> >        On 7/4/07, Andrew Smart < subscriptions@smart-knowhow.de> wrote:
> >
> >                Hi folks,
> >
> >                I'm using numpy arrays for storing data which is
> > generated
> > within an engine.
> >                I'm using the topmost dimension as time axis: every row
> > represents a full
> >                set of data created by the engine while one round.
> >
> >                Say: i have an array for storing prices (e.g. 10
> > different
> > prices are
> >                generated within one engine round). I'm storing/using the
> >
> > last 5 rounds, so
> >                I get an array with the dimensions (5,10).
> >
> >                If the engine runs longer than 5 rounds I have to
> > "remove"
> > the oldest record
> >                and move the younger records one position back.
> >
> >                Since I've a lot of such arrays I would like to use the
> > most
> > efficient
> >                method avaiable in numpy. On a pure memory-orientated
> > view
> > this would be
> >                just to copy ("move") the memory blocks from the younger
> > 4
> > rows one row
> >                further, thus having the first row for the new data.
> >
> >                In the C API I see some functions like copyswap() and
> > memmove() which
> >                indicate that such operations are possible at the C API
> > level. But I'm not
> >                sure the correct approach on the Python level.
> >
> >                Taking slices may be one options - but the new slice will
> > then occupy new
> >                memory, causing memory fragmentation...
> >                Looping over all data items, all rows is time consuming
> > and
> > surely wasting
> >                resources...
> >
> >                Any pointers/ideas?
> >
> >                Thanks,
> >                Andrew
> >
> >
> >                _______________________________________________
> >                SciPy-user mailing list
> >                SciPy-user@scipy.org
> >                http://projects.scipy.org/mailman/listinfo/scipy-user
> >
> >
> >
> >
> > _______________________________________________
> > SciPy-user mailing list
> > SciPy-user@scipy.org
> > http://projects.scipy.org/mailman/listinfo/scipy-user
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070704/376b14f4/attachment-0001.html
```