Back to Gotchas & Edge Cases

Attributes · Gotchas & Edge Cases · Practice Question

How Do You Remove an Attribute From a List or Column?

Sign in to track your progress

Question

How do you remove an attribute from a list or a table column in kdb+?

Solution

/ Remove attribute from a list using the null symbol `
n: `s# til 10
attr n           / `s

n: `#n
attr n           / ` — attribute removed

/ Remove from a table column
t: ([] sym:`g# `AAPL`GOOG`AAPL; price: 100 200 100)
attr t`sym       / `g

update `#sym from `t    / remove attribute from sym column in-place
attr t`sym              / ` — removed
/ or:
@[`t; `sym; `#]         / equivalent functional form

Explanation

The null symbol ` (a symbol with no name) acts as an "attribute eraser" when applied with #. Just as `s#, `u#, `p#, and `g# each apply their respective attribute, `# removes whichever attribute is currently set — no matter which one it is.

/ Works on any attribute type:
`# `s# til 10    / removes `s
`# `u# til 10    / removes `u
`# `g# `a`b`a    / removes `g

When would you need to remove an attribute?

  • When an attribute no longer reflects how the data will be used — for example, removing g# from a column you no longer query by, to save the memory its index uses.
  • Before bulk loads — removing g# before loading a very large batch of rows, then re-applying it once at the end, avoids maintaining the index row by row. Whether this is faster for your data is worth measuring:
bulkData: ([] sym:100000?`AAPL`GOOG`MSFT; price:100000?1000)
@[`t; `sym; `#]         / remove g# before the bulk insert
`t insert bulkData
@[`t; `sym; `g#]        / re-apply after all rows are inserted

Always assign the result. Unlike applying s# to an already-sorted list, removing an attribute does not change the original variable:

n: `s# til 10
`#n              / returns an unattributed list, but...
attr n           / `s — ...n itself still has the attribute
n: `#n
attr n           / `