Databases¶
database
¶
The schema model: tables, relationships, and the database that holds them.
Database¶
Database
¶
A collection of related tables that DFS can synthesize features over.
Create an empty database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable identifier for this database. |
required |
add_table
¶
Add a table to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the table under. |
required |
table
|
Any
|
A native frame or a narwhals frame, eager or lazy. It is lazified on the way in, so the two forms are interchangeable and may be mixed within one database. |
required |
primary_key
|
str | None
|
Column uniquely identifying a row. Required for a table used as a relationship parent or as the DFS target. |
None
|
row_creation_time
|
str | None
|
Column recording when a row became knowable. Required for order-dependent primitives on this table. |
None
|
validate
|
bool | str | Iterable[str]
|
Checks to run against the data before registering the
table. |
False
|
Returns:
| Type | Description |
|---|---|
Database
|
This database, to allow chaining. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
If the name is taken, a declared column is missing, a key is composite, or the backend differs from earlier tables. |
Warns:
| Type | Description |
|---|---|
MissingPrimaryKeyWarning
|
If |
add_relationship
¶
Link a parent table to a child table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
str
|
Name of the parent table. Must have a |
required |
child
|
str
|
Name of the child table. |
required |
foreign_key
|
str
|
The child's column pointing at the parent's primary key. |
required |
validate
|
bool | str | Iterable[str]
|
Which relationship checks to run before registering the
link. Defaults to |
'matching_key_dtypes'
|
Returns:
| Type | Description |
|---|---|
Database
|
This database, to allow chaining. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
If a table is unknown, the parent has no primary key, the foreign key is composite, or the child lacks that column. |
validate
¶
Run validation checks against the database.
Each argument selects from its own registry, and takes the same forms
as validate= on :meth:add_table: True for every check in
that scope, False for none, a check name, or an iterable of names.
A name outside the scope it is given to raises :class:ValueError.
Table checks run against every table in insertion order, then
relationship checks against every relationship, then database-wide
checks once. The first failure raises
:class:~tusk.exceptions.ValidationError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
bool | str | Iterable[str]
|
Checks spanning the whole database. |
True
|
tables
|
bool | str | Iterable[str]
|
Checks run against each table. |
True
|
relationships
|
bool | str | Iterable[str]
|
Checks run against each relationship. |
True
|
Returns:
| Type | Description |
|---|---|
Database
|
This database, to allow chaining. |
schema
¶
Return a table's schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
TableSchema
|
The table's schema. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
If the table is unknown. |
frame
¶
Return a table's lazy frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
The table's narwhals LazyFrame. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
If the table is unknown. |
children_of
¶
Return relationships where this table is the parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
list[Relationship]
|
Matching relationships, in insertion order. |
parents_of
¶
Return relationships where this table is the child.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
list[Relationship]
|
Matching relationships, in insertion order. |
input_excluded_columns
¶
Return columns that may not be fed to a primitive as an input.
Join keys only: the primary key and every foreign key. They identify
rows rather than measure anything, so MEAN(customer_id) is noise.
Foreign keys remain usable as groupby keys.
The row_creation_time is deliberately not here. It is a real
measurement — MONTH(signed_up_at)-style temporal transforms, and
N_UNIQUE or CUM_COUNT over a temporal column, are exactly the
features this split unblocks — and excluding it would leave
a zero-configuration run with no transform features at all. Contrast
:meth:output_excluded_columns, which does exclude it; conflating the
two sets is a bug this split exists to prevent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
The table's join-key column names. |
output_excluded_columns
¶
Return raw columns that never appear in the feature matrix.
Everything in :meth:input_excluded_columns, plus the
row_creation_time: passing the time index through as a feature
invites target leakage, and featuretools drops it from the matrix for
the same reason. Derived features over the row creation time, such as
MONTH(signed_up_at), are unaffected — only the raw column is
dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
Column names to omit from the feature matrix. |
Relationship¶
Relationship
dataclass
¶
A one-to-many link from a parent table to a child table.
The parent side is always the parent's primary_key; foreign_key
names the child's column.
Attributes:
| Name | Type | Description |
|---|---|---|
parent |
str
|
Name of the parent table. |
child |
str
|
Name of the child table. |
foreign_key |
str
|
Column on the child pointing at the parent's primary key. |
TableSchema¶
TableSchema
dataclass
¶
Everything phase 1 knows about a table.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Table name within the database. |
primary_key |
str | None
|
Column uniquely identifying a row, if declared. |
row_creation_time |
str | None
|
Column recording when a row became knowable. |
dtypes |
Mapping[str, Any]
|
Mapping of column name to narwhals dtype. |