Interactive online version: Binder badge

Miscellaneous (other interesting array functions)

Import the LArray library:

[2]:
from larray import *

Check the version of LArray:

[3]:
from larray import __version__
__version__
[3]:
'0.31'

Import a subset of the test array pop:

[4]:
# import a 6 x 2 x 2 subset of the 'pop' example array
pop = load_example_data('demography').pop[2016, 'BruCap', 100:105]
pop
[4]:
age  sex\nat  BE  FO
100        M  12   0
100        F  60   3
101        M  12   2
101        F  66   5
102        M   8   0
102        F  26   1
103        M   2   1
103        F  17   2
104        M   2   1
104        F  14   0
105        M   0   0
105        F   2   2

with total

Add totals to one axis

[5]:
pop.with_total('sex', label='B')
[5]:
age  sex\nat  BE  FO
100        M  12   0
100        F  60   3
100        B  72   3
101        M  12   2
101        F  66   5
101        B  78   7
102        M   8   0
102        F  26   1
102        B  34   1
103        M   2   1
103        F  17   2
103        B  19   3
104        M   2   1
104        F  14   0
104        B  16   1
105        M   0   0
105        F   2   2
105        B   2   2

Add totals to all axes at once

[6]:
# by default label is 'total'
pop.with_total()
[6]:
  age  sex\nat   BE  FO  total
  100        M   12   0     12
  100        F   60   3     63
  100    total   72   3     75
  101        M   12   2     14
  101        F   66   5     71
  101    total   78   7     85
  102        M    8   0      8
  102        F   26   1     27
  102    total   34   1     35
  103        M    2   1      3
  103        F   17   2     19
  103    total   19   3     22
  104        M    2   1      3
  104        F   14   0     14
  104    total   16   1     17
  105        M    0   0      0
  105        F    2   2      4
  105    total    2   2      4
total        M   36   4     40
total        F  185  13    198
total    total  221  17    238

where

where can be used to apply some computation depending on a condition

[7]:
# where(condition, value if true, value if false)
where(pop < 10, 0, -pop)
[7]:
age  sex\nat   BE  FO
100        M  -12   0
100        F  -60   0
101        M  -12   0
101        F  -66   0
102        M    0   0
102        F  -26   0
103        M    0   0
103        F  -17   0
104        M    0   0
104        F  -14   0
105        M    0   0
105        F    0   0

clip

Set all data between a certain range

[8]:
# clip(min, max)
# values below 10 are set to 10 and values above 50 are set to 50
pop.clip(10, 50)
[8]:
age  sex\nat  BE  FO
100        M  12  10
100        F  50  10
101        M  12  10
101        F  50  10
102        M  10  10
102        F  26  10
103        M  10  10
103        F  17  10
104        M  10  10
104        F  14  10
105        M  10  10
105        F  10  10

divnot0

Replace division by 0 to 0

[9]:
pop['BE'] / pop['FO']
/home/docs/checkouts/readthedocs.org/user_builds/larray/conda/0.31/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered during operation
  """Entry point for launching an IPython kernel.
/home/docs/checkouts/readthedocs.org/user_builds/larray/conda/0.31/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value (NaN) encountered during operation (this is typically caused by a 0 / 0)
  """Entry point for launching an IPython kernel.
[9]:
age\sex    M     F
    100  inf  20.0
    101  6.0  13.2
    102  inf  26.0
    103  2.0   8.5
    104  2.0   inf
    105  nan   1.0
[10]:
# divnot0 replaces results of division by 0 by 0.
# Using it should be done with care though
# because it can hide a real error in your data.
pop['BE'].divnot0(pop['FO'])
[10]:
age\sex    M     F
    100  0.0  20.0
    101  6.0  13.2
    102  0.0  26.0
    103  2.0   8.5
    104  2.0   0.0
    105  0.0   1.0

diff

The diff method calculates the n-th order discrete difference along a given axis. The first order difference is given by out[n+1] = in[n + 1] - in[n] along the given axis.

