Motivation
Abstract out parametrized (via custom function parameters) chainable (preferably via the DataFrame.prototype
) Polars expressions to provide user-defined, higher-level, reusable and chainable data analysis functions on the DataFrame
Desired behavior and failed intent
import pl from "nodejs-polars"
const { DataFrame, col } = pl
// user-defined, higher-level, reusable and chainable data analysis function
// with arbitrary complex parametrized Polars expressions
DataFrame.prototype.inc = function inc(column, x = 1, alias = `${column}Inc`) {
return this.withColumn(col(column).add(x).alias(alias))
}
const df = new DataFrame({ a: [1, 2, 3] })
// works, but implies code duplication on reuse
console.log(df.withColumn(col("a").add(1).alias("aInc")))
// desiged behavior gives TypeError: df.inc is not a function
console.log(df.inc("a").inc("a", 2, "aInc2"))
What it the recommended way to define custom functions that encapsulate Polars expressions in nodejs-polars
?