lace.Codebook.column_metadata

property Codebook.column_metadata: _ColumnMetadataIndexer

Get/set a column metadata.

Examples

Get the metadata for column

>>> from lace.examples import Animals
>>> codebook = Animals().codebook
>>> codebook.column_metadata["swims"]
{
  "name": "swims",
  "coltype": {
    "Categorical": {
      "k": 2,
      "hyper": {
        "pr_alpha": {
          "shape": 1.0,
          "scale": 1.0
        }
      },
      "value_map": {
        "u8": 2
      },
      "prior": null
    }
  },
  "notes": null,
  "missing_not_at_random": false
}

Set the metadata for column

>>> from lace import CategoricalPrior, ColumnMetadata, ValueMap
>>> swims_metadata = ColumnMetadata.categorical(
...     "swims",
...     3,
...     value_map=ValueMap.int(3),
... ).missing_not_at_random(True)
>>> codebook.column_metadata["swims"] = swims_metadata
>>> codebook.column_metadata["swims"]
{
  "name": "swims",
  "coltype": {
    "Categorical": {
      "k": 3,
      "hyper": null,
      "value_map": {
        "u8": 3
      },
      "prior": null
    }
  },
  "notes": null,
  "missing_not_at_random": true
}

If you try to set the metadata with the wrong name, you will get a talking to.

>>> swims_metadata = ColumnMetadata.categorical(
...     "not-swims",
...     3,
...     value_map=ValueMap.int(3),
... ).missing_not_at_random(True)
>>> try:
...     codebook.column_metadata["swims"] = swims_metadata
... except KeyError as err:
...     assert "'not-swims', which is invalid" in str(err)
... else:
...     assert False

You can also use append, extend, and remove, just like you would with a list

>>> codebook.column_names[-5:]
['smart', 'group', 'solitary', 'nestspot', 'domestic']
>>> codebook.column_metadata.append(
...    ColumnMetadata.continuous("number-in-wild")
... )
>>> codebook.column_names[-5:]
['group', 'solitary', 'nestspot', 'domestic', 'number-in-wild']

Extend the column metadata:

>>> codebook.column_metadata.extend([
...    ColumnMetadata.categorical("eats-trash", 2),
...    ColumnMetadata.categorical("scary", 2),
... ])
>>> codebook.column_names[-5:]
['nestspot', 'domestic', 'number-in-wild', 'eats-trash', 'scary']

Remove a column metadata:

>>> codebook.column_metadata.remove('eats-trash')
{
  "name": "eats-trash",
  "coltype": {
    "Categorical": {
      "k": 2,
      "hyper": null,
      "value_map": {
        "u8": 2
      },
      "prior": null
    }
  },
  "notes": null,
  "missing_not_at_random": false
}
>>> codebook.column_names[-5:]
['solitary', 'nestspot', 'domestic', 'number-in-wild', 'scary']