[11]:
pop = load_example_data('demography').pop[2005:2015, 'BruCap', 50]
pop
[11]:
time  sex\nat    BE    FO
2005        M  4289  1591
2005        F  4661  1584
2006        M  4335  1761
2006        F  4781  1580
2007        M  4291  1806
2007        F  4719  1650
2008        M  4349  1773
2008        F  4731  1680
2009        M  4429  2003
2009        F  4824  1722
2010        M  4582  2085
2010        F  4869  1928
2011        M  4677  2294
2011        F  5015  2104
2012        M  4463  2450
2012        F  4722  2186
2013        M  4610  2604
2013        F  4711  2254
2014        M  4725  2709
2014        F  4788  2349
2015        M  4841  2891
2015        F  4813  2498
[12]:
# calculates 'pop[year+1] - pop[year]'
pop.diff('time')
[12]:
time  sex\nat    BE   FO
2006        M    46  170
2006        F   120   -4
2007        M   -44   45
2007        F   -62   70
2008        M    58  -33
2008        F    12   30
2009        M    80  230
2009        F    93   42
2010        M   153   82
2010        F    45  206
2011        M    95  209
2011        F   146  176
2012        M  -214  156
2012        F  -293   82
2013        M   147  154
2013        F   -11   68
2014        M   115  105
2014        F    77   95
2015        M   116  182
2015        F    25  149
[13]:
# calculates 'pop[year+2] - pop[year]'
pop.diff('time', d=2)
[13]:
time  sex\nat    BE   FO
2007        M     2  215
2007        F    58   66
2008        M    14   12
2008        F   -50  100
2009        M   138  197
2009        F   105   72
2010        M   233  312
2010        F   138  248
2011        M   248  291
2011        F   191  382
2012        M  -119  365
2012        F  -147  258
2013        M   -67  310
2013        F  -304  150
2014        M   262  259
2014        F    66  163
2015        M   231  287
2015        F   102  244

ratio

[14]:
pop.ratio('nat')

# which is equivalent to
pop / pop.sum('nat')
[14]:
time  sex\nat                  BE                   FO
2005        M   0.729421768707483    0.270578231292517
2005        F  0.7463570856685349   0.2536429143314652
2006        M  0.7111220472440944   0.2888779527559055
2006        F  0.7516113818581984   0.2483886181418016
2007        M   0.703788748564868  0.29621125143513205
2007        F  0.7409326424870466  0.25906735751295334
2008        M  0.7103887618425351  0.28961123815746487
2008        F  0.7379503977538605  0.26204960224613943
2009        M  0.6885883084577115  0.31141169154228854
2009        F  0.7369385884509624  0.26306141154903756
2010        M  0.6872656367181641   0.3127343632818359
2010        F  0.7163454465205238   0.2836545534794762
2011        M  0.6709223927700474  0.32907760722995266
2011        F  0.7044528725944655  0.29554712740553446
2012        M  0.6455952553160712  0.35440474468392885
2012        F  0.6835552982049797  0.31644470179502027
2013        M  0.6390352093152204   0.3609647906847796
2013        F  0.6763819095477387   0.3236180904522613
2014        M   0.635593220338983   0.3644067796610169
2014        F  0.6708701134930644   0.3291298865069357
2015        M  0.6260993274702535   0.3739006725297465
2015        F  0.6583230748187663  0.34167692518123377

percents

[15]:
# or, if you want the previous ratios in percents
pop.percent('nat')
[15]:
time  sex\nat                  BE                  FO
2005        M    72.9421768707483    27.0578231292517
2005        F   74.63570856685348  25.364291433146516
2006        M   71.11220472440945  28.887795275590552
2006        F   75.16113818581984   24.83886181418016
2007        M    70.3788748564868  29.621125143513204
2007        F   74.09326424870466  25.906735751295336
2008        M   71.03887618425351   28.96112381574649
2008        F   73.79503977538606  26.204960224613945
2009        M   68.85883084577114  31.141169154228855
2009        F   73.69385884509624   26.30614115490376
2010        M   68.72656367181641  31.273436328183593
2010        F   71.63454465205237  28.365455347947623
2011        M   67.09223927700474   32.90776072299526
2011        F   70.44528725944654  29.554712740553448
2012        M   64.55952553160712  35.440474468392885
2012        F   68.35552982049798  31.644470179502026
2013        M   63.90352093152204   36.09647906847796
2013        F   67.63819095477388   32.36180904522613
2014        M  63.559322033898304  36.440677966101696
2014        F   67.08701134930644   32.91298865069357
2015        M   62.60993274702535   37.39006725297465
2015        F   65.83230748187663  34.167692518123374

