Primitives¶
primitives
¶
Primitives: the expression builders DFS composes into features.
Base classes¶
Primitive
¶
Bases: ABC
Base class for every primitive.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Registry key, also the upper-cased stem of generated names. |
input_dtypes |
tuple[DtypeFamily, ...]
|
One dtype family per input. Empty means the primitive
takes no column input, e.g. |
output_dtype |
Any
|
Fixed output dtype, or None to preserve the first input's. |
commutative |
bool
|
Whether argument order is irrelevant, so that only one
of |
stack_on_self |
bool
|
Whether this primitive may be applied to its own output. |
default_value |
Any
|
Value substituted for empty groups after a left join. |
return_dtype
¶
Compute the output dtype without touching data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dtypes
|
tuple[Any, ...]
|
Dtypes of the input features, in order. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The dtype of this primitive's output. |
generate_name
¶
Build the column name for an application of this primitive.
Every part is joined with __ so the result is a plain SQL
identifier. Parentheses and commas would be parsed as a function call
by any backend that generates SQL; see
:meth:generate_display_name for the readable form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg_names
|
Sequence[str]
|
Names of the inputs. For a zero-input aggregation
this is the child table's name, giving e.g.
|
required |
Returns:
| Type | Description |
|---|---|
str
|
The feature name. |
generate_display_name
¶
Build the readable name for an application of this primitive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg_names
|
Sequence[str]
|
Display names of the inputs. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The conventional parenthesised form, e.g. |
output_names
¶
Expand a feature name into one name per output column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_name
|
str
|
The name from :meth: |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
One name per output column; indexed when there is more than one. |
display_output_names
¶
Expand a display name into one readable name per output column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_name
|
str
|
The name from :meth: |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
One name per output column; indexed when there is more than one. |
outputs
¶
Normalize :meth:build to a tuple of expressions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*inputs
|
Expr
|
One expression per declared input. |
()
|
Returns:
| Type | Description |
|---|---|
tuple[Expr, ...]
|
One expression per output column. |
build
abstractmethod
¶
Build this primitive's narwhals expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*inputs
|
Expr
|
One expression per declared input. |
()
|
Returns:
| Type | Description |
|---|---|
Expr | Sequence[Expr]
|
A single expression, or a sequence for multi-output primitives. |
AggregationPrimitive
¶
TransformPrimitive
¶
Bases: Primitive
A primitive applied row-wise within a single table.
Attributes:
| Name | Type | Description |
|---|---|---|
order_dependent |
bool
|
Whether the expression needs an explicit ordering.
Narwhals requires |
Registry¶
register
¶
Register a primitive class under its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[_P]
|
The primitive class to register. |
required |
Returns:
| Type | Description |
|---|---|
type[_P]
|
The class unchanged, so this works as a decorator. |
Raises:
| Type | Description |
|---|---|
PrimitiveError
|
If the name is already registered to a different class. Re-registering the same class under its own name is a no-op. |
resolve
¶
Turn a name or instance into a primitive instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
str | Primitive
|
A registered primitive name, or an already-built instance. |
required |
Returns:
| Type | Description |
|---|---|
Primitive
|
A primitive instance. |
Raises:
| Type | Description |
|---|---|
PrimitiveError
|
If the name is not registered, or the primitive is not a frozen dataclass with equality enabled. |
resolve_all
¶
Aggregation primitives¶
AGG_DEFAULTS
module-attribute
¶
Count
dataclass
¶
Bases: AggregationPrimitive
Number of child rows in the group.
build
¶
Build the row-count expression.
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression counting rows. |
Sum
dataclass
¶
Bases: AggregationPrimitive
Sum of a numeric column.
build
¶
Build the sum expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to sum. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
Mean
dataclass
¶
Bases: AggregationPrimitive
Arithmetic mean of a numeric column.
build
¶
Build the mean expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to average. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
Min
dataclass
¶
Bases: AggregationPrimitive
Smallest value of a numeric column.
build
¶
Build the minimum expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to reduce. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
Max
dataclass
¶
Bases: AggregationPrimitive
Largest value of a numeric column.
build
¶
Build the maximum expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to reduce. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
Std
dataclass
¶
Bases: AggregationPrimitive
Sample standard deviation of a numeric column.
build
¶
Build the standard-deviation expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to reduce. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
Median
dataclass
¶
Bases: AggregationPrimitive
Median of a numeric column.
build
¶
Build the median expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to reduce. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
NUnique
dataclass
¶
Bases: AggregationPrimitive
Number of distinct known values in a column; nulls are not a value.
build
¶
Build the distinct-count expression, excluding null.
Polars counts null as one more distinct value, which contradicts this
primitive's own default_value: a customer whose only session had no
transactions would report 0 rows and 1 distinct value at once.
featuretools' NUM_UNIQUE excludes nulls, so counting them would
also diverge silently on any data containing them.
Subtracting a null indicator is deliberate rather than
expr.drop_nulls().n_unique(): drop_nulls is a length-changing
(filtration) expression, and narwhals rejects those inside a lazy
group_by().agg() on backends that cannot express them. Both
operands here are plain reductions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to count distinct values of. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
PercentTrue
dataclass
¶
Bases: AggregationPrimitive
Fraction of rows where a boolean column is true.
build
¶
Build the true-fraction expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The boolean column. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
Quantiles
dataclass
¶
Bases: AggregationPrimitive
Several quantiles of a numeric column, one output column per quantile.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Registry key. |
|
input_dtypes |
Tuple containing one dtype family (NUMERIC). |
|
output_dtype |
The output dtype (Float64). |
|
qs |
tuple[float, ...]
|
The quantiles to compute, each in [0, 1]. |
build
¶
Build one expression per quantile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
The column to reduce. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[Expr]
|
One narwhals expression per quantile. |
Transform primitives¶
Year
dataclass
¶
Bases: TransformPrimitive
Calendar year.
build
¶
Build the calendar-year expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the calendar year. |
Month
dataclass
¶
Bases: TransformPrimitive
Calendar month, 1-12.
build
¶
Build the calendar-month expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the calendar month. |
Day
dataclass
¶
Bases: TransformPrimitive
Day of month, 1-31.
build
¶
Build the day-of-month expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the day of month. |
Hour
dataclass
¶
Bases: TransformPrimitive
Hour of day, 0-23.
build
¶
Build the hour-of-day expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the hour of day. |
Weekday
dataclass
¶
Bases: TransformPrimitive
ISO weekday, 1 (Monday) to 7 (Sunday).
build
¶
Build the ISO-weekday expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the ISO weekday. |
IsWeekend
dataclass
¶
Bases: TransformPrimitive
Whether the date falls on a Saturday or Sunday.
build
¶
Build the weekend-indicator expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals boolean expression. |
Absolute
dataclass
¶
Bases: TransformPrimitive
Absolute value.
build
¶
Build the absolute-value expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression with absolute values. |
NaturalLog
dataclass
¶
Bases: TransformPrimitive
Natural logarithm. Non-positive inputs yield null or negative infinity.
build
¶
Build the natural-logarithm expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of natural logarithms. |
AddNumeric
dataclass
¶
Bases: TransformPrimitive
Sum of two numeric columns.
build
¶
Build the addition expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Expr
|
First column. |
required |
right
|
Expr
|
Second column. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
SubtractNumeric
dataclass
¶
Bases: TransformPrimitive
Difference of two numeric columns.
build
¶
Build the subtraction expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Expr
|
First numeric expression. |
required |
right
|
Expr
|
Second numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the difference. |
MultiplyNumeric
dataclass
¶
Bases: TransformPrimitive
Product of two numeric columns.
build
¶
Build the multiplication expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Expr
|
First column. |
required |
right
|
Expr
|
Second column. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression. |
DivideNumeric
dataclass
¶
Bases: TransformPrimitive
Ratio of two numeric columns.
build
¶
Build the division expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Expr
|
First numeric expression (numerator). |
required |
right
|
Expr
|
Second numeric expression (denominator). |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of the ratio. |
Order-dependent transform primitives¶
CumSum
dataclass
¶
Bases: TransformPrimitive
Running total in row-creation order.
build
¶
Build the cumulative-sum expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of cumulative sum. |
CumCount
dataclass
¶
Bases: TransformPrimitive
Running count of non-null values in row-creation order.
build
¶
Build the cumulative-count expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
An expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of cumulative count. |
CumMin
dataclass
¶
Bases: TransformPrimitive
Running minimum in row-creation order.
build
¶
Build the cumulative-minimum expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of cumulative minimum. |
CumMax
dataclass
¶
Bases: TransformPrimitive
Running maximum in row-creation order.
build
¶
Build the cumulative-maximum expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of cumulative maximum. |
Diff
dataclass
¶
Bases: TransformPrimitive
Change from the previous row in row-creation order.
build
¶
Build the row-difference expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A numeric expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of differences. |
TimeSincePrevious
dataclass
¶
Bases: TransformPrimitive
Seconds elapsed since the previous row in row-creation order.
build
¶
Build the elapsed-seconds expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
A temporal expression. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A narwhals expression of time elapsed in seconds. |