Skip to content

typeset_base

Type system used to infer the variable type of each column.

The design (a tree of identity relations plus inference relations, walked from a generic root type) follows visions (https://github.com/dylan-profiler/visions) by Ian Eaves and Simon Brugman, which this package used for type inference until version 4. This implementation adds an explicit precedence order, so that inference is deterministic, and explain_type.

A typeset is an ordered collection of types. Each type declares relations to other types:

  • an IdentityRelation to its parent: the type is a refinement of the parent, decided by the type's own contains_op (e.g. URL is a Text whose values parse as URLs). Identity relations form a tree rooted at a subclass of Generic.
  • InferenceRelations from other types: a series of the source type can be converted into this type (e.g. a Text series of numeric strings becomes Numeric), with a predicate and a transformer.

detect_type walks the identity tree only and never changes the data. infer_type walks identity and inference relations and returns the cast series alongside the type.

Precedence is explicit: when more than one relation leaving a type matches, the target that comes first in the typeset's type order wins. The order of the sequence passed to BaseTypeSet is the precedence order, so inference is deterministic. explain_type reports every step and every candidate that matched, to make ambiguous columns visible.

Classes

TypeRelation dataclass

A directed relation related_type -> type.

IdentityRelation dataclass

Bases: TypeRelation

The owning type is a refinement of related_type.

The relationship defaults to the owning type's contains_op.

InferenceRelation dataclass

Bases: TypeRelation

A series of related_type can be converted into the owning type.

ProfilingTypeMeta

Bases: type

Attributes

relations property

The type's relations, bound to the type (cached per class).

ProfilingBaseType

Base class for variable types.

Generic

Bases: ProfilingBaseType

Root type: every series belongs to it.

BaseTypeSet

An ordered collection of types and the relations between them.

Args: types: the types, in precedence order. When several relations leaving the same type match a series, the one whose target comes first in this sequence wins. Exactly one type must subclass Generic; it is the root. Every other type needs exactly one IdentityRelation to a type in the set.

Methods:

parent_of(type_)

The identity parent of a type, None for the root.

topological_order()

Types ordered so that every parent comes before its children.

relations_from(type_, inferential=True)

Relations leaving type_, in precedence order.

detect(series)

Walk identity relations only. Returns (series, path, state).

infer(series)

Walk identity and inference relations. Returns (cast series, path, state).

infer_and_cast(series)

Inferred type and cast series from a single traversal.

explain_type(series, inferential=True)

Every step of the traversal with all the candidates that matched.

A step with more than one entry in matched is a column where the precedence order decided the type. Example::

[{"from": "Unsupported", "matched": ["Text"], "chosen": "Text"},
 {"from": "Text", "matched": ["Numeric", "Categorical"],
  "chosen": "Numeric"}]

Functions:

step_cache(state)

Cache for results computed on the series at the current traversal step.

Predicates and the transformer of the chosen relation see the same series, so a transformer can reuse what a predicate computed (a parsed column, a distinct count). The traversal clears this cache after every transform.

has_values(fn)

Decorator: a column without non-null values belongs to no type.

The column is a :class:~polars_data_profiling.model.probe.InferenceColumn, whose facts were computed for every column in one lazy pass.