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
IdentityRelationto its parent: the type is a refinement of the parent, decided by the type's owncontains_op(e.g.URLis aTextwhose values parse as URLs). Identity relations form a tree rooted at a subclass ofGeneric. InferenceRelations from other types: a series of the source type can be converted into this type (e.g. aTextseries of numeric strings becomesNumeric), 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
¶
ProfilingTypeMeta
¶
ProfilingBaseType
¶
Base class for variable types.
Generic
¶
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.