growth_rate

using the same principle than diff

[16]:
pop.growth_rate('time')
[16]:
time  sex\nat                     BE                      FO
2006        M   0.010725110748426206     0.10685103708359522
2006        F   0.025745548165629694  -0.0025252525252525255
2007        M  -0.010149942329873126     0.02555366269165247
2007        F  -0.012967998326709893     0.04430379746835443
2008        M   0.013516662782568165   -0.018272425249169437
2008        F  0.0025429116338207248     0.01818181818181818
2009        M    0.01839503334099793     0.12972363226170333
2009        F   0.019657577679137603                   0.025
2010        M    0.03454504402799729    0.040938592111832255
2010        F   0.009328358208955223     0.11962833914053426
2011        M    0.02073330423395897     0.10023980815347722
2011        F   0.029985623331279524      0.0912863070539419
2012        M   -0.04575582638443447     0.06800348735832606
2012        F    -0.0584247258225324     0.03897338403041825
2013        M    0.03293748599596684     0.06285714285714286
2013        F  -0.002329521389241847     0.03110704483074108
2014        M   0.024945770065075923     0.04032258064516129
2014        F    0.01634472511144131     0.04214729370008873
2015        M    0.02455026455026455     0.06718346253229975
2015        F  0.0052213868003341685     0.06343124733929331

shift

The shift method drops first label of an axis and shifts all subsequent labels

[17]:
pop.shift('time')
[17]:
time  sex\nat    BE    FO
2006        M  4289  1591
2006        F  4661  1584
2007        M  4335  1761
2007        F  4781  1580
2008        M  4291  1806
2008        F  4719  1650
2009        M  4349  1773
2009        F  4731  1680
2010        M  4429  2003
2010        F  4824  1722
2011        M  4582  2085
2011        F  4869  1928
2012        M  4677  2294
2012        F  5015  2104
2013        M  4463  2450
2013        F  4722  2186
2014        M  4610  2604
2014        F  4711  2254
2015        M  4725  2709
2015        F  4788  2349
[18]:
# when shift is applied on an (increasing) time axis,
# it effectively brings "past" data into the future
pop.shift('time').ignore_labels('time') == pop[2005:2014].ignore_labels('time')
[18]:
time*  sex\nat    BE    FO
    0        M  True  True
    0        F  True  True
    1        M  True  True
    1        F  True  True
    2        M  True  True
    2        F  True  True
    3        M  True  True
    3        F  True  True
    4        M  True  True
    4        F  True  True
    5        M  True  True
    5        F  True  True
    6        M  True  True
    6        F  True  True
    7        M  True  True
    7        F  True  True
    8        M  True  True
    8        F  True  True
    9        M  True  True
    9        F  True  True
[19]:
# this is mostly useful when you want to do operations between the past and now
# as an example, here is an alternative implementation of the .diff method seen above:
pop.i[1:] - pop.shift('time')
[19]:
time  sex\nat    BE   FO
2006        M    46  170
2006        F   120   -4
2007        M   -44   45
2007        F   -62   70
2008        M    58  -33
2008        F    12   30
2009        M    80  230
2009        F    93   42
2010        M   153   82
2010        F    45  206
2011        M    95  209
2011        F   146  176
2012        M  -214  156
2012        F  -293   82
2013        M   147  154
2013        F   -11   68
2014        M   115  105
2014        F    77   95
2015        M   116  182
2015        F    25  149

Misc other interesting functions

There are a lot more interesting functions available:

  • round, floor, ceil, trunc,

  • exp, log, log10,

  • sqrt, absolute, nan_to_num, isnan, isinf, inverse,

  • sin, cos, tan, arcsin, arccos, arctan

  • and many many more…