|
| 1 | +cells: |
| 2 | + |
| 3 | +- markdown: | |
| 4 | + # Pythonic VS String Syntax |
| 5 | + |
| 6 | + |
| 7 | +- markdown: | |
| 8 | + Import the LArray library: |
| 9 | + |
| 10 | + |
| 11 | +- code: | |
| 12 | + from larray import * |
| 13 | + |
| 14 | + |
| 15 | +- markdown: | |
| 16 | + Check the version of LArray: |
| 17 | + |
| 18 | + |
| 19 | +- code: | |
| 20 | + from larray import __version__ |
| 21 | + __version__ |
| 22 | + |
| 23 | + |
| 24 | +- markdown: | |
| 25 | + LArray offers two syntaxes to build axes and make selections and aggregations. |
| 26 | + The first one is more ``Pythonic`` (uses Python structures) |
| 27 | + For example, you can create an *age_category* axis as follows: |
| 28 | + |
| 29 | + |
| 30 | +- code: | |
| 31 | + age_category = Axis(["0-9","10-17","18-66","67+"], "age_category") |
| 32 | + age_category |
| 33 | + |
| 34 | + |
| 35 | +- markdown: | |
| 36 | + The second one consists of using ``strings`` that are parsed. |
| 37 | + It is shorter to type. The same *age_category* axis could have been generated as follows: |
| 38 | + |
| 39 | + |
| 40 | +- code: | |
| 41 | + age_category = Axis("age_category=0-9,10-17,18-66,67+") |
| 42 | + age_category |
| 43 | + |
| 44 | + |
| 45 | +- markdown: | |
| 46 | + <div class="alert alert-warning"> |
| 47 | + **Warning:** The drawback of the string syntax is that some characters such as `, ; = : .. [ ] >>` |
| 48 | + have a special meaning and cannot be used with the ``String`` syntax. |
| 49 | + If you need to work with labels containing such special characters (when import data from an external source for example), you have to use the ``Pythonic`` syntax which allows to use any character in labels. |
| 50 | + </div> |
| 51 | + |
| 52 | + |
| 53 | +- markdown: | |
| 54 | + ## String Syntax |
| 55 | + |
| 56 | + |
| 57 | +- markdown: | |
| 58 | + ### Axes And Arrays creation |
| 59 | + |
| 60 | + The string syntax allows to easily create axes. |
| 61 | + |
| 62 | + When creating one axis, the labels are separated using ``,``: |
| 63 | + |
| 64 | + |
| 65 | +- code: | |
| 66 | + a = Axis('a=a0,a1,a2,a3') |
| 67 | + a |
| 68 | + |
| 69 | + |
| 70 | +- markdown: | |
| 71 | + The special syntax ``start..stop`` generates a sequence of labels: |
| 72 | + |
| 73 | + |
| 74 | +- code: | |
| 75 | + a = Axis('a=a0..a3') |
| 76 | + a |
| 77 | + |
| 78 | + |
| 79 | +- markdown: | |
| 80 | + When creating an array, it is possible to define several axes in the same string using ``;`` |
| 81 | + |
| 82 | + |
| 83 | +- code: | |
| 84 | + arr = zeros("a=a0..a2; b=b0,b1; c=c0..c5") |
| 85 | + arr |
| 86 | + |
| 87 | + |
| 88 | +- markdown: | |
| 89 | + ### Selection |
| 90 | + |
| 91 | + Starting from the array: |
| 92 | + |
| 93 | + |
| 94 | +- code: | |
| 95 | + marriages = load_example_data('demography_eurostat').marriages |
| 96 | + marriages.info |
| 97 | + |
| 98 | + |
| 99 | +- markdown: | |
| 100 | + an example of a selection using the ``Pythonic`` syntax is: |
| 101 | + |
| 102 | + |
| 103 | +- code: | |
| 104 | + marriages_subset = marriages[['Sweden', 'Finland'], X.partner['Reporting_country':], X.citizen['Total']] |
| 105 | + marriages_subset |
| 106 | + |
| 107 | + |
| 108 | +- markdown: | |
| 109 | + Using the ``String`` syntax, the same selection becomes: |
| 110 | + |
| 111 | + |
| 112 | +- code: | |
| 113 | + marriages_subset = marriages['Sweden,Finland', 'partner[Reporting_country:]', 'citizen[Total]'] |
| 114 | + marriages_subset |
| 115 | + |
| 116 | + |
| 117 | +- markdown: | |
| 118 | + ### Aggregation |
| 119 | + |
| 120 | + |
| 121 | +- markdown: | |
| 122 | + An example of an aggregation using the ``Pythonic`` syntax is: |
| 123 | + |
| 124 | + |
| 125 | +- code: | |
| 126 | + marriages.sum((X.citizen['Total'], X.citizen['Reporting_country':] >> 'Total_Check'), 'country') |
| 127 | + |
| 128 | + |
| 129 | +- markdown: | |
| 130 | + Using the ``String`` syntax, the same aggregation becomes: |
| 131 | + |
| 132 | + |
| 133 | +- code: | |
| 134 | + marriages.sum('citizen[Total]; citizen[Reporting_country:] >> Total_Check', 'country') |
| 135 | + |
| 136 | + |
| 137 | +- markdown: | |
| 138 | + where we used ``;`` to separate groups of labels from the same axis. |
| 139 | + |
| 140 | + |
| 141 | +# The lines below here may be deleted if you do not need them. |
| 142 | +# --------------------------------------------------------------------------- |
| 143 | +metadata: |
| 144 | + kernelspec: |
| 145 | + display_name: Python 3 |
| 146 | + language: python |
| 147 | + name: python3 |
| 148 | + language_info: |
| 149 | + codemirror_mode: |
| 150 | + name: ipython |
| 151 | + version: 3 |
| 152 | + file_extension: .py |
| 153 | + mimetype: text/x-python |
| 154 | + name: python |
| 155 | + nbconvert_exporter: python |
| 156 | + pygments_lexer: ipython3 |
| 157 | + version: 3.7.3 |
| 158 | +nbformat: 4 |
| 159 | +nbformat_minor: 2 |
| 160 | + |
0 commit comments