pola-rs / nodejs-polars

nodejs front-end of polars

Home Page:https://pola-rs.github.io/nodejs-polars/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generic signature for dataframes and series

scarf005 opened this issue · comments

Motivation

  • The type DataFrame and Series in nodejs-polars lack generic type signature.
  • Being able to know dataframe's structure in the type signature improves productivity and safety.
  • This can be achieved using typescript's powerful type system. (e.g: Pick, Omit)

Feature Request

  • Add generic type parameter for DataFrame and Series type
  • Defaults to any for type checker performance
  • Describe types using string representation of DataType
  • (Later) make methods of DataFrame and Series dependently typed.

As-is

import pl from "npm:nodejs-polars"

const df = pl.DataFrame({
	fruit: ["Apples", "Oranges"],
	comparability: [0, 1],
})
// df: pl.DataFrame

const obj = df.toObject()
// obj: Record<string, any[]>

const fruit = df.getColumn('fruit')
// fruit: pl.Series

To-be

import pl from "npm:nodejs-polars"

const df = pl.DataFrame({
	fruit: ["Apples", "Oranges"],
	comparability: [0, 1],
})
// df: pl.DataFrame<{ fruit: pl.Series<"Utf8">, comparability: pl.Series<"Float64"> }>

const obj = df.toObject()
// obj: { fruit: string[], comparability: number[] }

const fruit = df.getColumn('fruit')
// fruit: pl.Series<"Float64">

Additional notes

I'm interested in opening a PR.

See also

@scarf005 Feel free to put a PR in, if you need help please reach out on Discord. Thx

I think this is an interesting idea, and one that I have considered as well.

There are some considerations to take into account though. the python & rust implementations both use an untyped Series and DataFrame, so this does cause some deviation from the other implementations, but that may be okay. I'd be happy to review a POC & see what it looks like though!