Column Metadata Conventions¶
Store labels, definitions, units and other column documentation with your parquet files. polarstory uses this convention to generate data dictionaries.
Metadata Structure¶
Use the file-level key columns. Its value is a json object keyed by column
name. Each column has its own dictionary of attributes.
import json
import polars as pl
# document the columns
df = pl.DataFrame({
'age': [42, 53],
'diabetes': [0, 1],
'patient_id': ['a', 'b'],
'score': [3, 7],
})
columns = {
'age': {
'label': 'Index age',
'description': 'Completed years of age on the index date',
'unit': 'years',
'kind': 'continuous',
'source': 'Enrollment',
'group': 'Demographics',
},
'diabetes': {
'label': 'Diabetes diagnosis',
'kind': 'binary',
'value_labels': {0: 'No', 1: 'Yes'},
'group': 'Comorbidities',
},
'patient_id': {'label': 'Patient identifier', 'sensitive': True},
'score': {'value_labels': 'Higher scores indicate greater severity'},
}
# encode the column dictionary as json
df.write_parquet('cohort.parquet', metadata={'columns': json.dumps(columns)})
Parquet key-value metadata accepts strings. Use json.dumps() to store the
column dictionary and json.loads() to retrieve it.
# read metadata without loading observations
metadata = pl.read_parquet_metadata('cohort.parquet')
columns = json.loads(metadata.get('columns', '{}'))
print(columns['age']['label'])
Use exact column names, including capitalization. Metadata is optional for both the dataset and individual columns. It does not rename columns or change their data types or values.
Preferred Attributes¶
| Attribute | Suggested type | Meaning | Example |
|---|---|---|---|
label |
str | Short display name | Age at cohort entry |
description |
str | Definition or longer explanation | Age in completed years on the index date |
kind |
str | Descriptive classification | continuous, binary, ordinal |
sensitive |
bool | Sensitive-value suppression in polarstory | True |
value_labels |
dict or str | Code labels or explanation of values | {0: 'No', 1: 'Yes'} |
unit |
str | Unit of measurement | years, mg/dL, mmHg |
source |
str | Source table, system or dataset | Enrollment |
group |
str | Logical grouping | Demographics, Outcomes |
All attributes are optional. The suggested types and names are not a validation schema. Add your own json-compatible attributes as needed.
Custom Attributes¶
# add dataset-specific documentation
columns['age'].update({
'definition': 'Age at the index date',
'derived': True,
'valid_range': [18, 120],
})
Polarstory displays custom fields under their original names.
Value Labels¶
Use a dictionary to document coded values. Use a string to explain a scale or values that do not need individual labels.
columns = {
# numeric codes
'diabetes': {'value_labels': {0: 'No', 1: 'Yes', 9: 'Unknown'}},
# text codes
'sex': {'value_labels': {'F': 'Female', 'M': 'Male', 'U': 'Unknown'}},
# codes with leading zeros
'site_id': {'value_labels': {'001': 'Site A', '002': 'Site B'}},
# boolean codes
'enrolled': {'value_labels': {False: 'No', True: 'Yes'}},
# scale description
'score': {'value_labels': '0–10; higher scores indicate greater severity'},
}
- json converts dictionary keys to strings:
1becomes'1'andTruebecomes'true'. - Use strings for codes with leading zeros.
- Value labels do not recode values or define missing values.
Using Metadata in polarstory¶
| Field | How polarstory uses it... |
|---|---|
label, description |
Display strings in reports and dictionaries |
value_labels |
Label frequencies using code-to-string mappings; display text descriptions in the dictionary |
sensitive |
Suppress sensitive values and value labels by default |
kind, unit, source, group, custom fields |
Display in the data dictionary |
To change how polarstory profiles a column, use Variables.set().
import polarstory as ps
# treat a numeric-coded variable as categorical
variables = ps.Variables().set('diabetes', kind='categorical')
p = ps.profile('cohort.parquet', variables=variables)
Recommendations¶
- Use short labels and put detailed definitions in
descriptionordefinition. - Use consistent units, code labels and group names across datasets and partitions.
- Update documentation when renaming, dropping, or deriving columns.
- Carry metadata forward explicitly when writing a new parquet file.
- Mark sensitive columns with
sensitive=Trueto avoid having values explicitly included in reports.
See Working with column metadata for examples of generating data dictionaries, editing metadata, and preserving it when rewriting datasets.