Pythonic VS String Syntax

Import the LArray library:


In [ ]:
from larray import *

The LArray library offers two syntaxes to build axes and make selections and aggregations. The first one is more Pythonic (uses Python structures) For example, you can create an age_category axis as follows:


In [ ]:
age_category = Axis(["0-9", "10-17", "18-66", "67+"], "age_category")
age_category

The second one consists of using strings that are parsed. It is shorter to type. The same age_category axis could have been generated as follows:


In [ ]:
age_category = Axis("age_category=0-9,10-17,18-66,67+")
age_category
**Warning:** The drawback of the string syntax is that some characters such as `, ; = : .. [ ] >>` have a special meaning and cannot be used with the ``String`` syntax. If you need to work with labels containing such special characters (when importing data from an external source for example), you have to use the ``Pythonic`` syntax which allows to use any character in labels.

String Syntax

Axes And Arrays creation

The string syntax allows to easily create axes.

When creating one axis, the labels are separated using ,:


In [ ]:
a = Axis('a=a0,a1,a2,a3')
a

The special syntax start..stop generates a sequence of labels:


In [ ]:
a = Axis('a=a0..a3')
a

When creating an array, it is possible to define several axes in the same string using ;


In [ ]:
arr = zeros("a=a0..a2; b=b0,b1; c=c0..c5")
arr

Selection

Starting from the array:


In [ ]:
immigration = load_example_data('demography_eurostat').immigration
immigration.info

an example of a selection using the Pythonic syntax is:


In [ ]:
# since the labels 'Belgium' and 'Netherlands' also exists in the 'citizenship' axis, 
# we need to explicitly specify that we want to make a selection over the 'country' axis
immigration_subset = immigration[X.country['Belgium', 'Netherlands'], 'Female', 2015:]
immigration_subset

Using the String syntax, the same selection becomes:


In [ ]:
immigration_subset = immigration['country[Belgium,Netherlands]', 'Female', 2015:]
immigration_subset

Aggregation

An example of an aggregation using the Pythonic syntax is:


In [ ]:
immigration.mean((X.time[2014::2] >> 'even_years', X.time[::2] >> 'odd_years'), 'citizenship')

Using the String syntax, the same aggregation becomes:


In [ ]:
immigration.mean('time[2014::2] >> even_years; time[::2] >> odd_years', 'citizenship')

where we used ; to separate groups of labels from the same axis.