Schema, Selectors, and Column Roles
Dataframe schemas, column selectors, and column roles work together to make column-oriented operations explicit and reusable. A schema snapshot describes the current columns. A selector describes which columns to use. Roles attach contextual purpose metadata to columns in a specific data frame.
Inspect a schema snapshot
Call GetSchema() to get a read-only snapshot of the current column schema. The schema preserves the data frame's typed column keys and records each column's key, ordinal, element type, missing-value metadata, categorical category-index metadata, semantic column metadata, and role. A schema object is for inspection and selector resolution; it does not mutate the data frame.
The row-key type is also part of DataFrameSchema<R, C>. A schema does not contain actual row keys, row count, or a profile of the data. Those belong to the data frame instance.
AllowsMissingValues describes whether the column's logical domain and representation allow missing values. It does not report whether the current data contains any missing values. A column can allow missing values even when all its current values are present. Missing-value representation and the runtime flags used to decide whether an operation checks for missing values are distinct from this schema fact.
var schema = frame.GetSchema();
foreach (var column in schema.Columns)
Console.WriteLine($"{column.Key}: {column.ElementType.Name}");Schema snapshots do not update after they are returned. If you change the frame structure, vector-owned metadata, semantic metadata, or column roles, call GetSchema() again to inspect the new state.
Select columns by schema metadata
A ColumnSelector is a reusable column-selection request. The type is non-generic because many selectors do not depend on the column-key type. Key-based factories such as Key<C> and typed key predicates carry the key type where needed.
Selectors resolve against schema metadata, not vector values. Results are distinct and returned in schema order. Explicit missing keys throw. Type, role, predicate, and categorical selectors that match nothing return an empty selection. A key selector resolved against a schema with an incompatible column-key type throws.
Selectors can be used directly with dataframe operations such as GetColumns and RemoveColumns.
var numericColumns = frame.GetColumns(
ColumnSelector.OfType<double>());Assign column roles
ColumnRole values are dataframe-owned metadata. They describe how a column should be interpreted in that data frame, such as a feature, target, grouping key, weight, offset, or identifier. The default role, None, means no explicit role has been assigned.
Roles are assigned or cleared on the data frame. Later schema snapshots reflect the current roles, while previously returned schema objects remain unchanged.
frame.SetColumnRole(ColumnSelector.OfType<double>(), ColumnRole.Feature);
frame.SetColumnRole("target", ColumnRole.Target);
var roleSnapshot = frame.GetSchema();Role selectors make role-based column selections concise:
var features = frame.GetColumns(ColumnSelector.HasRole(ColumnRole.Feature));
frame.ClearColumnRole(ColumnSelector.HasRole(ColumnRole.Feature));Assign semantic column metadata
ColumnSemanticInfo stores semantic value metadata that is separate from contextual column roles. The current semantic metadata records explicit MeasurementScale values. Use SetColumnSemantics to assign semantic metadata to one column, and inspect the captured value through Semantics on a schema snapshot.
Categorical representation and measurement scale are separate concepts. IsCategorical indicates categorical-vector structure, while MeasurementScale describes the meaning of the represented values. A categorical column may therefore validly use nominal, ordinal, interval, or ratio scale.
Preserve metadata when column identity is preserved
Dataframe-owned column metadata follows a column when an operation preserves that column's identity. Column subsets, selector-based selection, row-only selections, clones, and one-to-one column key replacement preserve current role and semantic metadata for retained columns. Removed columns do not leave dataframe-owned metadata behind, and newly added logical columns start with default metadata. In-place column replacement also preserves role and semantic metadata: when the replacement changes the key, the metadata follows the column to its new key. Appending or inserting a derived column creates a new logical column, so assign its role and semantics explicitly. After transforming values, review retained semantics to ensure they still describe the transformed data.
frame.SetColumnRole("sales", ColumnRole.Feature);
var salesOnly = frame.GetColumns(ColumnSelector.Key("sales"));
salesOnly.RenameColumn("sales", "revenue");
var revenueRole = salesOnly.GetSchema()["revenue"].Role;