nodejs-polars
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -11,2 +11,5 @@ /// <reference types="node" /> | ||
export interface DataFrame { | ||
(column: string): Series<any>; | ||
(column: string, ...columns: string[]): DataFrame; | ||
(row: number): any[]; | ||
_df: JsDataFrame; | ||
@@ -22,2 +25,3 @@ dtypes: DataType[]; | ||
[inspect](): string; | ||
[Symbol.iterator](): Generator<any, void, any>; | ||
inner(): JsDataFrame; | ||
@@ -803,2 +807,3 @@ /** | ||
sample(n?: number, frac?: number, withReplacement?: boolean): DataFrame; | ||
schema(): Record<string, string>; | ||
/** | ||
@@ -1086,5 +1091,12 @@ * Select columns from this DataFrame. | ||
toJS(): object; | ||
toJS(options: { | ||
orient: "row" | "col" | "literal"; | ||
}): object; | ||
toJSON(): string; | ||
toJSON(dest: string | Stream): void; | ||
toJSON(dest?: string | Stream): void | string; | ||
toJSON(options: { | ||
orient: "row" | "col" | "literal"; | ||
}): string; | ||
toJSON(dest: string | Stream, options?: { | ||
orient: "row" | "col" | "literal"; | ||
}): void; | ||
toSeries(index: number): Series<any>; | ||
@@ -1271,3 +1283,3 @@ toString(): string; | ||
export declare function DataFrame(): DataFrame; | ||
export declare function DataFrame(data: Record<string, any[]>): DataFrame; | ||
export declare function DataFrame(data: Record<string, any>): DataFrame; | ||
export declare function DataFrame(data: Record<string, any>[]): DataFrame; | ||
@@ -1274,0 +1286,0 @@ export declare function DataFrame(data: Series<any>[]): DataFrame; |
@@ -38,279 +38,128 @@ "use strict"; | ||
const noArgUnwrap = (method) => () => unwrap(method); | ||
const clone = noArgWrap("clone"); | ||
const drop = (name, ...names) => { | ||
names.unshift(name); | ||
if (!Array.isArray(names[0]) && names.length === 1) { | ||
return wrap("drop", { name: names[0] }); | ||
} | ||
const df = clone(); | ||
names.flat(2).forEach((name) => { | ||
unwrap("drop_in_place", { name }, df._df); | ||
}); | ||
return df; | ||
}; | ||
const describe = () => { | ||
const df = (0, exports.dfWrapper)(_df); | ||
const describeCast = (df) => { | ||
return DataFrame(df.getColumns().map(s => { | ||
if (s.isNumeric() || s.isBoolean()) { | ||
return s.cast(datatypes_1.DataType.Float64); | ||
} | ||
else { | ||
return s; | ||
} | ||
})); | ||
}; | ||
const summary = (0, functions_1.concat)([ | ||
describeCast(df.mean()), | ||
describeCast(df.std()), | ||
describeCast(df.min()), | ||
describeCast(df.max()), | ||
describeCast(df.median()) | ||
]); | ||
summary.insertAtIdx(0, (0, series_1.Series)("describe", ["mean", "std", "min", "max", "median"])); | ||
return summary; | ||
}; | ||
const fold = (fn) => { | ||
const df = (0, exports.dfWrapper)(_df); | ||
if (df.width === 1) { | ||
return df.toSeries(0); | ||
} | ||
return df.getColumns().reduce((acc, curr) => fn(acc, curr)); | ||
}; | ||
const hashRows = (obj, k1, k2, k3) => { | ||
if (obj?.k0 !== undefined) { | ||
return hashRows(obj.k0, obj.k1, obj.k2, obj.k3); | ||
} | ||
return (0, series_1.seriesWrapper)(unwrap("hash_rows", { | ||
k0: obj ?? 0, | ||
k1: k1 ?? 1, | ||
k2: k2 ?? 2, | ||
k3: k3 ?? 3 | ||
})); | ||
}; | ||
const hstack = (columns) => { | ||
if (!Array.isArray(columns)) { | ||
columns = columns.getColumns(); | ||
} | ||
return wrap("hstack", { | ||
columns: columns.map(col => col._series), | ||
in_place: false | ||
}); | ||
}; | ||
const join = (df, options) => { | ||
options = { how: "inner", suffix: "right", ...options }; | ||
const on = (0, utils_1.columnOrColumns)(options.on); | ||
const how = options.how; | ||
const suffix = options.suffix; | ||
let leftOn = (0, utils_1.columnOrColumns)(options.leftOn); | ||
let rightOn = (0, utils_1.columnOrColumns)(options.rightOn); | ||
if (on) { | ||
leftOn = on; | ||
rightOn = on; | ||
} | ||
if ((leftOn && !rightOn) || (rightOn && !leftOn)) { | ||
throw new TypeError("You should pass the column to join on as an argument."); | ||
} | ||
return wrap("join", { | ||
other: df._df, | ||
on, | ||
how, | ||
left_on: leftOn, | ||
right_on: rightOn, | ||
suffix, | ||
}); | ||
}; | ||
const rename = (mapping) => { | ||
const df = (0, exports.dfWrapper)(_df).clone(); | ||
Object.entries(mapping).forEach(([key, value]) => { | ||
unwrap("rename", { column: key, new_col: value }, df._df); | ||
}); | ||
return df; | ||
}; | ||
const sample = (opts, frac, withReplacement = false) => { | ||
if (opts?.n !== undefined || opts?.frac !== undefined) { | ||
return sample(opts.n, opts.frac, opts.withReplacement); | ||
} | ||
if (typeof opts === "number") { | ||
return wrap("sample_n", { | ||
n: opts, | ||
withReplacement | ||
const df = { | ||
_df, | ||
[inspect]() { | ||
return unwrap("as_str"); | ||
}, | ||
*[Symbol.iterator]() { | ||
let start = 0; | ||
let len = this.height; | ||
while (start < len) { | ||
const s = this.toSeries(start); | ||
start++; | ||
yield s; | ||
} | ||
}, | ||
get dtypes() { | ||
return unwrap("dtypes"); | ||
}, | ||
get height() { | ||
return unwrap("height"); | ||
}, | ||
get width() { | ||
return unwrap("width"); | ||
}, | ||
get shape() { | ||
return { height: this.height, width: this.width }; | ||
}, | ||
get columns() { | ||
return unwrap("columns"); | ||
}, | ||
inner: () => _df, | ||
clone: noArgWrap("clone"), | ||
describe() { | ||
const describeCast = (df) => { | ||
return DataFrame(df.getColumns().map(s => { | ||
if (s.isNumeric() || s.isBoolean()) { | ||
return s.cast(datatypes_1.DataType.Float64); | ||
} | ||
else { | ||
return s; | ||
} | ||
})); | ||
}; | ||
const summary = (0, functions_1.concat)([ | ||
describeCast(this.mean()), | ||
describeCast(this.std()), | ||
describeCast(this.min()), | ||
describeCast(this.max()), | ||
describeCast(this.median()) | ||
]); | ||
summary.insertAtIdx(0, (0, series_1.Series)("describe", ["mean", "std", "min", "max", "median"])); | ||
return summary; | ||
}, | ||
downsample: (opt, rule, n) => (0, groupby_1.GroupBy)(_df, opt?.by ?? opt, true, opt?.rule ?? rule, opt?.n ?? n), | ||
drop(name, ...names) { | ||
names.unshift(name); | ||
if (!Array.isArray(names[0]) && names.length === 1) { | ||
return wrap("drop", { name: names[0] }); | ||
} | ||
const df = this.clone(); | ||
names.flat(2).forEach((name) => { | ||
unwrap("drop_in_place", { name }, df._df); | ||
}); | ||
} | ||
if (typeof frac === "number") { | ||
return wrap("sample_frac", { | ||
frac, | ||
withReplacement, | ||
}); | ||
} | ||
else { | ||
throw new TypeError("must specify either 'frac' or 'n'"); | ||
} | ||
}; | ||
const sort = (arg, reverse = false) => { | ||
if (arg?.by !== undefined) { | ||
return sort(arg.by, arg.reverse); | ||
} | ||
if (Array.isArray(arg) || (0, utils_1.isExpr)(arg)) { | ||
return df; | ||
}, | ||
dropNulls(...subset) { | ||
if (subset.length) { | ||
return wrap("drop_nulls", { subset: subset.flat(2) }); | ||
} | ||
else { | ||
return wrap("drop_nulls"); | ||
} | ||
}, | ||
dropDuplicates(opts = true, subset) { | ||
if (opts?.maintainOrder !== undefined) { | ||
return this.dropDuplicates(opts.maintainOrder, opts.subset); | ||
} | ||
if (subset) { | ||
subset = [subset].flat(2); | ||
} | ||
return wrap("drop_duplicates", { maintainOrder: opts.maintainOrder, subset }); | ||
}, | ||
explode(...columns) { | ||
return (0, exports.dfWrapper)(_df).lazy() | ||
.sort(arg, reverse) | ||
.collectSync({ noOptimization: true, stringCache: false }); | ||
} | ||
return wrap("sort", { by: arg, reverse }); | ||
}; | ||
const toCSV = (dest, options) => { | ||
options = { hasHeader: true, sep: ",", ...options }; | ||
if (dest instanceof stream_1.Stream.Writable) { | ||
unwrap("write_csv_stream", { writeStream: dest, ...options }); | ||
} | ||
else if (typeof dest === "string") { | ||
unwrap("write_csv", { path: dest, ...options }); | ||
} | ||
else if (!dest || (dest.constructor.name === "Object" && !dest["dest"])) { | ||
let body = ""; | ||
const writeStream = new stream_1.Stream.Writable({ | ||
write(chunk, _encoding, callback) { | ||
body += chunk; | ||
callback(null); | ||
} | ||
}); | ||
unwrap("write_csv_stream", { writeStream, ...options, ...dest }); | ||
return body; | ||
} | ||
else { | ||
throw new TypeError("unknown destination type, Supported types are 'string' and 'Stream.Writeable'"); | ||
} | ||
}; | ||
const toJSON = (dest) => { | ||
if (dest instanceof stream_1.Stream.Writable) { | ||
unwrap("write_json_stream", { writeStream: dest }); | ||
} | ||
else if (typeof dest === "string") { | ||
// eslint-disable-next-line no-undef | ||
unwrap("write_json", { path: dest }); | ||
} | ||
else if (!dest) { | ||
let body = ""; | ||
const writeStream = new stream_1.Stream.Writable({ | ||
write(chunk, _encoding, callback) { | ||
body += chunk; | ||
callback(null); | ||
} | ||
}); | ||
unwrap("write_json_stream", { writeStream }); | ||
return body; | ||
} | ||
else { | ||
throw new TypeError("unknown destination type, Supported types are 'string' and 'Stream.Writeable'"); | ||
} | ||
}; | ||
const withColumn = (column) => { | ||
if ((0, utils_1.isSeries)(column)) { | ||
return wrap("with_column", { _series: column._series }); | ||
} | ||
else { | ||
return (0, exports.dfWrapper)(_df).withColumns(column); | ||
} | ||
}; | ||
const withColumns = (column, ...columns) => { | ||
columns.unshift(column); | ||
if ((0, utils_1.isSeriesArray)(columns)) { | ||
return columns.reduce((acc, curr) => acc.withColumn(curr), (0, exports.dfWrapper)(_df)); | ||
} | ||
else { | ||
return (0, exports.dfWrapper)(_df) | ||
.explode(columns) | ||
.collectSync({ noOptimization: true }); | ||
}, | ||
filter(predicate) { | ||
return this | ||
.lazy() | ||
.withColumns(columns) | ||
.collectSync({ noOptimization: true, stringCache: false }); | ||
} | ||
}; | ||
const select = (firstSelection, ...selection) => { | ||
selection.unshift(firstSelection); | ||
const hasExpr = selection.some(s => (0, utils_1.isExpr)(s)); | ||
if (hasExpr) { | ||
return (0, exports.dfWrapper)(_df) | ||
.lazy() | ||
.select(selection) | ||
.filter(predicate) | ||
.collectSync(); | ||
} | ||
else { | ||
return wrap("select", { selection: (0, utils_1.columnOrColumnsStrict)(selection) }); | ||
} | ||
}; | ||
const shiftAndFill = (periods, fillValue) => { | ||
return (0, exports.dfWrapper)(_df) | ||
.lazy() | ||
.shiftAndFill(periods, fillValue) | ||
.collectSync(); | ||
}; | ||
const shrinkToFit = (inPlace = false) => { | ||
if (inPlace) { | ||
return unwrap("shrink_to_fit"); | ||
} | ||
else { | ||
const d = (0, exports.dfWrapper)(_df).clone(); | ||
unwrap("shrink_to_fit", {}, d._df); | ||
return d; | ||
} | ||
}; | ||
const withColumnRenamed = (opt, replacement) => { | ||
if (opt?.existing !== undefined) { | ||
return withColumnRenamed(opt.existing, opt.replacement); | ||
} | ||
else { | ||
return (0, exports.dfWrapper)(_df).rename({ [opt]: replacement }); | ||
} | ||
}; | ||
const dropNulls = (...subset) => { | ||
if (subset.length) { | ||
return wrap("drop_nulls", { subset: subset.flat(2) }); | ||
} | ||
else { | ||
return wrap("drop_nulls"); | ||
} | ||
}; | ||
const dropDuplicates = (opts = true, subset) => { | ||
if (opts?.maintainOrder !== undefined) { | ||
return dropDuplicates(opts.maintainOrder, opts.subset); | ||
} | ||
if (subset) { | ||
subset = [subset].flat(2); | ||
} | ||
return wrap("drop_duplicates", { maintainOrder: opts.maintainOrder, subset }); | ||
}; | ||
const explode = (...columns) => { | ||
return (0, exports.dfWrapper)(_df).lazy() | ||
.explode(columns) | ||
.collectSync({ noOptimization: true }); | ||
}; | ||
const filter = (predicate) => { | ||
return (0, exports.dfWrapper)(_df) | ||
.lazy() | ||
.filter(predicate) | ||
.collectSync(); | ||
}; | ||
return { | ||
_df, | ||
[inspect]() { return unwrap("as_str"); }, | ||
get dtypes() { return unwrap("dtypes"); }, | ||
get height() { return unwrap("height"); }, | ||
get width() { return unwrap("width"); }, | ||
get shape() { return { height: unwrap("height"), width: unwrap("width") }; }, | ||
get columns() { return unwrap("columns"); }, | ||
inner: () => _df, | ||
clone, | ||
describe, | ||
downsample: (opt, rule, n) => (0, groupby_1.GroupBy)(_df, opt?.by ?? opt, true, opt?.rule ?? rule, opt?.n ?? n), | ||
drop, | ||
dropNulls, | ||
}, | ||
fillNull: (strategy) => wrap("fill_null", { strategy }), | ||
findIdxByName: (name) => unwrap("find_idx_by_name", { name }), | ||
fold, | ||
frameEqual: (other, nullEqual = true) => unwrap("frame_equal", { other: other._df, nullEqual }), | ||
getColumn: (name) => (0, series_1.seriesWrapper)(unwrap("column", { name })), | ||
getColumns: () => unwrap("get_columns").map(s => (0, series_1.seriesWrapper)(s)), | ||
fold(fn) { | ||
if (this.width === 1) { | ||
return this.toSeries(0); | ||
} | ||
return this.getColumns().reduce((acc, curr) => fn(acc, curr)); | ||
}, | ||
frameEqual(other, nullEqual = true) { | ||
return unwrap("frame_equal", { other: other._df, nullEqual }); | ||
}, | ||
getColumn(name) { | ||
return (0, series_1.seriesWrapper)(unwrap("column", { name })); | ||
}, | ||
getColumns() { | ||
return unwrap("get_columns").map(s => (0, series_1.seriesWrapper)(s)); | ||
}, | ||
groupBy: (...by) => (0, groupby_1.GroupBy)(_df, (0, utils_1.columnOrColumnsStrict)(by)), | ||
hashRows, | ||
hashRows(obj = 0, k1 = 1, k2 = 2, k3 = 3) { | ||
if (typeof obj === "number" || typeof obj === "bigint") { | ||
return (0, series_1.seriesWrapper)(unwrap("hash_rows", { k0: obj, k1: k1, k2: k2, k3: k3 })); | ||
} | ||
return (0, series_1.seriesWrapper)(unwrap("hash_rows", { k0: 0, k1, k2, k3, ...obj })); | ||
}, | ||
head: (length = 5) => wrap("head", { length }), | ||
hstack, | ||
hstack(columns) { | ||
if (!Array.isArray(columns)) { | ||
columns = columns.getColumns(); | ||
} | ||
return wrap("hstack", { | ||
columns: columns.map(col => col._series), | ||
in_place: false | ||
}); | ||
}, | ||
insertAtIdx: (index, s) => unwrap("insert_at_idx", { index, new_col: s._series }), | ||
@@ -321,10 +170,56 @@ interpolate: noArgWrap("interpolate"), | ||
isUnique: () => (0, series_1.seriesWrapper)(unwrap("is_unique")), | ||
join, | ||
join(df, options) { | ||
options = { how: "inner", suffix: "right", ...options }; | ||
const on = (0, utils_1.columnOrColumns)(options.on); | ||
const how = options.how; | ||
const suffix = options.suffix; | ||
let leftOn = (0, utils_1.columnOrColumns)(options.leftOn); | ||
let rightOn = (0, utils_1.columnOrColumns)(options.rightOn); | ||
if (on) { | ||
leftOn = on; | ||
rightOn = on; | ||
} | ||
if ((leftOn && !rightOn) || (rightOn && !leftOn)) { | ||
throw new TypeError("You should pass the column to join on as an argument."); | ||
} | ||
return wrap("join", { | ||
other: df._df, | ||
on, | ||
how, | ||
left_on: leftOn, | ||
right_on: rightOn, | ||
suffix, | ||
}); | ||
}, | ||
lazy: () => (0, dataframe_1.LazyDataFrame)(unwrap("lazy")), | ||
limit: (length = 5) => wrap("head", { length }), | ||
max: (axis = 0) => axis === 0 ? wrap("max") : (0, series_1.seriesWrapper)(unwrap("hmax")), | ||
mean: (axis = 0, nullStrategy = "ignore") => axis === 0 ? wrap("mean") : (0, series_1.seriesWrapper)(unwrap("hmean", { nullStrategy })), | ||
max(axis = 0) { | ||
if (axis === 1) { | ||
return (0, series_1.seriesWrapper)(unwrap("hmax")); | ||
} | ||
else { | ||
return wrap("max"); | ||
} | ||
}, | ||
mean(axis = 0, nullStrategy = "ignore") { | ||
if (axis === 1) { | ||
return (0, series_1.seriesWrapper)(unwrap("hmean", { nullStrategy })); | ||
} | ||
return wrap("mean"); | ||
}, | ||
median: noArgWrap("median"), | ||
melt: (ids, values) => wrap("melt", { idVars: (0, utils_1.columnOrColumns)(ids), valueVars: (0, utils_1.columnOrColumns)(values) }), | ||
min: (axis = 0) => axis === 0 ? wrap("min") : (0, series_1.seriesWrapper)(unwrap("hmin")), | ||
melt(ids, values) { | ||
return wrap("melt", { | ||
idVars: (0, utils_1.columnOrColumns)(ids), | ||
valueVars: (0, utils_1.columnOrColumns)(values) | ||
}); | ||
}, | ||
min(axis = 0) { | ||
if (axis === 1) { | ||
return (0, series_1.seriesWrapper)(unwrap("hmin")); | ||
} | ||
else { | ||
return wrap("min"); | ||
} | ||
}, | ||
nChunks: noArgUnwrap("n_chunks"), | ||
@@ -334,17 +229,129 @@ nullCount: noArgWrap("null_count"), | ||
rechunk: noArgWrap("rechunk"), | ||
rename, | ||
replaceAtIdx: (index, newColumn) => unwrap("replace_at_idx", { index, newColumn: newColumn._series }), | ||
rename(mapping) { | ||
const df = this.clone(); | ||
Object.entries(mapping).forEach(([column, new_col]) => { | ||
unwrap("rename", { column, new_col }, df._df); | ||
}); | ||
return df; | ||
}, | ||
replaceAtIdx(index, newColumn) { | ||
unwrap("replace_at_idx", { | ||
index, | ||
newColumn: newColumn._series | ||
}); | ||
return this; | ||
}, | ||
rows: noArgUnwrap("to_rows"), | ||
sample, | ||
select, | ||
sample(opts, frac, withReplacement = false) { | ||
if (opts?.n !== undefined || opts?.frac !== undefined) { | ||
return this.sample(opts.n, opts.frac, opts.withReplacement); | ||
} | ||
if (typeof opts === "number") { | ||
return wrap("sample_n", { | ||
n: opts, | ||
withReplacement | ||
}); | ||
} | ||
if (typeof frac === "number") { | ||
return wrap("sample_frac", { | ||
frac, | ||
withReplacement, | ||
}); | ||
} | ||
else { | ||
throw new TypeError("must specify either 'frac' or 'n'"); | ||
} | ||
}, | ||
schema: noArgUnwrap("schema"), | ||
select(...selection) { | ||
const hasExpr = selection.flat().some(s => (0, utils_1.isExpr)(s)); | ||
if (hasExpr) { | ||
return (0, exports.dfWrapper)(_df) | ||
.lazy() | ||
.select(selection) | ||
.collectSync(); | ||
} | ||
else { | ||
return wrap("select", { selection: (0, utils_1.columnOrColumnsStrict)(selection) }); | ||
} | ||
}, | ||
shift: (opt) => wrap("shift", { periods: opt?.periods ?? opt }), | ||
shiftAndFill, | ||
shrinkToFit, | ||
slice: (opts, length) => wrap("slice", { offset: opts?.offset ?? opts, length: opts?.length ?? length }), | ||
sort, | ||
shiftAndFill(periods, fillValue) { | ||
return (0, exports.dfWrapper)(_df) | ||
.lazy() | ||
.shiftAndFill(periods, fillValue) | ||
.collectSync(); | ||
}, | ||
shrinkToFit(inPlace = false) { | ||
if (inPlace) { | ||
unwrap("shrink_to_fit"); | ||
} | ||
else { | ||
const d = this.clone(); | ||
unwrap("shrink_to_fit", {}, d._df); | ||
return d; | ||
} | ||
}, | ||
slice(opts, length) { | ||
if (typeof opts === "number") { | ||
return wrap("slice", { offset: opts, length }); | ||
} | ||
return wrap("slice", opts); | ||
}, | ||
sort(arg, reverse = false) { | ||
if (arg?.by !== undefined) { | ||
return this.sort(arg.by, arg.reverse); | ||
} | ||
if (Array.isArray(arg) || (0, utils_1.isExpr)(arg)) { | ||
return (0, exports.dfWrapper)(_df).lazy() | ||
.sort(arg, reverse) | ||
.collectSync({ noOptimization: true, stringCache: false }); | ||
} | ||
return wrap("sort", { by: arg, reverse }); | ||
}, | ||
std: noArgWrap("std"), | ||
sum: (axis = 0, nullStrategy = "ignore") => axis === 0 ? wrap("sum") : (0, series_1.seriesWrapper)(unwrap("hsum", { nullStrategy })), | ||
sum(axis = 0, nullStrategy = "ignore") { | ||
if (axis === 1) { | ||
return (0, series_1.seriesWrapper)(unwrap("hsum", { nullStrategy })); | ||
} | ||
return wrap("sum"); | ||
}, | ||
tail: (length = 5) => wrap("tail", { length }), | ||
toCSV: toCSV, | ||
toJS() { | ||
toCSV(dest, options) { | ||
options = { hasHeader: true, sep: ",", ...options }; | ||
if (dest instanceof stream_1.Stream.Writable) { | ||
unwrap("write_csv_stream", { writeStream: dest, ...options }); | ||
} | ||
else if (typeof dest === "string") { | ||
unwrap("write_csv", { path: dest, ...options }); | ||
} | ||
else if (!dest || (dest.constructor.name === "Object" && !dest["dest"])) { | ||
let body = ""; | ||
const writeStream = new stream_1.Stream.Writable({ | ||
write(chunk, _encoding, callback) { | ||
body += chunk; | ||
callback(null); | ||
} | ||
}); | ||
unwrap("write_csv_stream", { writeStream, ...options, ...dest }); | ||
return body; | ||
} | ||
else { | ||
throw new TypeError("unknown destination type, Supported types are 'string' and 'Stream.Writeable'"); | ||
} | ||
}, | ||
toJS(options) { | ||
if (options?.orient === "row") { | ||
const columns = this.columns; | ||
const rows = this.rows(); | ||
return rows.map(row => { | ||
return row.reduce((acc, curr, currIdx) => ({ | ||
[columns[currIdx]]: curr, | ||
...acc | ||
})); | ||
}); | ||
} | ||
if (options?.orient === "literal") { | ||
return unwrap("to_js"); | ||
} | ||
return unwrap("get_columns").reduce((acc, curr) => { | ||
@@ -358,5 +365,32 @@ const s = (0, series_1.seriesWrapper)(curr); | ||
}, | ||
toJSON: toJSON, | ||
toJSON(arg0, options) { | ||
if (arg0 === "") { | ||
return this.toJS({ orient: "literal", ...options }); | ||
} | ||
return this.__toJSON(arg0); | ||
}, | ||
__toJSON(dest) { | ||
if (dest instanceof stream_1.Stream.Writable) { | ||
unwrap("write_json_stream", { writeStream: dest }); | ||
} | ||
else if (typeof dest === "string" && dest.length) { | ||
unwrap("write_json", { path: dest }); | ||
} | ||
else if (!dest) { | ||
let body = ""; | ||
const writeStream = new stream_1.Stream.Writable({ | ||
write(chunk, _encoding, callback) { | ||
body += chunk; | ||
callback(null); | ||
} | ||
}); | ||
unwrap("write_json_stream", { writeStream }); | ||
return body; | ||
} | ||
else { | ||
throw new TypeError("unknown destination type, Supported types are 'string' and 'Stream.Writeable'"); | ||
} | ||
}, | ||
toSeries: (index) => (0, series_1.seriesWrapper)(unwrap("select_at_idx", { index })), | ||
toString: () => unwrap("as_str"), | ||
toString: noArgUnwrap("as_str"), | ||
add: (other) => wrap("add", { other: prepareOtherArg(other)._series }), | ||
@@ -374,5 +408,2 @@ sub: (other) => wrap("sub", { other: prepareOtherArg(other)._series }), | ||
apply: () => { throw (0, error_1.todo)(); }, | ||
dropDuplicates, | ||
explode, | ||
filter, | ||
map: (fn) => map((0, exports.dfWrapper)(_df), fn), | ||
@@ -383,8 +414,54 @@ pipe: (fn) => { throw (0, error_1.todo)(); }, | ||
vstack: (other) => wrap("vstack", { other: other._df }), | ||
withColumn, | ||
withColumns, | ||
withColumnRenamed, | ||
withColumn(column) { | ||
if ((0, utils_1.isSeries)(column)) { | ||
return wrap("with_column", { _series: column._series }); | ||
} | ||
else { | ||
return this.withColumns(column); | ||
} | ||
}, | ||
withColumns(column, ...columns) { | ||
columns.unshift(column); | ||
if ((0, utils_1.isSeriesArray)(columns)) { | ||
return columns.reduce((acc, curr) => acc.withColumn(curr), (0, exports.dfWrapper)(_df)); | ||
} | ||
else { | ||
return this | ||
.lazy() | ||
.withColumns(columns) | ||
.collectSync({ noOptimization: true, stringCache: false }); | ||
} | ||
}, | ||
withColumnRenamed(opt, replacement) { | ||
if (typeof opt === "string") { | ||
return this.rename({ [opt]: replacement }); | ||
} | ||
else { | ||
return this.rename({ [opt.existing]: opt.replacement }); | ||
} | ||
}, | ||
withRowCount: (name = "row_nr") => wrap("with_row_count", { name }), | ||
where: filter | ||
where(predicate) { | ||
return this.filter(predicate); | ||
} | ||
}; | ||
return new Proxy(df, { | ||
get: function (target, prop, receiver) { | ||
if (typeof prop === "string" && target.columns.includes(prop)) { | ||
return target.getColumn(prop); | ||
} | ||
if (Array.isArray(prop) && target.columns.includes(prop[0])) { | ||
return target.select(prop); | ||
} | ||
if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) { | ||
return target.row(Number(prop)); | ||
} | ||
else { | ||
return Reflect.get(target, prop, receiver); | ||
} | ||
}, | ||
has: function (target, p) { | ||
return target.columns.includes(p); | ||
} | ||
}); | ||
}; | ||
@@ -391,0 +468,0 @@ exports.dfWrapper = dfWrapper; |
@@ -1,2 +0,2 @@ | ||
export declare class InvalidOperationError extends Error { | ||
export declare class InvalidOperationError extends RangeError { | ||
constructor(method: any, dtype: any); | ||
@@ -3,0 +3,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.todo = exports.NotImplemented = exports.InvalidOperationError = void 0; | ||
class InvalidOperationError extends Error { | ||
class InvalidOperationError extends RangeError { | ||
constructor(method, dtype) { | ||
@@ -6,0 +6,0 @@ super(`Invalid operation: ${method} is not supported for ${dtype}`); |
@@ -65,4 +65,2 @@ "use strict"; | ||
pl.lit = lazy_1.funcs.lit; | ||
// export import max = lazy.max | ||
// export import fold = lazy.fold | ||
pl.arange = lazy_1.funcs.arange; | ||
@@ -69,0 +67,0 @@ pl.argSortBy = lazy_1.funcs.argSortBy; |
@@ -1,2 +0,2 @@ | ||
declare const _default: any; | ||
export default _default; | ||
declare const bindings: any; | ||
export default bindings; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const up1 = (0, path_1.join)(__dirname, "../"); | ||
exports.default = (0, helper_1.loadBinding)(up1, "nodejs-polars", "nodejs-polars"); | ||
const bindings = (0, helper_1.loadBinding)(up1, "nodejs-polars", "nodejs-polars"); | ||
exports.default = bindings; |
@@ -6,3 +6,2 @@ import { DataFrame } from "../dataframe"; | ||
declare type JsLazyFrame = any; | ||
export declare type Option<T> = T | undefined; | ||
declare type LazyJoinOptions = { | ||
@@ -26,2 +25,3 @@ how?: "left" | "inner" | "outer" | "cross"; | ||
export interface LazyDataFrame { | ||
_ldf: any; | ||
get columns(): string[]; | ||
@@ -139,3 +139,3 @@ /** | ||
*/ | ||
first(): LazyDataFrame; | ||
first(): DataFrame; | ||
/** | ||
@@ -156,10 +156,2 @@ * Start a groupby operation. | ||
/** | ||
* Prints the value that this node in the computation graph evaluates to and passes on the value. | ||
*/ | ||
inspect(): LazyDataFrame; | ||
/** | ||
* Interpolate intermediate values. The interpolation method is linear. | ||
*/ | ||
interpolate(): any; | ||
/** | ||
* Add a join operation to the Logical Plan. | ||
@@ -166,0 +158,0 @@ */ |
@@ -24,133 +24,7 @@ "use strict"; | ||
}; | ||
const dropNulls = (...subset) => { | ||
if (subset.length) { | ||
return wrap("dropNulls", { subset: subset.flat(2) }); | ||
} | ||
else { | ||
return wrap("dropNulls"); | ||
} | ||
}; | ||
const dropDuplicates = (opts = true, subset) => { | ||
if (opts?.maintainOrder !== undefined) { | ||
return dropDuplicates(opts.maintainOrder, opts.subset); | ||
} | ||
if (subset) { | ||
subset = [subset].flat(2); | ||
} | ||
return wrap("dropDuplicates", { maintainOrder: opts.maintainOrder, subset }); | ||
}; | ||
const explode = (...columns) => { | ||
if (!columns.length) { | ||
const cols = (0, utils_1.selectionToExprList)((0, exports.LazyDataFrame)(ldf).columns, false); | ||
return wrap("explode", { column: cols }); | ||
} | ||
const column = (0, utils_1.selectionToExprList)(columns, false); | ||
return wrap("explode", { column }); | ||
}; | ||
const fetch = (numRows, opts) => { | ||
if (opts?.noOptimization) { | ||
opts.predicatePushdown = false; | ||
opts.projectionPushdown = false; | ||
} | ||
if (opts) { | ||
const _ldf = unwrap("optimizationToggle", opts); | ||
return (0, dataframe_1.dfWrapper)(unwrap("fetchSync", { numRows }, _ldf)); | ||
} | ||
return (0, dataframe_1.dfWrapper)(unwrap("fetchSync", { numRows })); | ||
}; | ||
const fillNull = (exprOrValue) => { | ||
const fillValue = (0, expr_1.exprToLitOrExpr)(exprOrValue)._expr; | ||
return wrap("fillNull", { fillValue }); | ||
}; | ||
const filter = (exprOrValue) => { | ||
const predicate = (0, expr_1.exprToLitOrExpr)(exprOrValue, false)._expr; | ||
return wrap("filter", { predicate }); | ||
}; | ||
const groupBy = (opt, maintainOrder = true) => { | ||
if (opt?.by !== undefined) { | ||
return groupBy(opt.by, opt.maintainOrder); | ||
} | ||
return (0, groupby_1.LazyGroupBy)(ldf, opt, maintainOrder); | ||
}; | ||
const join = (df, options) => { | ||
options = { | ||
how: "inner", | ||
suffix: "right", | ||
allowParallel: true, | ||
forceParallel: false, | ||
...options | ||
}; | ||
const { how, suffix, allowParallel, forceParallel } = options; | ||
let leftOn; | ||
let rightOn; | ||
if (options.on) { | ||
const on = (0, utils_1.selectionToExprList)(options.on, false); | ||
leftOn = on; | ||
rightOn = on; | ||
} | ||
else if ((options.leftOn && !options.rightOn) || (options.rightOn && !options.leftOn)) { | ||
throw new TypeError("You should pass the column to join on as an argument."); | ||
} | ||
else { | ||
leftOn = (0, utils_1.selectionToExprList)(options.leftOn, false); | ||
rightOn = (0, utils_1.selectionToExprList)(options.rightOn, false); | ||
} | ||
return wrap("join", { | ||
other: df._ldf, | ||
how, | ||
leftOn, | ||
rightOn, | ||
suffix, | ||
allowParallel, | ||
forceParallel | ||
}); | ||
}; | ||
const rename = (mapping) => { | ||
const existing = Object.keys(mapping); | ||
const replacements = Object.values(mapping); | ||
return wrap("rename", { existing, replacements }); | ||
}; | ||
const select = (...exprs) => { | ||
const predicate = (0, utils_1.selectionToExprList)(exprs, false); | ||
return wrap("select", { predicate }); | ||
}; | ||
const shiftAndFill = (optOrPeriods, fillValue) => { | ||
if (optOrPeriods?.periods) { | ||
return shiftAndFill(optOrPeriods.periods, optOrPeriods.fillValue); | ||
} | ||
else { | ||
const periods = optOrPeriods; | ||
fillValue = (0, expr_1.exprToLitOrExpr)(fillValue)._expr; | ||
return wrap("shiftAndFill", { periods, fillValue }); | ||
} | ||
}; | ||
const slice = (opt, len) => { | ||
if (opt?.offset !== undefined) { | ||
return slice(opt.offset, opt.length); | ||
} | ||
return wrap("slice", { offset: opt, len }); | ||
}; | ||
const sort = (arg, reverse = false) => { | ||
if (arg?.by !== undefined) { | ||
return sort(arg.by, arg.reverse); | ||
} | ||
if (typeof arg === "string") { | ||
return wrap("sort", { by: arg, reverse }); | ||
} | ||
else { | ||
reverse = [reverse].flat(3); | ||
const by = (0, utils_1.selectionToExprList)(arg, false); | ||
return wrap("sort_by_exprs", { by, reverse }); | ||
} | ||
}; | ||
const withColumn = (expr) => { | ||
return wrap("withColumns", { expr }); | ||
}; | ||
const withColumns = (...columns) => { | ||
const exprs = (0, utils_1.selectionToExprList)(columns, false); | ||
return wrap("withColumns", { exprs }); | ||
}; | ||
return { | ||
_ldf: ldf, | ||
get columns() { return unwrap("columns"); }, | ||
get columns() { | ||
return unwrap("columns"); | ||
}, | ||
describePlan: () => unwrap("describePlan"), | ||
@@ -162,11 +36,89 @@ describeOptimizedPlan: withOptimizationToggle("describeOptimizedPlan"), | ||
drop: (...cols) => wrap("dropColumns", { cols: cols.flat(2) }), | ||
dropDuplicates, | ||
dropNulls, | ||
explode, | ||
fetch, | ||
fillNull, | ||
filter, | ||
groupBy, | ||
dropDuplicates(opts = true, subset) { | ||
if (opts?.maintainOrder !== undefined) { | ||
return this.dropDuplicates(opts.maintainOrder, opts.subset); | ||
} | ||
if (subset) { | ||
subset = [subset].flat(2); | ||
} | ||
return wrap("dropDuplicates", { maintainOrder: opts.maintainOrder, subset }); | ||
}, | ||
dropNulls(...subset) { | ||
if (subset.length) { | ||
return wrap("dropNulls", { subset: subset.flat(2) }); | ||
} | ||
else { | ||
return wrap("dropNulls"); | ||
} | ||
}, | ||
explode(...columns) { | ||
if (!columns.length) { | ||
const cols = (0, utils_1.selectionToExprList)((0, exports.LazyDataFrame)(ldf).columns, false); | ||
return wrap("explode", { column: cols }); | ||
} | ||
const column = (0, utils_1.selectionToExprList)(columns, false); | ||
return wrap("explode", { column }); | ||
}, | ||
fetch(numRows, opts) { | ||
if (opts?.noOptimization) { | ||
opts.predicatePushdown = false; | ||
opts.projectionPushdown = false; | ||
} | ||
if (opts) { | ||
const _ldf = unwrap("optimizationToggle", opts); | ||
return (0, dataframe_1.dfWrapper)(unwrap("fetchSync", { numRows }, _ldf)); | ||
} | ||
return (0, dataframe_1.dfWrapper)(unwrap("fetchSync", { numRows })); | ||
}, | ||
first() { | ||
return this.fetch(1); | ||
}, | ||
fillNull(exprOrValue) { | ||
const fillValue = (0, expr_1.exprToLitOrExpr)(exprOrValue)._expr; | ||
return wrap("fillNull", { fillValue }); | ||
}, | ||
filter(exprOrValue) { | ||
const predicate = (0, expr_1.exprToLitOrExpr)(exprOrValue, false)._expr; | ||
return wrap("filter", { predicate }); | ||
}, | ||
groupBy(opt, maintainOrder = true) { | ||
if (opt?.by !== undefined) { | ||
return (0, groupby_1.LazyGroupBy)(ldf, opt.by, opt.maintainOrder); | ||
} | ||
return (0, groupby_1.LazyGroupBy)(ldf, opt, maintainOrder); | ||
}, | ||
head: (len = 5) => wrap("slice", { offset: 0, len }), | ||
join, | ||
join(df, options) { | ||
options = { | ||
how: "inner", | ||
suffix: "right", | ||
allowParallel: true, | ||
forceParallel: false, | ||
...options | ||
}; | ||
const { how, suffix, allowParallel, forceParallel } = options; | ||
let leftOn; | ||
let rightOn; | ||
if (options.on) { | ||
const on = (0, utils_1.selectionToExprList)(options.on, false); | ||
leftOn = on; | ||
rightOn = on; | ||
} | ||
else if ((options.leftOn && !options.rightOn) || (options.rightOn && !options.leftOn)) { | ||
throw new TypeError("You should pass the column to join on as an argument."); | ||
} | ||
else { | ||
leftOn = (0, utils_1.selectionToExprList)(options.leftOn, false); | ||
rightOn = (0, utils_1.selectionToExprList)(options.rightOn, false); | ||
} | ||
return wrap("join", { | ||
other: df._ldf, | ||
how, | ||
leftOn, | ||
rightOn, | ||
suffix, | ||
allowParallel, | ||
forceParallel | ||
}); | ||
}, | ||
last: () => wrap("tail", { length: 1 }), | ||
@@ -177,12 +129,51 @@ limit: (len = 5) => wrap("slice", { offset: 0, len }), | ||
median: wrapNullArgs("median"), | ||
melt: (ids, values) => wrap("melt", { idVars: (0, utils_1.columnOrColumnsStrict)(ids), valueVars: (0, utils_1.columnOrColumnsStrict)(values) }), | ||
melt(ids, values) { | ||
return wrap("melt", { | ||
idVars: (0, utils_1.columnOrColumnsStrict)(ids), | ||
valueVars: (0, utils_1.columnOrColumnsStrict)(values) | ||
}); | ||
}, | ||
min: wrapNullArgs("min"), | ||
quantile: (quantile) => wrap("quantile", { quantile }), | ||
rename, | ||
rename(mapping) { | ||
const existing = Object.keys(mapping); | ||
const replacements = Object.values(mapping); | ||
return wrap("rename", { existing, replacements }); | ||
}, | ||
reverse: wrapNullArgs("reverse"), | ||
select, | ||
select(...exprs) { | ||
const predicate = (0, utils_1.selectionToExprList)(exprs, false); | ||
return wrap("select", { predicate }); | ||
}, | ||
shift: (periods) => wrap("shift", { periods }), | ||
shiftAndFill, | ||
slice, | ||
sort, | ||
shiftAndFill(optOrPeriods, fillValue) { | ||
if (typeof optOrPeriods === "number") { | ||
fillValue = (0, expr_1.exprToLitOrExpr)(fillValue)._expr; | ||
return wrap("shiftAndFill", { periods: optOrPeriods, fillValue }); | ||
} | ||
else { | ||
fillValue = (0, expr_1.exprToLitOrExpr)(optOrPeriods.fillValue)._expr; | ||
const periods = optOrPeriods.periods; | ||
return wrap("shiftAndFill", { periods, fillValue }); | ||
} | ||
}, | ||
slice(opt, len) { | ||
if (opt?.offset !== undefined) { | ||
return wrap("slice", { offset: opt.offset, len: opt.length }); | ||
} | ||
return wrap("slice", { offset: opt, len }); | ||
}, | ||
sort(arg, reverse = false) { | ||
if (arg?.by !== undefined) { | ||
return this.sort(arg.by, arg.reverse); | ||
} | ||
if (typeof arg === "string") { | ||
return wrap("sort", { by: arg, reverse }); | ||
} | ||
else { | ||
reverse = [reverse].flat(3); | ||
const by = (0, utils_1.selectionToExprList)(arg, false); | ||
return wrap("sort_by_exprs", { by, reverse }); | ||
} | ||
}, | ||
std: wrapNullArgs("std"), | ||
@@ -193,3 +184,6 @@ sum: wrapNullArgs("sum"), | ||
withColumn: (expr) => wrap("withColumn", { expr: expr._expr }), | ||
withColumns, | ||
withColumns(...columns) { | ||
const exprs = (0, utils_1.selectionToExprList)(columns, false); | ||
return wrap("withColumns", { exprs }); | ||
}, | ||
withColumnRenamed: (existing, replacement) => wrap("withColumnRenamed", { existing, replacement }), | ||
@@ -196,0 +190,0 @@ withRowCount: (name = "row_nr") => wrap("withRowCount", { name }), |
@@ -521,5 +521,5 @@ import { DataType } from "../datatypes"; | ||
* >>> df | ||
* ... .groupby("a") | ||
* ... .groupBy("a") | ||
* ... .agg(pl.col("b").list()) | ||
* ... .sort(by="a") | ||
* ... .sort({by:"a"}) | ||
* | ||
@@ -526,0 +526,0 @@ * shape: (3, 2) |
@@ -14,25 +14,28 @@ "use strict"; | ||
const unwrap = (args) => (0, dataframe_1.LazyDataFrame)(polars_internal_1.default.ldf.groupby(args)); | ||
const agg = (...aggs) => unwrap({ | ||
aggs: aggs.map(a => a._expr), | ||
aggMethod: "agg", | ||
...baseArgs | ||
}); | ||
const head = (n = 5) => unwrap({ | ||
n, | ||
aggs: [], | ||
aggMethod: "head", | ||
...baseArgs | ||
}); | ||
const tail = (n = 5) => unwrap({ | ||
n, | ||
aggs: [], | ||
aggMethod: "tail", | ||
...baseArgs | ||
}); | ||
return { | ||
agg, | ||
head, | ||
tail | ||
agg(...aggs) { | ||
return unwrap({ | ||
aggs: aggs.map(a => a._expr), | ||
aggMethod: "agg", | ||
...baseArgs | ||
}); | ||
}, | ||
head(n = 5) { | ||
return unwrap({ | ||
n, | ||
aggs: [], | ||
aggMethod: "head", | ||
...baseArgs | ||
}); | ||
}, | ||
tail(n = 5) { | ||
return unwrap({ | ||
n, | ||
aggs: [], | ||
aggMethod: "tail", | ||
...baseArgs | ||
}); | ||
} | ||
}; | ||
}; | ||
exports.LazyGroupBy = LazyGroupBy; |
@@ -133,2 +133,10 @@ import { DataType, DtypeToPrimitive, Optional } from "./datatypes"; | ||
/** | ||
* __Rename this Series.__ | ||
* | ||
* @param name - new name | ||
* @see {@link rename} {@link alias} | ||
* | ||
*/ | ||
as(name: string): Series<T>; | ||
/** | ||
* Cast between data types. | ||
@@ -1102,7 +1110,7 @@ */ | ||
* | ||
* @param start - Start of the slice (negative indexing may be used). | ||
* @param offset - Start of the slice (negative indexing may be used). | ||
* @param length - length of the slice. | ||
*/ | ||
slice(opt: { | ||
start: number; | ||
offset: number; | ||
length: number; | ||
@@ -1282,3 +1290,7 @@ }): Series<T>; | ||
*/ | ||
toJS(): object; | ||
toJS(): { | ||
name: string; | ||
datatype: string; | ||
values: any[]; | ||
}; | ||
toFrame(): DataFrame; | ||
@@ -1285,0 +1297,0 @@ } |
@@ -41,14 +41,2 @@ "use strict"; | ||
}; | ||
const clone = () => wrap("clone"); | ||
const alias = (name) => { | ||
const s = clone(); | ||
unwrap("rename", { name }, s._series); | ||
return s; | ||
}; | ||
const filter = (opt) => { | ||
if (opt?.predicate) { | ||
return filter(opt.predicate); | ||
} | ||
return wrap("filter", { filter: opt._series }); | ||
}; | ||
const inPlaceOptional = (method) => (obj) => { | ||
@@ -62,19 +50,2 @@ if (obj === true || obj?.["inPlace"] === true) { | ||
}; | ||
const reinterpret = (signed = true) => { | ||
const dtype = unwrap("dtype"); | ||
if ([datatypes_1.DataType.UInt64, datatypes_1.DataType.Int64].includes(datatypes_1.DataType[dtype])) { | ||
return wrap("reinterpret", { signed }); | ||
} | ||
else { | ||
throw new error_1.InvalidOperationError("reinterpret", dtype); | ||
} | ||
}; | ||
const rename = (obj, inPlace = false) => { | ||
if (obj?.inPlace ?? inPlace) { | ||
unwrap("rename", { name: obj?.name ?? obj }); | ||
} | ||
else { | ||
return alias(obj?.name ?? obj); | ||
} | ||
}; | ||
const rolling = (method) => (opts, weights, minPeriods, center) => { | ||
@@ -93,55 +64,3 @@ const windowSize = opts?.["windowSize"] ?? (typeof opts === "number" ? opts : null); | ||
}; | ||
const sample = (opts, frac, withReplacement = false) => { | ||
if (opts?.n || typeof opts === "number") { | ||
return wrap("sample_n", { | ||
n: opts?.n ?? opts, | ||
withReplacement: opts?.withReplacement ?? withReplacement | ||
}); | ||
} | ||
if (opts?.frac ?? frac) { | ||
return wrap("sample_frac", { | ||
frac: opts?.frac ?? frac, | ||
withReplacement: withReplacement, | ||
}); | ||
} | ||
else { | ||
throw new Error("must specify either 'frac' or 'n'"); | ||
} | ||
}; | ||
const seriesEqual = (other, opt = true) => unwrap("series_equal", { | ||
other: other._series, | ||
null_equal: opt?.nullEqual ?? opt | ||
}); | ||
const skew = (opt = true) => { | ||
return unwrap("skew", { | ||
bias: opt?.bias ?? (typeof opt === "boolean" ? opt : true) | ||
}); | ||
}; | ||
const shiftAndFill = (opt, fillValue) => { | ||
const s = (0, exports.seriesWrapper)(_s); | ||
const name = s.name; | ||
return s | ||
.toFrame() | ||
.select((0, lazy_functions_1.col)(name).shiftAndFill(opt, fillValue)) | ||
.getColumn(name); | ||
}; | ||
const set = (filter, value) => { | ||
const dtype = unwrap("dtype"); | ||
const dt = datatypes_1.DTYPE_TO_FFINAME[datatypes_1.DataType[dtype]]; | ||
if (!dt) { | ||
throw (0, error_1.todo)(); | ||
} | ||
return wrap(`set_with_mask_${dt}`, { filter: filter._series, value }); | ||
}; | ||
const setAtIdx = (indices, value) => { | ||
const dtype = unwrap("dtype"); | ||
const dt = datatypes_1.DTYPE_TO_FFINAME[datatypes_1.DataType[dtype]]; | ||
if (!dt) { | ||
throw (0, error_1.todo)(); | ||
} | ||
indices = (0, utils_1.isSeries)(indices) ? indices.cast(datatypes_1.DataType.UInt32).toArray() : indices; | ||
return wrap(`set_at_idx_${dt}`, { indices, value }); | ||
}; | ||
const propOrElse = (obj, key, otherwise) => ({ [key]: obj?.[key] ?? obj ?? otherwise }); | ||
const out = { | ||
const seriesObject = { | ||
_series: _s, | ||
@@ -189,3 +108,3 @@ [inspect]() { | ||
alias(name) { | ||
const s = clone(); | ||
const s = this.clone(); | ||
unwrap("rename", { name }, s._series); | ||
@@ -209,2 +128,5 @@ return s; | ||
argUnique: noArgWrap("arg_unique"), | ||
as(name) { | ||
return this.alias(name); | ||
}, | ||
bitand(other) { | ||
@@ -457,6 +379,21 @@ return wrap("bitand", { other: other._series }); | ||
rechunk: inPlaceOptional("rechunk"), | ||
reinterpret, | ||
reinterpret(signed = true) { | ||
const dtype = unwrap("dtype"); | ||
if ([datatypes_1.DataType.UInt64, datatypes_1.DataType.Int64].includes(datatypes_1.DataType[dtype])) { | ||
return wrap("reinterpret", { signed }); | ||
} | ||
else { | ||
throw new error_1.InvalidOperationError("reinterpret", dtype); | ||
} | ||
}, | ||
rem: (field) => dtypeAccessor(wrap)("rem", { field, key: "other" }), | ||
remainder: (field) => dtypeAccessor(wrap)("rem", { field, key: "other" }), | ||
rename, | ||
rename(obj, inPlace = false) { | ||
if (obj?.inPlace ?? inPlace) { | ||
unwrap("rename", { name: obj?.name ?? obj }); | ||
} | ||
else { | ||
return this.alias(obj?.name ?? obj); | ||
} | ||
}, | ||
rollingMax: rolling("rolling_max"), | ||
@@ -467,13 +404,90 @@ rollingMean: rolling("rolling_mean"), | ||
rollingVar: rolling("rolling_var"), | ||
round: (o) => wrap("round", { decimals: o?.decimals ?? o }), | ||
sample, | ||
seriesEqual, | ||
set, | ||
setAtIdx, | ||
shift: (opt = 1) => wrap("shift", { periods: opt?.periods ?? opt }), | ||
shiftAndFill, | ||
round(opt) { | ||
if (this.isNumeric()) { | ||
if (typeof opt === "number") { | ||
return wrap("round", { decimals: opt }); | ||
} | ||
else { | ||
return wrap("round", opt); | ||
} | ||
} | ||
else { | ||
throw new error_1.InvalidOperationError("round", this.dtype); | ||
} | ||
}, | ||
sample(opts, frac, withReplacement = false) { | ||
if (opts?.n !== undefined || opts?.frac !== undefined) { | ||
return this.sample(opts.n, opts.frac, opts.withReplacement); | ||
} | ||
if (typeof opts === "number") { | ||
return wrap("sample_n", { | ||
n: opts, | ||
withReplacement | ||
}); | ||
} | ||
if (typeof frac === "number") { | ||
return wrap("sample_frac", { | ||
frac, | ||
withReplacement, | ||
}); | ||
} | ||
else { | ||
throw new TypeError("must specify either 'frac' or 'n'"); | ||
} | ||
}, | ||
seriesEqual(other, opt = true) { | ||
return unwrap("series_equal", { | ||
other: other._series, | ||
null_equal: opt?.nullEqual ?? opt | ||
}); | ||
}, | ||
set(filter, value) { | ||
const dtype = this.dtype; | ||
const dt = datatypes_1.DTYPE_TO_FFINAME[datatypes_1.DataType[dtype]]; | ||
if (!dt) { | ||
throw (0, error_1.todo)(); | ||
} | ||
return wrap(`set_with_mask_${dt}`, { filter: filter._series, value }); | ||
}, | ||
setAtIdx(indices, value) { | ||
const dtype = this.dtype; | ||
const dt = datatypes_1.DTYPE_TO_FFINAME[datatypes_1.DataType[dtype]]; | ||
if (!dt) { | ||
throw (0, error_1.todo)(); | ||
} | ||
indices = (0, utils_1.isSeries)(indices) ? indices.cast(datatypes_1.DataType.UInt32).toArray() : indices; | ||
unwrap(`set_at_idx_${dt}`, { indices, value }); | ||
return this; | ||
}, | ||
shift(opt = 1) { | ||
if (typeof opt === "number") { | ||
return wrap("shift", { periods: opt }); | ||
} | ||
return wrap("shift", opt); | ||
}, | ||
shiftAndFill(opt, fillValue) { | ||
return this | ||
.toFrame() | ||
.select((0, lazy_functions_1.col)(this.name).shiftAndFill(opt, fillValue)) | ||
.getColumn(this.name); | ||
}, | ||
shrinkToFit: inPlaceOptional("shrink_to_fit"), | ||
skew, | ||
slice: (opt, length) => wrap("slice", { offset: opt?.start ?? opt, length: opt?.length ?? length }), | ||
sort: (opt) => wrap("sort", propOrElse(opt, "reverse", false)), | ||
skew(opt = true) { | ||
if (typeof opt === "boolean") { | ||
return unwrap("skew", { bias: opt }); | ||
} | ||
return unwrap("skew", opt); | ||
}, | ||
slice(opts, length) { | ||
if (typeof opts === "number") { | ||
return wrap("slice", { offset: opts, length }); | ||
} | ||
return wrap("slice", opts); | ||
}, | ||
sort(opt = false) { | ||
if (typeof opt === "boolean") { | ||
return wrap("sort", { reverse: opt }); | ||
} | ||
return wrap("sort", opt); | ||
}, | ||
sub: (field) => dtypeAccessor(wrap)("sub", { field, key: "other" }), | ||
@@ -505,3 +519,3 @@ sum: noArgUnwrap("sum"), | ||
}; | ||
return new Proxy(out, { | ||
return new Proxy(seriesObject, { | ||
get: function (target, prop, receiver) { | ||
@@ -514,3 +528,3 @@ if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) { | ||
} | ||
} | ||
}, | ||
}); | ||
@@ -517,0 +531,0 @@ }; |
@@ -9,3 +9,2 @@ import { Expr } from "./lazy/expr"; | ||
export declare type ExprOrString = Expr | string; | ||
export declare type Option<T> = T | undefined; | ||
export declare type DownsampleRule = "month" | "week" | "day" | "hour" | "minute" | "second"; | ||
@@ -12,0 +11,0 @@ export declare type FillNullStrategy = "backward" | "forward" | "mean" | "min" | "max" | "zero" | "one"; |
{ | ||
"name": "nodejs-polars", | ||
"version": "0.0.4", | ||
"repository": "https://github.com/universalmind303/polars.git", | ||
"license": "MIT", | ||
"version": "0.0.5", | ||
"repository": "https://github.com/pola-rs/polars.git", | ||
"license": "SEE LICENSE IN LICENSE", | ||
"main": "bin/index.js", | ||
@@ -10,2 +10,8 @@ "files": [ | ||
], | ||
"keywords": [ | ||
"polars", | ||
"dataframe", | ||
"data-processing", | ||
"rust" | ||
], | ||
"napi": { | ||
@@ -38,6 +44,6 @@ "name": "nodejs-polars", | ||
"bench": "node -r @swc-node/register benchmark/bench.ts", | ||
"build_alt": "RUSTFLAGS='-C codegen-units=16' napi build --platform --release", | ||
"build:native": "napi build --platform --release", | ||
"build": "napi build --platform --release artifacts/", | ||
"build:debug": "napi build --platform polars/", | ||
"build:all": "tsc -p tsconfig.build.json && napi build --platform --release bin/", | ||
"build:all:slim": "tsc -p tsconfig.build.json && RUSTFLAGS='-C codegen-units=16' napi build --platform --release bin/", | ||
"build:ts": "tsc -p tsconfig.build.json", | ||
@@ -47,3 +53,3 @@ "format:rs": "cargo fmt", | ||
"format:yaml": "prettier --parser yaml --write './**/*.{yml,yaml}'", | ||
"lint": "eslint . -c ./.eslintrc.json './**/*.{ts,tsx,js}'", | ||
"lint:ts": "eslint -c ./.eslintrc.json 'polars/**/*.{ts,tsx,js}'", | ||
"prepublishOnly": "napi prepublish -t npm", | ||
@@ -103,15 +109,15 @@ "test": "jest", | ||
"optionalDependencies": { | ||
"nodejs-polars-win32-x64-msvc": "0.0.4", | ||
"nodejs-polars-darwin-x64": "0.0.4", | ||
"nodejs-polars-linux-x64-gnu": "0.0.4", | ||
"nodejs-polars-win32-ia32-msvc": "0.0.4", | ||
"nodejs-polars-linux-arm64-gnu": "0.0.4", | ||
"nodejs-polars-linux-arm-gnueabihf": "0.0.4", | ||
"nodejs-polars-darwin-arm64": "0.0.4", | ||
"nodejs-polars-android-arm64": "0.0.4", | ||
"nodejs-polars-freebsd-x64": "0.0.4", | ||
"nodejs-polars-linux-x64-musl": "0.0.4", | ||
"nodejs-polars-linux-arm64-musl": "0.0.4", | ||
"nodejs-polars-win32-arm64-msvc": "0.0.4" | ||
"nodejs-polars-win32-x64-msvc": "0.0.5", | ||
"nodejs-polars-darwin-x64": "0.0.5", | ||
"nodejs-polars-linux-x64-gnu": "0.0.5", | ||
"nodejs-polars-win32-ia32-msvc": "0.0.5", | ||
"nodejs-polars-linux-arm64-gnu": "0.0.5", | ||
"nodejs-polars-linux-arm-gnueabihf": "0.0.5", | ||
"nodejs-polars-darwin-arm64": "0.0.5", | ||
"nodejs-polars-android-arm64": "0.0.5", | ||
"nodejs-polars-freebsd-x64": "0.0.5", | ||
"nodejs-polars-linux-x64-musl": "0.0.5", | ||
"nodejs-polars-linux-arm64-musl": "0.0.5", | ||
"nodejs-polars-win32-arm64-msvc": "0.0.5" | ||
} | ||
} |
123
README.md
@@ -1,1 +0,122 @@ | ||
# nodejs-polars | ||
# Polars | ||
[![rust docs](https://docs.rs/polars/badge.svg)](https://docs.rs/polars/latest/polars/) | ||
[![Build and test](https://github.com/pola-rs/polars/workflows/Build%20and%20test/badge.svg)](https://github.com/pola-rs/polars/actions) | ||
[![](https://img.shields.io/crates/v/polars.svg)](https://crates.io/crates/polars) | ||
[![PyPI Latest Release](https://img.shields.io/pypi/v/polars.svg)](https://pypi.org/project/polars/) | ||
```js | ||
import pl from "polars" | ||
const df = pl.DataFrame( | ||
{ | ||
A: [1, 2, 3, 4, 5], | ||
fruits: ["banana", "banana", "apple", "apple", "banana"], | ||
B: [5, 4, 3, 2, 1], | ||
cars: ["beetle", "audi", "beetle", "beetle", "beetle"], | ||
} | ||
) | ||
const df2 = df | ||
.sort("fruits") | ||
.select( | ||
"fruits", | ||
"cars", | ||
pl.lit("fruits").alias("literal_string_fruits"), | ||
pl.col("B").filter(pl.col("cars").eq(lit("beetle"))).sum(), | ||
pl.col("A").filter(pl.col("B").gt(2)).sum().over("cars").alias("sum_A_by_cars"), | ||
pl.col("A").sum().over("fruits").alias("sum_A_by_fruits"), | ||
pl.col("A").reverse().over("fruits").flatten().alias("rev_A_by_fruits") | ||
) | ||
console.log(df2) | ||
shape: (5, 8) | ||
┌──────────┬──────────┬──────────────┬─────┬─────────────┬─────────────┬─────────────┐ | ||
│ fruits ┆ cars ┆ literal_stri ┆ B ┆ sum_A_by_ca ┆ sum_A_by_fr ┆ rev_A_by_fr │ | ||
│ --- ┆ --- ┆ ng_fruits ┆ --- ┆ rs ┆ uits ┆ uits │ | ||
│ str ┆ str ┆ --- ┆ i64 ┆ --- ┆ --- ┆ --- │ | ||
│ ┆ ┆ str ┆ ┆ i64 ┆ i64 ┆ i64 │ | ||
╞══════════╪══════════╪══════════════╪═════╪═════════════╪═════════════╪═════════════╡ | ||
│ "apple" ┆ "beetle" ┆ "fruits" ┆ 11 ┆ 4 ┆ 7 ┆ 4 │ | ||
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
│ "apple" ┆ "beetle" ┆ "fruits" ┆ 11 ┆ 4 ┆ 7 ┆ 3 │ | ||
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
│ "banana" ┆ "beetle" ┆ "fruits" ┆ 11 ┆ 4 ┆ 8 ┆ 5 │ | ||
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
│ "banana" ┆ "audi" ┆ "fruits" ┆ 11 ┆ 2 ┆ 8 ┆ 2 │ | ||
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
│ "banana" ┆ "beetle" ┆ "fruits" ┆ 11 ┆ 4 ┆ 8 ┆ 1 │ | ||
└──────────┴──────────┴──────────────┴─────┴─────────────┴─────────────┴─────────────┘ | ||
``` | ||
## Node setup | ||
Install the latest polars version with: | ||
```sh | ||
$ yarn add nodejs-polars # yarn | ||
$ npm i -s nodejs-polars # npm | ||
``` | ||
Releases happen quite often (weekly / every few days) at the moment, so updating polars regularly to get the latest bugfixes / features might not be a bad idea. | ||
#### Rust version | ||
Required Rust version `>=1.52` | ||
## Documentation | ||
Want to know about all the features Polars supports? Read the docs! | ||
#### Python | ||
* Installation guide: `$ pip3 install polars` | ||
* [Python documentation](https://pola-rs.github.io/polars/py-polars/html/reference/index.html) | ||
* [User guide](https://pola-rs.github.io/polars-book/) | ||
#### Rust | ||
* [Rust documentation (master branch)](https://pola-rs.github.io/polars/polars/index.html) | ||
* [User guide](https://pola-rs.github.io/polars-book/) | ||
### Node | ||
* COMING SOON! | ||
## Contribution | ||
Want to contribute? Read our [contribution guideline](https://github.com/pola-rs/polars/blob/master/CONTRIBUTING.md). | ||
## \[Node\]: compile polars from source | ||
If you want a bleeding edge release or maximal performance you should compile **polars** from source. | ||
1. Install the latest [Rust compiler](https://www.rust-lang.org/tools/install) | ||
2. Run `npm|yarn install` | ||
3. Choose any of: | ||
* Fastest binary, very long compile times: | ||
```bash | ||
$ cd nodejs-polars && yarn build:all # this will generate a /bin directory with the compiles TS code, as well as the rust binary | ||
``` | ||
* Fast binary, Shorter compile times: | ||
```bash | ||
$ cd nodejs-polars && yarn build:all:slim # this will generate a /bin directory with the compiles TS code, as well as the rust binary | ||
``` | ||
### Debugging | ||
for an unoptimized build, you can run `yarn build:debug` | ||
## Acknowledgements | ||
Development of Polars is proudly powered by | ||
[![Xomnia](https://raw.githubusercontent.com/pola-rs/polars-static/master/sponsors/xomnia.png)](https://www.xomnia.com/) | ||
## Sponsors | ||
[<img src="https://raw.githubusercontent.com/pola-rs/polars-static/master/sponsors/xomnia.png" height="40" />](https://www.xomnia.com/)   [<img src="https://www.jetbrains.com/company/brand/img/jetbrains_logo.png" height="50" />](https://www.jetbrains.com) |
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
283447
7592
122
0
1