Getting Started¶
To use the LArray library, the first thing to do is to import it
In [1]: from larray import *
Create an array¶
Working with the LArray library mainly consists of manipulating LArray data structures. They represent N-dimensional labelled arrays and are composed of data (numpy ndarray), axes and optionally a title. An axis contains a list of labels and may have a name (if not given, the axis is anonymous).
You can create an array from scratch by supplying data, axes and optionally a title:
# define data
In [2]: data = [[20, 22],
...: [33, 31],
...: [79, 81],
...: [28, 34]]
...:
# define axes
In [3]: age_category = Axis(["0-9", "10-17", "18-66", "67+"], "age_category")
In [4]: sex = Axis(["M", "F"], "sex")
# create LArray object
In [5]: arr = LArray(data, [age_category, sex], "population by age category and sex")
In [6]: arr
Out[6]:
age_category\sex M F
0-9 20 22
10-17 33 31
18-66 79 81
67+ 28 34
Note
LArray offers two syntaxes to build axes and make selections and aggregations. The first one is more Pythonic (uses Python structures) and allows to use any character in labels. The second one consists of using strings that are parsed. It is shorter to type. For example, you could create the age_category axis above as follows:
age_category = Axis("age_category=0-9,10-17,18-66,67+")
The drawback of the string syntax is that some characters such as , ; = : .. [ ] >> have a special meaning and cannot be used in labels. Strings containing only integers are interpreted as such.
In this getting started section we will use the first, more verbose, syntax which works in all cases and provide the equivalent using the shorter syntax in comments. More examples can be found in the tutorial and the API reference section.
Here are the key properties for an array:
# array summary : dimensions + description of axes
In [7]: arr.info
Out[7]:
population by age category and sex
4 x 2
age_category [4]: '0-9' '10-17' '18-66' '67+'
sex [2]: 'M' 'F'
dtype: int64
# number of dimensions
In [8]: arr.ndim