Column Operations API
API reference for adding, modifying, and transforming columns.
withColumn()
Add a new column or replace an existing column.
withColumn(name: string, expr: Expr): DataFrame<T & Record<K, V>>Parameters:
name- Name of the new or existing columnexpr- Expression defining the column values
Returns: DataFrame with the new/modified column
Example:
import { col } from "molniya";
// Add new column
df.withColumn("total", col("price").mul(col("quantity")))
// Replace existing column
df.withColumn("price", col("price").mul(1.1)) // 10% increasewithColumns()
Add or replace multiple columns at once.
withColumns(columns: Record<string, Expr> | Array<{name: string, expr: Expr}>): DataFrame<T>Parameters:
columns- Object or array of column definitions
Example:
import { col } from "molniya";
// Object syntax
df.withColumns({
tax: col("amount").mul(0.08),
total: col("amount").mul(1.08),
discount: col("amount").mul(0.1)
})
// Array syntax
df.withColumns([
{ name: "tax", expr: col("amount").mul(0.08) },
{ name: "total", expr: col("amount").mul(1.08) },
{ name: "discount", expr: col("amount").mul(0.1) }
])cast()
Cast columns to different data types.
cast(schema: Partial<Record<keyof T, DType>>): DataFrame<T>Parameters:
schema- Object mapping column names to data types
Example:
import { DType } from "molniya";
df.cast({
id: DType.int32,
amount: DType.float64,
count: DType.int32
})fillNull()
Fill null values with a specified value.
fillNull(value: Expr | unknown): ColumnRefParameters:
value- Value to replace nulls with
Example:
import { col } from "molniya";
// Fill with constant
df.withColumn("discount", col("discount").fillNull(0))
// Fill with another column
df.withColumn("nickname", col("nickname").fillNull(col("name")))fillNulls()
Fill null values in multiple columns.
fillNulls(values: Record<string, Expr | unknown>): DataFrame<T>Parameters:
values- Object mapping column names to fill values
Example:
df.fillNulls({
discount: 0,
tax_rate: 0.08,
status: "pending"
})dropNulls()
Remove rows with null values.
dropNulls(how: "any" | "all" = "any", subset?: string[]): DataFrame<T>Parameters:
how- "any" (drop if any null) or "all" (drop only if all null)subset- Optional columns to check (default: all columns)
Example:
// Drop rows with any null
df.dropNulls()
// Drop rows with all nulls
df.dropNulls("all")
// Drop only if specific columns are null
df.dropNulls("any", ["id", "name", "email"])Arithmetic Operations
add()
Addition.
add(other: Expr | number): ArithmeticExprExample:
col("a").add(col("b"))
col("value").add(10)sub()
Subtraction.
sub(other: Expr | number): ArithmeticExprExample:
col("total").sub(col("tax"))mul()
Multiplication.
mul(other: Expr | number): ArithmeticExprExample:
col("price").mul(col("quantity"))div()
Division.
div(other: Expr | number): ArithmeticExprExample:
col("sum").div(col("count"))mod()
Modulo (remainder).
mod(other: Expr | number): ArithmeticExprExample:
col("value").mod(10)neg()
Negation.
neg(): ArithmeticExprExample:
col("value").neg()Math Functions
round()
Round to specified decimal places.
round(decimals: number): ArithmeticExprExample:
col("price").round(2) // 2 decimal placesfloor()
Round down to nearest integer.
floor(): ArithmeticExprExample:
col("price").floor()ceil()
Round up to nearest integer.
ceil(): ArithmeticExprExample:
col("price").ceil()abs()
Absolute value.
abs(): ArithmeticExprExample:
col("change").abs()sqrt()
Square root.
sqrt(): ArithmeticExprExample:
col("value").sqrt()pow()
Power/exponentiation.
pow(exp: number): ArithmeticExprExample:
col("value").pow(2) // Square
col("value").pow(0.5) // Square rootConditional Operations
when()
Conditional expression builder.
when(condition: Expr, value: Expr | unknown): WhenExprExample:
import { when, col, lit } from "molniya";
df.withColumn("grade",
when(col("score").gte(90), "A")
.when(col("score").gte(80), "B")
.when(col("score").gte(70), "C")
.otherwise("F")
)coalesce()
Return first non-null value.
coalesce(...exprs: Expr[]): ExprExample:
import { coalesce, col } from "molniya";
df.withColumn("contact",
coalesce(col("email"), col("phone"), col("address"))
)String Operations
concat()
Concatenate strings.
concat(...exprs: Expr[]): StringExprExample:
import { concat, col, lit } from "molniya";
df.withColumn("full_name",
concat(col("first"), lit(" "), col("last"))
)length()
String length.
length(col: ColumnRef): ExprExample:
import { length, col } from "molniya";
df.withColumn("name_length", length(col("name")))substring()
Extract substring.
substring(start: number, length: number): StringExprExample:
col("phone").substring(0, 3) // Area codeupper() / lower()
Case conversion.
upper(): StringExpr
lower(): StringExprExample:
col("code").upper()
col("email").lower()Date Operations
year() / month() / day()
Extract date components.
year(col: ColumnRef): Expr
month(col: ColumnRef): Expr
day(col: ColumnRef): ExprExample:
import { year, month, day, col } from "molniya";
df.withColumn("year", year(col("date")))
.withColumn("month", month(col("date")))
.withColumn("day", day(col("date")))addDays() / subDays()
Date arithmetic.
addDays(days: number): DateExpr
subDays(days: number): DateExprExample:
col("date").addDays(7)
col("date").subDays(30)diffDays()
Difference between dates.
diffDays(other: ColumnRef): ExprExample:
col("end").diffDays(col("start"))Type Casting
cast()
Cast to different type.
cast(dtype: DType): ExprExample:
import { DType } from "molniya";
col("id").cast(DType.int32)
col("price").cast(DType.float64)Chaining Operations
Operations can be chained for complex transformations:
df.withColumn("total",
col("price")
.mul(col("quantity")) // price * quantity
.mul(lit(1).sub(col("discount"))) // apply discount
.mul(1.08) // add tax
.round(2) // round to 2 decimals
)