Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/api-reference/expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
options:
members:
- abs
- add
- alias
- all
- and_
- any
- cast
- count
Expand All @@ -17,10 +19,14 @@
- cum_sum
- diff
- drop_nulls
- eq
- ewm_mean
- exp
- fill_null
- filter
- floordiv
- ge
- gt
- clip
- is_between
- is_duplicated
Expand All @@ -32,18 +38,25 @@
- is_null
- is_unique
- kurtosis
- le
- len
- log
- lt
- map_batches
- max
- mean
- median
- min
- mode
- mul
- ne
- not_
- null_count
- n_unique
- or_
- over
- pipe
- pow
- quantile
- rank
- replace_strict
Expand All @@ -56,7 +69,9 @@
- skew
- sqrt
- std
- sub
- sum
- truediv
- unique
- var
show_source: false
Expand Down
172 changes: 169 additions & 3 deletions narwhals/expr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import functools
import math
import operator
from collections.abc import Iterable, Mapping, Sequence
from typing import TYPE_CHECKING, Any, Callable

Expand Down Expand Up @@ -196,6 +198,17 @@ def __eq__(self, other: Self | Any) -> Self: # type: ignore[override]
ExprMetadata.from_binary_op(self, other),
)

def eq(self, other: Self | Any) -> Self:
"""Method equivalent of equality operator `expr == other`.

Arguments:
other: A literal or expression value to compare against.

Returns:
A new expression.
"""
return self.__eq__(other)

def __ne__(self, other: Self | Any) -> Self: # type: ignore[override]
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -204,6 +217,17 @@ def __ne__(self, other: Self | Any) -> Self: # type: ignore[override]
ExprMetadata.from_binary_op(self, other),
)

def ne(self, other: Self | Any) -> Self:
"""Method equivalent of equality operator `expr != other`.

Arguments:
other: A literal or expression value to compare against.

Returns:
A new expression.
"""
return self.__ne__(other)

def __and__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -212,6 +236,17 @@ def __and__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def and_(self, *others: Any) -> Self:
"""Method equivalent of bitwise "and" operator `expr & other & ...`.

Arguments:
*others: One or more integer or boolean expressions to evaluate/combine.

Returns:
A new expression.
"""
return functools.reduce(operator.and_, (self, *others)) # type: ignore[no-any-return]

def __rand__(self, other: Any) -> Self:
return (self & other).alias("literal") # type: ignore[no-any-return]

Expand All @@ -223,6 +258,17 @@ def __or__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def or_(self, *others: Any) -> Self:
"""Method equivalent of bitwise "or" operator `expr | other | ...`.

Arguments:
*others: One or more integer or boolean expressions to evaluate/combine.

Returns:
A new expression.
"""
return functools.reduce(operator.or_, (self, *others)) # type: ignore[no-any-return]

def __ror__(self, other: Any) -> Self:
return (self | other).alias("literal") # type: ignore[no-any-return]

Expand All @@ -234,6 +280,17 @@ def __add__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def add(self, other: Any) -> Self:
"""Method equivalent of addition operator `expr + other`.

Arguments:
other: numeric or string value; accepts expression input.

Returns:
A new expression.
"""
return self.__add__(other)

def __radd__(self, other: Any) -> Self:
return (self + other).alias("literal") # type: ignore[no-any-return]

Expand All @@ -245,6 +302,17 @@ def __sub__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def sub(self, other: Any) -> Self:
"""Method equivalent of subtraction operator `expr - other`.

Arguments:
other: Numeric literal or expression value.

Returns:
A new expression.
"""
return self.__sub__(other)

def __rsub__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -261,6 +329,17 @@ def __truediv__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def truediv(self, other: Any) -> Self:
"""Method equivalent of division operator `expr / other`.

Arguments:
other: Numeric literal or expression value.

Returns:
A new expression.
"""
return self.__truediv__(other)

def __rtruediv__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -277,6 +356,17 @@ def __mul__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def mul(self, other: Any) -> Self:
"""Method equivalent of multiplication operator `expr * other`.

Arguments:
other: Numeric literal or expression value.

Returns:
A new expression.
"""
return self.__mul__(other)

def __rmul__(self, other: Any) -> Self:
return (self * other).alias("literal") # type: ignore[no-any-return]

Expand All @@ -288,6 +378,17 @@ def __le__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def le(self, other: Any) -> Self:
"""Method equivalent of "less than or equal" operator `expr <= other`.

Arguments:
other: A literal or expression value to compare with.

Returns:
A new expression.
"""
return self.__le__(other)

def __lt__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -296,6 +397,17 @@ def __lt__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def lt(self, other: Any) -> Self:
"""Method equivalent of "less than" operator `expr < other`.

Arguments:
other: A literal or expression value to compare with.

Returns:
A new expression.
"""
return self.__lt__(other)

def __gt__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -304,6 +416,17 @@ def __gt__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def gt(self, other: Any) -> Self:
"""Method equivalent of "greater than" operator `expr > other`.

Arguments:
other: A literal or expression value to compare with.

Returns:
A new expression.
"""
return self.__gt__(other)

def __ge__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -312,6 +435,17 @@ def __ge__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def ge(self, other: Any) -> Self:
"""Method equivalent of "greater than or equal" operator `expr >= other`.

Arguments:
other: A literal or expression value to compare with.

Returns:
A new expression.
"""
return self.__ge__(other)

def __pow__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -320,6 +454,17 @@ def __pow__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def pow(self, other: Any) -> Self:
"""Method equivalent of exponentiation operator `expr ** exponent`.

Arguments:
other: Numeric literal or expression exponent value.

Returns:
A new expression.
"""
return self.__pow__(other)

def __rpow__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand All @@ -336,6 +481,17 @@ def __floordiv__(self, other: Any) -> Self:
ExprMetadata.from_binary_op(self, other),
)

def floordiv(self, other: Any) -> Self:
"""Method equivalent of integer division operator `expr // other`.

Arguments:
other: Numeric literal or expression value.

Returns:
A new expression.
"""
return self.__floordiv__(other)

def __rfloordiv__(self, other: Any) -> Self:
return self.__class__(
lambda plx: apply_n_ary_operation(
Expand Down Expand Up @@ -366,6 +522,14 @@ def __invert__(self) -> Self:
lambda plx: self._to_compliant_expr(plx).__invert__()
)

def not_(self) -> Self:
"""Method equivalent of inversion operator `~expr`.

Returns:
A new expression.
"""
return self.__invert__()

def any(self) -> Self:
"""Return whether any of the values in the column are `True`.

Expand Down Expand Up @@ -1353,9 +1517,11 @@ def fill_null(
strategy=strategy,
limit=limit,
),
self._metadata.with_orderable_window()
if strategy is not None
else self._metadata,
(
self._metadata.with_orderable_window()
if strategy is not None
else self._metadata
),
)

# --- partial reduction ---
Expand Down
Loading