node.jsnodejs-polars

Abstract out Polars expressions with user-defined chainable functions on the `DataFrame`


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?


Solution

  • a functional approach that does not require additional libraries would be to create a simple wrapper function & reexport polars with an overridden DataFrame method within your own package.

    // polars_extension.js
    import pl from 'nodejs-polars'
    
    const customMethods = {
      sumAlias() {
        return this.sum();
      },
    };
    
    export default {
      ...pl,
      DataFrame(...args) {
        return Object.assign(pl.DataFrame(...args), customMethods);
      }
    }
    
    // another_file.js
    import pl from './polars_extension'
    
    pl.DataFrame({num: [1, 2, 3]}).sumAlias()