69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import pl, { DataFrame } from 'nodejs-polars'
|
|
import fs, { writeFile } from 'fs'
|
|
|
|
export function test_x(params: any) {
|
|
const fooSeries = pl.Series("foo", [1, 2, 3])
|
|
|
|
let res = fooSeries.sum()
|
|
|
|
console.log(res)
|
|
|
|
let df = pl.readCSV("data/iris.csv")
|
|
}
|
|
|
|
interface CSVDatabase2 {
|
|
df: DataFrame
|
|
filepath: string
|
|
}
|
|
|
|
export function create_csv_database(filepath: string): CSVDatabase2 {
|
|
// let df = DataFrame([], { columns: ["key", "value"] })
|
|
let df = DataFrame({ 'a_key': ['1'], 'a_value': ['2'] })
|
|
|
|
// writeFile(filepath, 'key,value\nheader,next', () => { })
|
|
// let df = pl.readCSV(filepath)
|
|
df.writeCSV(filepath)
|
|
|
|
return { df, filepath }
|
|
}
|
|
|
|
export function save_db(db: CSVDatabase2) {
|
|
db.df.writeCSV(db.filepath)
|
|
}
|
|
|
|
export function add_row(db: CSVDatabase2, row: any) {
|
|
let d = DataFrame(row)
|
|
db.df = db.df.vstack(d)
|
|
}
|
|
|
|
export function print_head(db: CSVDatabase2) {
|
|
console.log(db.df.head())
|
|
}
|
|
|
|
// can filter everything that is not the row, and do to db
|
|
|
|
export function delete_row_by_key__({ df, filepath }: CSVDatabase2, key: string) {
|
|
// db.df.drop(pl.col("key").eq(key))
|
|
// df = df.select((pl.col("key") != key).alias("nrs > 1"))
|
|
// db.df = db.df.filter(pl.col("key").neq(key))
|
|
}
|
|
|
|
export function delete_row_by_key(db: CSVDatabase2, key: string) {
|
|
console.log("NEXT COLUMNS")
|
|
console.log(db.df.columns)
|
|
db.df = db.df.filter(pl.col('a_key').neq(pl.lit(key)))
|
|
}
|
|
|
|
export function select_columns(db: CSVDatabase2, columns: string[]): DataFrame {
|
|
return db.df.select(...columns)
|
|
}
|
|
|
|
export function join_dataframes(db1: CSVDatabase2, db2: CSVDatabase2, on: string): DataFrame {
|
|
return db1.df.join(db2.df, { on, how: "inner" }) // or "left", "right", "outer"
|
|
}
|
|
|
|
export function add_simple_index(db: CSVDatabase2) {
|
|
const indices = Array.from(Array(db.df.height).keys()) // Create 0-N index
|
|
db.df = db.df.withColumn(pl.Series("index", indices))
|
|
}
|