File: how-to-examine-list-fields.md

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (132 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
jupytext:
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.16.1
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

How to list an array's fields/columns/keys
==========================================

```{code-cell} ipython3
:tags: [hide-cell]

%config InteractiveShell.ast_node_interactivity = "last_expr_or_assign"
```

## Arrays of records

+++

As seen in {doc}`how-to-create-records`, one of Awkward Array's most useful features is the ability to compose separate arrays into a single record structure:

```{code-cell} ipython3
import awkward as ak
import numpy as np

records = ak.Array(
    [
        {"x": 0.014309631995020777, "y": 0.7077380205549498},
        {"x": 0.44925764718311145, "y": 0.11927022136408238},
        {"x": 0.9870653236436898, "y": 0.1543661194285082},
        {"x": 0.7071893130949595, "y": 0.3966721033002645},
        {"x": 0.3059032831996634, "y": 0.5094743992919755},
    ]
)
```

The type of an array gives an indication of the fields that it contains. We can see that the `records` array contains two fields `"x"` and `"y"`:

```{code-cell} ipython3
print(records.type)
```

```{code-cell} ipython3
records.type.show()
```

The {class}`ak.Array` object itself provides a convenient {attr}`ak.Array.fields` property that returns the list of field names

```{code-cell} ipython3
records.fields
```

In addition to this, Awkward Array also provides a high-level {func}`ak.fields` function that returns the same result

```{code-cell} ipython3
ak.fields(records)
```

## Arrays of tuples

+++

In addition to records, Awkward Array also has the concept of _tuples_.

```{code-cell} ipython3
tuples = ak.Array(
    [
        (1, 2, 3),
        (1, 2, 3),
    ]
)
```

These look very similar to records, but the fields are un-named:

```{code-cell} ipython3
print(tuples.type)
```

Despite this, the {func}`ak.fields` function, and {attr}`ak.Array.fields` property both return non-empty lists of strings when used to query a tuple array:

```{code-cell} ipython3
ak.fields(tuples)
```

```{code-cell} ipython3
tuples.fields
```

The returned field names are string-quoted integers (`"0"`, `"1"`, ...) that refer to zero-indexed tuple _slots_, and can be used to project the array:

```{code-cell} ipython3
tuples["0"]
```

```{code-cell} ipython3
tuples["1"]
```

Whilst the fields of records can be accessed as attributes of the array:

```{code-cell} ipython3
records.x
```

The same is not true of tuples, because integers are not valid attribute names:

```{code-cell} ipython3
:tags: [raises-exception]

tuples.0
```

The close similarity between records and tuples naturally raises the question:
> How do I know whether an array contains records or tuples?

The {func}`ak.is_tuple` function can be used to differentiate between the two

```{code-cell} ipython3
ak.is_tuple(tuples)
```

```{code-cell} ipython3
ak.is_tuple(records)
```