UNPKG

nodejs-polars

Version:

Polars: Blazingly fast DataFrames in Rust, Python, Node.js, R and SQL

82 lines (81 loc) 2.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.when = when; const polars_internal_1 = __importDefault(require("../internals/polars_internal")); const expr_1 = require("./expr"); function ChainedWhen(_chainedwhen) { return { then: ({ _expr }) => ChainedThen(_chainedwhen.then(_expr)), }; } function ChainedThen(_chainedthen) { return { when: ({ _expr }) => ChainedWhen(_chainedthen.when(_expr)), then: ({ _expr }) => ChainedThen(_chainedthen.then(_expr)), otherwise: ({ _expr }) => expr_1.Expr(_chainedthen.otherwise(_expr)), }; } function Then(_then) { return { when: ({ _expr }) => ChainedWhen(_then.when(_expr)), otherwise: ({ _expr }) => expr_1.Expr(_then.otherwise(_expr)), }; } /** * Utility function. * @see {@link when} */ function When(_when) { return { then: ({ _expr }) => Then(_when.then(_expr)), }; } /** * Start a when, then, otherwise expression. * * @example * ``` * // Below we add a column with the value 1, where column "foo" > 2 and the value -1 where it isn't. * > df = pl.DataFrame({"foo": [1, 3, 4], "bar": [3, 4, 0]}) * > df.withColumn(pl.when(pl.col("foo").gt(2)).then(pl.lit(1)).otherwise(pl.lit(-1))) * shape: (3, 3) * ┌─────┬─────┬─────────┐ * │ foo ┆ bar ┆ literal │ * │ --- ┆ --- ┆ --- │ * │ i64 ┆ i64 ┆ i32 │ * ╞═════╪═════╪═════════╡ * │ 1 ┆ 3 ┆ -1 │ * ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ * │ 3 ┆ 4 ┆ 1 │ * ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ * │ 4 ┆ 0 ┆ 1 │ * └─────┴─────┴─────────┘ * * // Or with multiple `when, thens` chained: * > df.with_column( * ... pl.when(pl.col("foo").gt(2)) * ... .then(1) * ... .when(pl.col("bar").gt(2)) * ... .then(4) * ... .otherwise(-1) * ... ) * shape: (3, 3) * ┌─────┬─────┬─────────┐ * │ foo ┆ bar ┆ literal │ * │ --- ┆ --- ┆ --- │ * │ i64 ┆ i64 ┆ i32 │ * ╞═════╪═════╪═════════╡ * │ 1 ┆ 3 ┆ 4 │ * ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ * │ 3 ┆ 4 ┆ 1 │ * ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ * │ 4 ┆ 0 ┆ 1 │ * └─────┴─────┴─────────┘ * ``` */ function when(expr) { return When(polars_internal_1.default.when(expr._expr)); }