Skip to content

[Enh]: Support Arithmetic and Logical Expr methods (like Polars) #3153

@arv-anshul

Description

@arv-anshul

We would like to learn about your use case. For example, if this feature is needed to adopt Narwhals in an open source project, could you please enter the link to it below?

Currently, chaining arithmetic and logical operations in Narwhals requires wrapping expressions in parentheses to ensure the correct order of operations. In contrast, Polars supports method-based alternatives for operators such as .add(), .sub(), .and_(), .eq(), etc., on expressions. This leads to more readable and fluent code.

Please describe the purpose of the new feature or describe the problem to solve.

It would be highly beneficial for Narwhals to implement a similar API, allowing expressions to be chained cleanly using methods instead of infix operators.

Current behaviour

(nw.col("price") / nw.col("item_count")).round(2)
((nw.col("a") > 10) & (nw.col("b") < 5)) | (nw.col("c") == 100)

After feature implementation

nw.col("price").truediv("item_count").round(2)

nw.col("a").gt(10).and_(nw.col("b").lt(5)).or_(nw.col("c").eq(100))

nw.col("discount").eq(0).or_(nw.col("total").gt(100)).and_(nw.col("status").eq("valid"))

nw.col("score").sub("penalty").mul(1.5).ge(75)

Suggest a solution if possible.

No response

If you have tried alternatives, please describe them below.

These methods are present in Polars API.

Arithmetic Operations from Polars API

Method Description
add Method equivalent of addition operator expr + other
sub Method equivalent of subtraction operator expr - other
mul Method equivalent of multiplication operator expr * other
truediv Method equivalent of float division operator expr / other
floordiv Method equivalent of integer division operator expr // other
mod Method equivalent of modulus operator expr % other
pow Method equivalent of exponentiation operator expr ** exponent
neg Method equivalent of unary minus operator -expr

Logical Operations from Polars API

Method Description
and_ Method equivalent of bitwise "and" operator expr & other & ...
or_ Method equivalent of bitwise "or" operator `expr
eq Method equivalent of equality operator expr == other
eq_missing Method equivalent of equality operator expr == other where None == None
ne Method equivalent of inequality operator expr != other
ne_missing Method equivalent of equality operator expr != other where None == None
ge Method equivalent of “greater than or equal” operator expr >= other
gt Method equivalent of “greater than” operator expr > other
le Method equivalent of “less than or equal” operator expr <= other
lt Method equivalent of “less than” operator expr < other
not_ Negate a boolean expression
xor Method equivalent of bitwise exclusive‑or operator expr ^ other

Additional information that may help us understand your needs.

Check polars.Expr docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions