larray.sequence

larray.sequence(axis, initial=0, inc=None, mult=None, func=None, axes=None, title=None, meta=None) Array[source]

Create an array by sequentially applying modifications to the array along axis.

The value for each label in axis will be given by sequentially transforming the value for the previous label. This transformation on the previous label value consists of applying the function “func” on that value if provided, or to multiply it by mult and increment it by inc otherwise.

Parameters
axisaxis definition (Axis, str, int)

Axis along which to apply mod. An axis definition can be passed as a string. An int will be interpreted as the length for a new anonymous axis.

initialscalar or Array, optional

Value for the first label of axis. Defaults to 0.

incscalar, Array, optional

Value to increment the previous value by. Defaults to 1 unless mult is provided (in which case it defaults to 0).

multscalar, Array, optional

Value to multiply the previous value by. Defaults to None.

funcfunction/callable, optional

Function to apply to the previous value. Defaults to None. Note that this is much slower than using inc and/or mult.

axesint, tuple of int or tuple/list/AxisCollection of Axis, optional

Axes of the result. Defaults to the union of axes present in other arguments.

titlestr, optional

Deprecated. See ‘meta’ below.

metalist of pairs or dict or Metadata, optional

Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.

Examples

>>> year = Axis('year=2016..2019')
>>> sex = Axis('sex=M,F')
>>> sequence(year)
year  2016  2017  2018  2019
         0     1     2     3
>>> sequence('year=2016..2019')
year  2016  2017  2018  2019
         0     1     2     3
>>> sequence(year, 1.0, 0.5)
year  2016  2017  2018  2019
       1.0   1.5   2.0   2.5
>>> sequence(year, 1.0, mult=1.5)
year  2016  2017  2018   2019
       1.0   1.5  2.25  3.375
>>> inc = Array([1, 2], [sex])
>>> inc
sex  M  F
     1  2
>>> sequence(year, 1.0, inc)
sex\year  2016  2017  2018  2019
       M   1.0   2.0   3.0   4.0
       F   1.0   3.0   5.0   7.0
>>> mult = Array([2, 3], [sex])
>>> mult
sex  M  F
     2  3
>>> sequence(year, 1.0, mult=mult)
sex\year  2016  2017  2018  2019
       M   1.0   2.0   4.0   8.0
       F   1.0   3.0   9.0  27.0
>>> initial = Array([3, 4], [sex])
>>> initial
sex  M  F
     3  4
>>> sequence(year, initial, 1)
sex\year  2016  2017  2018  2019
       M     3     4     5     6
       F     4     5     6     7
>>> sequence(year, initial, mult=2)
sex\year  2016  2017  2018  2019
       M     3     6    12    24
       F     4     8    16    32
>>> sequence(year, initial, inc, mult)
sex\year  2016  2017  2018  2019
       M     3     7    15    31
       F     4    14    44   134
>>> def modify(prev_value):
...     return prev_value / 2
>>> sequence(year, 8, func=modify)
year  2016  2017  2018  2019
         8     4     2     1
>>> sequence(3)
{0}*  0  1  2
      0  1  2
>>> sequence('year', axes=(sex, year))
sex\year  2016  2017  2018  2019
       M     0     1     2     3
       F     0     1     2     3

sequence can be used as the inverse of growth_rate:

>>> a = Array([1.0, 2.0, 3.0, 3.0], year)
>>> a
year  2016  2017  2018  2019
       1.0   2.0   3.0   3.0
>>> g = a.growth_rate() + 1
>>> g
year  2017  2018  2019
       2.0   1.5   1.0
>>> sequence(year, a[2016], mult=g)
year  2016  2017  2018  2019
       1.0   2.0   3.0   3.0