T←"price" "qty":[[1 2 3] [4 5 6]]
T
≢T["price":[1 2 3] "qty":[4 5 6]]
2ₓ
An array can have string keys on any of its axes. A key names one position on its axis. The position still has its number, counting from 0. Functions keep their ordinary meanings, and their results keep the keys.
: puts the keys on its left onto the leading axis of the array on its right. Several keys name the major cells. This table is a vector of two columns, each keyed by its name:
With a single key, : makes the whole right argument the one item of a keyed vector:
On a matrix, the keys name the rows:
You can write a keyed vector the way it is displayed, as a bracket list of key:value items. In ["aa":1 2], the 2 has no key. The empty record is ⍬:⍬.
A record inside a vector needs its own brackets, as in [15 ["aa":1]].
A character or a string is one key. 'a' 'b' is the single key "ab", because characters side by side form one string. Write "a" "b" for two keys. A numeric subscript chooses an axis. ι₀ lists the keys of axis 0:
K:⍠axis A puts keys on any axis of A. Here they name the columns:
Selecting : on the first axis and "Feb" on the second gives the Feb column:
Several key vectors label successive axes. Write them inside one pair of brackets, with ; between them, because each one contains spaces:
ι₀ ₁ lists the keys of both axes:
: takes several axes in the order given. This builds the same M by giving the column keys before the row keys:
: removes every key and axis name:
:₁ removes only axis 1’s keys and name:
For an axis without keys, ι lists its positions:
A dfn can build a record with : like any other code:
Keys on one axis must be unique strings, one for each position:
| Expression | Error |
|---|---|
"aa" "aa":1 2 |
DOMAIN |
"aa" "bb":₀[1 2 3] |
LENGTH |
Index ⌷ takes a key or a position for each leading axis. Selecting a single key or position removes its axis. The key "bob" and the position 1 select the same row:
Selecting on both axes gives one item. : selects a whole axis. Keys and positions can be mixed:
Selecting with a vector keeps the axis. A string counts as one key. Selecting the same item more than once, by key or by position, drops the selected axis’s keys, as Index describes. To select one row and keep the axis, use a one-item vector of keys:
Pick ⊃ selects by key too. One key picks a row as a record:
Dot indexing puts the selection after the array, with one bracket item for each axis. M.["alice" "qty"] is "alice" "qty"⌷M. A path of names after a dot, as in rec.addr.city, picks by each name in turn through nested records:
After a function, .name reads a field of the function’s result. Paths work as they do after an array. ⊢.addr.city means {ω.addr.city}:
This lets you read a field inside an operator without writing a dfn. Any function can come before the .:
10 5
10.5
When the name after the . holds a function, the expression is an inner product instead, as +.× is. Indexing shows f.[I], which selects from a function’s result.
Modified assignment works through a dot path:
Plain assignment through a dot path creates any missing keys and records along the path:
On an axis without keys, the new position gets a key and the existing positions have none:
Plain assignment through dot indexing appends a missing row or column, as assignment through ⌷ does. New cells start as the array’s prototype until the assignment fills them. Here the first assignment adds a row for cara, and the second a tax column:
Assigning to a new row and a new column at once adds both. The assigned cell gets the value, and the other new cells hold the prototype:
Reading a missing key gives the fill. Modified assignment at a missing key adds the key, starting from the fill, as x←x f y would. Assignment to a position past the end changes nothing, because a position can’t extend an axis. For T←"aa" "bb":1 2:
| Expression | Result |
|---|---|
"cc"⌷T |
0, the fill |
T.["cc"]+←1 |
adds "cc" holding 1 |
T.[2]←9 |
T unchanged |
Assigning to a single key or position stores the whole right argument there. Assigning to a vector selection aligns the kept axis with the right argument’s keys. The same keyed u gives a different result each way:
When both arguments of a pervasive function have keys on an axis, they align by key. The result lists the left argument’s keys, then the keys only the right argument has. A missing entry is read as the prototype of the array it is missing from:
["aa":2 "bb":3]
["aa":0 "bb":0]
["aa":$t "bb":$t]
⍠ with an options record chooses the fill for one call. The fill takes the type of the argument that reads it:
Keys in a different order still line up:
Each keyed axis aligns on its own. Two one-cell matrices with no keys in common add up to a 2×2 matrix:
When one argument has no keys on an axis, agreement goes by position. A keyed singleton is stretched to match the other argument and loses its key. Enclosing the other argument keeps the key and puts the sum inside it:
15 25 35
["base":[15 25 35]]
["aa":11 "bb":22]
An axis operand chooses which axes correspond. Lined up with the columns of M, the keyed vector is added to the qty column, and the result has a new tax column:
Transpose moves each axis with its keys:
Reverse, rotate, take, drop and filtering move keys with their positions:
["cc":5 "bb":20 "aa":10]
["bb":20 "cc":5 "aa":10]
["cc":5]
["bb":20 "cc":5]
["aa":10 "cc":5]
Reduction removes the reduced axis and its keys:
Scan and Each keep their argument’s keys:
Reshape keeps the keys only when the shape is unchanged. Ravel drops the keys of the axes it merges. Table keeps the keys of the axis it leaves alone:
["aa":1 "bb":2]
1 2 1 2
[0 1 2 3]ₓ
"aa" "bb"
Rank keeps the keys of the frame. An axis inside the result cells keeps a key only when every cell has that key in the same place. Pairing each item of pair with itself keeps the row keys:
Reversing each row of M moves the column keys to the same positions in every row, so the result keeps them:
Reversing only alice’s row loses the column keys, because the two rows now have them in different orders:
A rotation with a different count for each row drops the rotated axis’s keys in the same way:
Outer product keeps both arguments’ axes and their keys:
An array of Power counts supplies the result’s frame, with its keys:
Inner product, Decode and matrix solve align the keys of the axes they contract. When both arguments have keys on the contracted axes, the key sets must be equal, or the call raises a LENGTH error. When either argument has none, the contraction goes by position:
Solving against S, which has keys on both axes, gives a vector keyed by S’s column keys:
Inverting S swaps its row and column keys:
Catenate joins the keys along its axis and aligns the other keyed axes. A position from an unkeyed part has no key:
B lists its rows in the opposite order from A:
Replicate can repeat the items of an unkeyed axis, and the other axes keep their keys:
Positions added by Take, expand or fill from an unkeyed part have no keys:
Catenate and Replicate cannot construct an axis with repeated keys. For V←"aa" "bb":1 2:
| Expression | Reason |
|---|---|
V,V |
Duplicate joined keys |
2#V |
Repeated keys |
Full windows add an unkeyed axis of window positions. The axis that a window slides along loses its keys in every window:
Even a single window of 3, which covers every key, loses those keys:
Each padded window is keyed by its centre:
Each item of a stencil’s result has the key of the position at the centre of its window. Inside the operand, a window has no keys along the axes it slides on:
Before comparing, Match aligns keys on every axis where both arguments have keys. An axis without keys matches a keyed axis by position. Axis names match by the same rule used to pair axes in arithmetic: two axes match when they have the same name or either has none. A keyed vector matches its reverse, because its keys move with its values, and it matches the same values without keys:
Searches compare values or cells and return the key of a position where a match is found. For a value that isn’t found, the search returns the length, as usual:
Grade down lists the keys from the largest value to the smallest:
Membership compares values and keeps the left argument’s keys:
A position in a matrix can contain both keys and numbers. Iota without a subscript still takes its argument’s values as a shape, whatever keys the argument has:
1ₓ "price"
[0 0] [0 1] [0 2]
[1 0] [1 1] [1 2]
Unique and the set functions compare values and keep the keys of the positions they select:
["bb":2]
["aa":1]
["aa":1 "bb":2 "yy":3]
["aa":1 "cc":2]
Monadic Key passes the keys of each group’s positions to its operand. Dyadic Key keeps the keys on the grouped values:
An axis name identifies a dimension, such as city, while its keys identify positions along it, such as LA and NY. Axis names are unique within an array.
An array’s shape is keyed by its axis names. Reshaping by a keyed shape names the axes:
An axis operand can name an axis instead of numbering it:
An entry of the shape without a key leaves its axis unnamed. N:ρA ρ A renames the axes. :ρA ρ A removes only the names, and ρA ρ :A only the position keys. A whole-number key equal to its position stands for no key, as the 1 in "city" 1 does. A reshape that keeps the shape also keeps the position keys and shares the array’s storage:
A keyed vector to the left of : uses each key as an axis name and that key’s value as the axis’s position keys:
Jan Feb Mar
NY 10 20 30
LA 40 50 60
The shape of sales is keyed by axis name, and its positions are identified by keys:
An axis description can be kept in a name and reused, or attached to chosen axes. The other axes keep their names and keys:
[2 "month":3]ₓ
["city":2 "month":3]ₓ
A record of several axes attaches to the chosen axes in the order given:
Agreement first pairs axes with equal names, wherever they sit. Unpaired axes broadcast. When every axis of the left argument pairs and the right argument has more axes, the result has the right argument’s axis order. Otherwise the left argument’s axes come first, then the axes only the right argument has:
monthly+C has the order of C, the larger argument, as C+monthly does. When the arguments have the same axes, the result has the left argument’s order, and C+Cᵀ pairs each axis with the one of the same name:
The remaining axes pair in leading-axis order. A named axis paired with an unnamed one keeps its name. Length agreement and key alignment then apply as usual:
Axes with different names stay separate, each broadcasting against the other:
Use a name or a list of names as the ⍠ operand to specify axes. Index with that operand selects positions on the named axes:
["month":3]ρ["Jan":0 "Feb":1 "Mar":2]ₓ
$t
["city":2]ₓ
15ₓ
Transpose moves names with their axes. Selecting with a single key or position removes the axis and its name. Replicate and Table keep the name. Reshape takes the axis names from its left argument. Ravel drops the names of the axes it merges. New axes start unnamed:
["month":3 "city":2]ₓ
["month":3]ₓ
[2 3]ₓ
[6]ₓ
["city":4]ₓ
["city":2 1]ₓ
When an operation gives two axes the same name, both lose it. Their position keys remain:
Applying ι to a keyed shape gives a named index: the positions of an axis, arranged along an axis of that name. Named indices let a formula read as it does in index notation. A named index used alone in a selection reads the axis with that name, wherever it stands in the brackets. The result’s axis keeps that name. Selectors that share a name pair with each other and read the data’s axes in order, as m.[i i] below does. Reading at a position outside its axis returns the fill, as for any selector. As the ⍠ operand, a named index specifies its axis:
["i":3 "j":3]ₓ
["i":3]ρ[0 4 8]
12
["k":4]ρ[10 10 10 10]ₓ
$t
•index gives an index with a new axis name that starts with _ and cannot collide with names on the caller’s data. Plain display leaves that name out. Box mode and ρ show it shortened, as in _·…. Indices with different names build a grid, here of the differences between every pair of items:
| Expression | Error |
|---|---|
+/⍠"missing" C |
INDEX |
["city":2 "city":3]ρ0 |
DOMAIN |
Array converts Python dicts to keyed vectors recursively. .py converts them back to dicts.
Pass keys to Array to attach position keys, and names to attach axis names. None leaves an axis without keys or a name. The .keys and .names properties return the position keys and axis names. .np copies the values to NumPy.
rows, cols = ['NY', 'LA'], ['Jan', 'Feb', 'Mar']
data = [[10, 20, 30], [40, 50, 60]]
sales = Array(data, keys=(rows, cols), names=('city', 'month'))
assert sales.keys == (tuple(rows), tuple(cols))
assert sales.names == ('city', 'month')
assert sales.np.tolist() == data
columns_only = Array(data, keys=(None, cols), names=(None, 'month'))
assert columns_only.keys == (None, tuple(cols))
assert columns_only.names == (None, 'month').df converts to a pandas DataFrame. For a matrix, the position keys become the row and column labels, and the axis names become the names of the index and the columns. .py uses this conversion for keyed arrays of rank 2 or more. Install pandas with pip install pandas. basedpl imports pandas only when converting.
Above rank 2, the last axis supplies the columns, and the earlier axes form a row MultiIndex. Unkeyed axes use labels that count from 0. Units and vectors become one-column DataFrames.
counts = np.arange(12).reshape(2, 2, 3)
names = 'year', 'city', 'month'
cube = Array(counts, keys=(None, rows, cols), names=names)
assert cube.df.index.names == ['year', 'city']
assert cube.df.loc[(1, 'LA'), 'Mar'] == 11
assert Array({'aa': 1, 'bb': 2}).df.loc['bb', 0] == 2
assert Array(7).df.loc[0, 0] == 7•json converts ordinary JSON objects to keyed vectors. The process protocol includes axis_keys and axis_names alongside the shape, row-major data and prototype. For sales above:
Use null for an axis without keys or a name. Omit axis_keys when no axis has keys, and axis_names when no axis has a name: