Some Useful Functions

Import the LArray library:


In [ ]:
from larray import *

In [ ]:
# load 'demography_eurostat' dataset
demography_eurostat = load_example_data('demography_eurostat')

# extract the 'population' array from the dataset 
population = demography_eurostat.population
population

with total

Add totals to one or several axes:


In [ ]:
population.with_total('gender', label='Total')

See with_total for more details and examples.

where

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


In [ ]:
# where(condition, value if true, value if false)
where(population < population.mean('time'), -population, population)

See where for more details and examples.

clip

Set all data between a certain range:


In [ ]:
# values below 10 millions are set to 10 millions
population.clip(minval=10**7)

In [ ]:
# values above 40 millions are set to 40 millions
population.clip(maxval=4*10**7)

In [ ]:
# values below 10 millions are set to 10 millions and 
# values above 40 millions are set to 40 millions
population.clip(10**7, 4*10**7)

In [ ]:
# Using vectors to define the lower and upper bounds
lower_bound = sequence(population.time, initial=5_500_000, inc=50_000)
upper_bound = sequence(population.time, 41_000_000, inc=100_000)

print(lower_bound, '\n')
print(upper_bound, '\n')

population.clip(lower_bound, upper_bound)

See clip for more details and examples.

divnot0

Replace division by 0 by 0:


In [ ]:
divisor = ones(population.axes, dtype=int)
divisor['Male'] = 0
divisor

In [ ]:
population / divisor

In [ ]:
# we use astype(int) since the divnot0 method 
# returns a float array in this case while 
# we want an integer array
population.divnot0(divisor).astype(int)

See divnot0 for more details and examples.

ratio

The ratio (rationot0) method returns an array with all values divided by the sum of values along given axes:


In [ ]:
population.ratio('gender')

# which is equivalent to
population / population.sum('gender')

See ratio and rationot0 for more details and examples.

percents


In [ ]:
# or, if you want the previous ratios in percents
population.percent('gender')

See percent for more details and examples.

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.


In [ ]:
# calculates 'diff[year+1] = population[year+1] - population[year]'
population.diff('time')

In [ ]:
# calculates 'diff[year+2] = population[year+2] - population[year]'
population.diff('time', d=2)

In [ ]:
# calculates 'diff[year] = population[year+1] - population[year]'
population.diff('time', label='lower')

See diff for more details and examples.

growth_rate

The growth_rate method calculates the growth along a given axis.

It is roughly equivalent to a.diff(axis, d, label) / a[axis.i[:-d]]:


In [ ]:
population.growth_rate('time')

See growth_rate for more details and examples.

shift

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


In [ ]:
population.shift('time')

In [ ]:
# when shift is applied on an (increasing) time axis,
# it effectively brings "past" data into the future
population_shifted = population.shift('time')
stack({'population_shifted_2014': population_shifted[2014], 'population_2013': population[2013]}, 'array')

See shift for more details and examples.

Other interesting functions

There are a lot more interesting functions that you can find in the API reference in sections Aggregation Functions, Miscellaneous and Utility Functions.