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
In [ ]:
population.with_total('gender', label='Total')
See with_total for more details and examples.
In [ ]:
# where(condition, value if true, value if false)
where(population < population.mean('time'), -population, population)
See where for more details and examples.
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.
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.
In [ ]:
population.ratio('gender')
# which is equivalent to
population / population.sum('gender')
In [ ]:
# or, if you want the previous ratios in percents
population.percent('gender')
See percent for more details and examples.
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.
In [ ]:
population.growth_rate('time')
See growth_rate for more details and examples.
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.
There are a lot more interesting functions that you can find in the API reference in sections Aggregation Functions, Miscellaneous and Utility Functions.