@ohos-ports/nodejs-polars 0.23.4-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +217 -0
  3. package/bin/cfg.d.ts +156 -0
  4. package/bin/cfg.js +52 -0
  5. package/bin/dataframe.d.ts +2135 -0
  6. package/bin/dataframe.js +783 -0
  7. package/bin/datatypes/conversion.d.ts +2 -0
  8. package/bin/datatypes/conversion.js +2 -0
  9. package/bin/datatypes/datatype.d.ts +285 -0
  10. package/bin/datatypes/datatype.js +430 -0
  11. package/bin/datatypes/field.d.ts +26 -0
  12. package/bin/datatypes/field.js +36 -0
  13. package/bin/datatypes/index.d.ts +43 -0
  14. package/bin/datatypes/index.js +120 -0
  15. package/bin/error.d.ts +7 -0
  16. package/bin/error.js +17 -0
  17. package/bin/functions.d.ts +26 -0
  18. package/bin/functions.js +208 -0
  19. package/bin/groupby.d.ts +212 -0
  20. package/bin/groupby.js +156 -0
  21. package/bin/html.d.ts +4 -0
  22. package/bin/html.js +18 -0
  23. package/bin/index.d.ts +256 -0
  24. package/bin/index.js +267 -0
  25. package/bin/internals/construction.d.ts +7 -0
  26. package/bin/internals/construction.js +256 -0
  27. package/bin/internals/polars_internal.d.ts +2 -0
  28. package/bin/internals/polars_internal.js +7 -0
  29. package/bin/io.d.ts +433 -0
  30. package/bin/io.js +344 -0
  31. package/bin/lazy/dataframe.d.ts +785 -0
  32. package/bin/lazy/dataframe.js +427 -0
  33. package/bin/lazy/expr/datetime.d.ts +8 -0
  34. package/bin/lazy/expr/datetime.js +27 -0
  35. package/bin/lazy/expr/index.d.ts +1152 -0
  36. package/bin/lazy/expr/index.js +632 -0
  37. package/bin/lazy/expr/list.d.ts +8 -0
  38. package/bin/lazy/expr/list.js +115 -0
  39. package/bin/lazy/expr/string.d.ts +534 -0
  40. package/bin/lazy/expr/string.js +131 -0
  41. package/bin/lazy/expr/struct.d.ts +126 -0
  42. package/bin/lazy/expr/struct.js +26 -0
  43. package/bin/lazy/functions.d.ts +612 -0
  44. package/bin/lazy/functions.js +687 -0
  45. package/bin/lazy/groupby.d.ts +29 -0
  46. package/bin/lazy/groupby.js +30 -0
  47. package/bin/lazy/index.d.ts +4 -0
  48. package/bin/lazy/index.js +20 -0
  49. package/bin/lazy/whenthen.d.ts +67 -0
  50. package/bin/lazy/whenthen.js +81 -0
  51. package/bin/native-polars.d.ts +2 -0
  52. package/bin/native-polars.js +267 -0
  53. package/bin/series/datetime.d.ts +8 -0
  54. package/bin/series/datetime.js +34 -0
  55. package/bin/series/index.d.ts +1135 -0
  56. package/bin/series/index.js +700 -0
  57. package/bin/series/list.d.ts +8 -0
  58. package/bin/series/list.js +87 -0
  59. package/bin/series/string.d.ts +300 -0
  60. package/bin/series/string.js +125 -0
  61. package/bin/series/struct.d.ts +36 -0
  62. package/bin/series/struct.js +35 -0
  63. package/bin/shared_traits.d.ts +1458 -0
  64. package/bin/shared_traits.js +2 -0
  65. package/bin/sql.d.ts +244 -0
  66. package/bin/sql.js +63 -0
  67. package/bin/types.d.ts +287 -0
  68. package/bin/types.js +2 -0
  69. package/bin/utils.d.ts +38 -0
  70. package/bin/utils.js +69 -0
  71. package/package.json +45 -0
@@ -0,0 +1,783 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DataFrame = exports._DataFrame = exports.writeCsvDefaultOptions = void 0;
7
+ const node_stream_1 = require("node:stream");
8
+ const datatypes_1 = require("./datatypes");
9
+ const functions_1 = require("./functions");
10
+ const groupby_1 = require("./groupby");
11
+ const html_1 = require("./html");
12
+ const construction_1 = require("./internals/construction");
13
+ const polars_internal_1 = __importDefault(require("./internals/polars_internal"));
14
+ const dataframe_1 = require("./lazy/dataframe");
15
+ const expr_1 = require("./lazy/expr");
16
+ const functions_2 = require("./lazy/functions");
17
+ const series_1 = require("./series");
18
+ const utils_1 = require("./utils");
19
+ const inspect = Symbol.for("nodejs.util.inspect.custom");
20
+ const jupyterDisplay = Symbol.for("Jupyter.display");
21
+ exports.writeCsvDefaultOptions = {
22
+ includeBom: false,
23
+ includeHeader: true,
24
+ separator: ",",
25
+ quoteChar: '"',
26
+ lineTerminator: "\n",
27
+ batchSize: 1024,
28
+ maintainOrder: true,
29
+ };
30
+ function prepareOtherArg(anyValue) {
31
+ if (series_1.Series.isSeries(anyValue)) {
32
+ return anyValue;
33
+ }
34
+ return (0, series_1.Series)([anyValue]);
35
+ }
36
+ function map(df, fn) {
37
+ return df.rows().map(fn);
38
+ }
39
+ function mapPolarsTypeToJSONSchema(colType) {
40
+ const typeMapping = {
41
+ Null: "null",
42
+ Bool: "boolean",
43
+ Int8: "integer",
44
+ Int16: "integer",
45
+ Int32: "integer",
46
+ Int64: "integer",
47
+ UInt8: "integer",
48
+ UInt16: "integer",
49
+ UInt32: "integer",
50
+ UInt64: "integer",
51
+ Float32: "number",
52
+ Float64: "number",
53
+ Date: "string",
54
+ Datetime: "string",
55
+ Utf8: "string",
56
+ Categorical: "string",
57
+ List: "array",
58
+ Struct: "object",
59
+ };
60
+ const dataType = colType.variant;
61
+ return typeMapping[dataType] || "string";
62
+ }
63
+ /** @ignore */
64
+ const _DataFrame = (_df) => {
65
+ const unwrap = (method, ...args) => {
66
+ return _df[method](...args);
67
+ };
68
+ const wrap = (method, ...args) => {
69
+ return (0, exports._DataFrame)(unwrap(method, ...args));
70
+ };
71
+ const df = {
72
+ /** @ignore */
73
+ _df,
74
+ [inspect]() {
75
+ return _df.toString();
76
+ },
77
+ *[Symbol.iterator]() {
78
+ let start = 0;
79
+ const len = this.width;
80
+ while (start < len) {
81
+ const s = this.toSeries(start);
82
+ start++;
83
+ yield s;
84
+ }
85
+ },
86
+ get [Symbol.toStringTag]() {
87
+ return "DataFrame";
88
+ },
89
+ get dtypes() {
90
+ return _df.dtypes().map(datatypes_1.DataType.deserialize);
91
+ },
92
+ get height() {
93
+ return _df.height;
94
+ },
95
+ get width() {
96
+ return _df.width;
97
+ },
98
+ get shape() {
99
+ return _df.shape;
100
+ },
101
+ get columns() {
102
+ return _df.columns;
103
+ },
104
+ set columns(names) {
105
+ _df.columns = names;
106
+ },
107
+ /**
108
+ * Return back text/html and application/vnd.dataresource+json representations
109
+ * of the DataFrame. This is intended to be a simple view of the DataFrame
110
+ * inside of notebooks.
111
+ *
112
+ * @returns Media bundle / mimetype keys for Jupyter frontends
113
+ */
114
+ [jupyterDisplay]() {
115
+ let rows = 50;
116
+ if (process.env.POLARS_FMT_MAX_ROWS) {
117
+ rows = Number.parseInt(process.env.POLARS_FMT_MAX_ROWS, 10);
118
+ }
119
+ const limited = this.limit(rows);
120
+ return {
121
+ "application/vnd.dataresource+json": limited.toDataResource(),
122
+ "text/html": limited.toHTML(),
123
+ };
124
+ },
125
+ get schema() {
126
+ return this.getColumns().reduce((acc, curr) => {
127
+ acc[curr.name] = curr.dtype;
128
+ return acc;
129
+ }, {});
130
+ },
131
+ clone() {
132
+ return wrap("clone");
133
+ },
134
+ describe() {
135
+ const describeCast = (df) => {
136
+ return (0, exports.DataFrame)(df.getColumns().map((s) => {
137
+ if (s.isNumeric() || s.isBoolean()) {
138
+ return s.cast(datatypes_1.DataType.Float64);
139
+ }
140
+ return s;
141
+ }));
142
+ };
143
+ const summary = (0, functions_1.concat)([
144
+ describeCast(this.mean()),
145
+ describeCast(this.std()),
146
+ describeCast(this.min()),
147
+ describeCast(this.max()),
148
+ describeCast(this.median()),
149
+ ]);
150
+ summary.insertAtIdx(0, (0, series_1.Series)("describe", ["mean", "std", "min", "max", "median"]));
151
+ return summary;
152
+ },
153
+ inner() {
154
+ return _df;
155
+ },
156
+ drop(...names) {
157
+ if (!Array.isArray(names[0]) && names.length === 1) {
158
+ return wrap("drop", names[0]);
159
+ }
160
+ const df = this.clone();
161
+ for (const name of names.flat(2)) {
162
+ df.inner().dropInPlace(name);
163
+ }
164
+ return df;
165
+ },
166
+ dropNulls(...subset) {
167
+ if (subset.length) {
168
+ return wrap("dropNulls", subset.flat(2));
169
+ }
170
+ return wrap("dropNulls");
171
+ },
172
+ explode(...columns) {
173
+ return (0, exports._DataFrame)(_df)
174
+ .lazy()
175
+ .explode(columns)
176
+ .collectSync({ noOptimization: true });
177
+ },
178
+ extend(other) {
179
+ return wrap("extend", other.inner());
180
+ },
181
+ filter(predicate) {
182
+ return this.lazy().filter(predicate).collectSync();
183
+ },
184
+ fillNull(strategy) {
185
+ return wrap("fillNull", strategy);
186
+ },
187
+ findIdxByName(name) {
188
+ return unwrap("findIdxByName", name);
189
+ },
190
+ fold(fn) {
191
+ if (this.width === 1) {
192
+ return this.toSeries(0);
193
+ }
194
+ return this.getColumns().reduce((acc, curr) => fn(acc, curr));
195
+ },
196
+ frameEqual(other, nullEqual = true) {
197
+ return unwrap("frameEqual", other._df, nullEqual);
198
+ },
199
+ getColumn(name) {
200
+ return (0, series_1._Series)(_df.column(name));
201
+ },
202
+ getColumns() {
203
+ return _df.getColumns().map(series_1._Series);
204
+ },
205
+ groupBy(...by) {
206
+ return (0, groupby_1._GroupBy)(_df, (0, utils_1.columnOrColumnsStrict)(by));
207
+ },
208
+ groupByRolling(opts) {
209
+ return (0, groupby_1.RollingGroupBy)((0, exports._DataFrame)(_df), opts.indexColumn, opts.period, opts.offset, opts.closed, opts.by);
210
+ },
211
+ groupByDynamic({ indexColumn, every, period, offset, label, includeBoundaries, closed, by, startBy, }) {
212
+ return (0, groupby_1.DynamicGroupBy)((0, exports._DataFrame)(_df), indexColumn, every, period, offset, label, includeBoundaries, closed, by, startBy);
213
+ },
214
+ upsample(opts, every, by, maintainOrder) {
215
+ let timeColumn;
216
+ if (typeof opts === "string") {
217
+ timeColumn = opts;
218
+ }
219
+ else {
220
+ timeColumn = opts.timeColumn;
221
+ by = opts.by;
222
+ every = opts.every;
223
+ maintainOrder = opts.maintainOrder ?? false;
224
+ }
225
+ if (typeof by === "string") {
226
+ by = [by];
227
+ }
228
+ else {
229
+ by = by ?? [];
230
+ }
231
+ return (0, exports._DataFrame)(_df.upsample(by, timeColumn, every, maintainOrder));
232
+ },
233
+ hashRows(obj = 0n, k1 = 1n, k2 = 2n, k3 = 3n) {
234
+ if (typeof obj === "number" || typeof obj === "bigint") {
235
+ return (0, series_1._Series)(_df.hashRows(BigInt(obj), BigInt(k1), BigInt(k2), BigInt(k3)));
236
+ }
237
+ const o = { k0: obj, k1: k1, k2: k2, k3: k3, ...obj };
238
+ return (0, series_1._Series)(_df.hashRows(BigInt(o.k0), BigInt(o.k1), BigInt(o.k2), BigInt(o.k3)));
239
+ },
240
+ head(length = 5) {
241
+ return wrap("head", length);
242
+ },
243
+ hstack(columns, inPlace = false) {
244
+ if (!Array.isArray(columns)) {
245
+ columns = columns.getColumns();
246
+ }
247
+ // OHOS/napi-rs workaround: native hstack cannot extract JsSeries
248
+ // references from a JS array; build via withColumn instead.
249
+ let out = _df;
250
+ for (const col of columns) {
251
+ out = out.withColumn(col.inner());
252
+ }
253
+ return (0, exports._DataFrame)(out);
254
+ },
255
+ insertAtIdx(idx, series) {
256
+ _df.insertAtIdx(idx, series.inner());
257
+ },
258
+ interpolate() {
259
+ return this.select((0, functions_2.col)("*").interpolate());
260
+ },
261
+ isDuplicated: () => (0, series_1._Series)(_df.isDuplicated()),
262
+ isEmpty: () => _df.height === 0,
263
+ isUnique: () => (0, series_1._Series)(_df.isUnique()),
264
+ join(other, options) {
265
+ options = { how: "inner", ...options };
266
+ const on = (0, utils_1.columnOrColumns)(options.on);
267
+ const how = options.how;
268
+ const suffix = options.suffix;
269
+ const coalesce = options.coalesce;
270
+ const validate = options.validate;
271
+ if (how === "cross") {
272
+ return (0, exports._DataFrame)(_df.join(other._df, [], [], how, suffix, coalesce, validate));
273
+ }
274
+ let leftOn = (0, utils_1.columnOrColumns)(options.leftOn);
275
+ let rightOn = (0, utils_1.columnOrColumns)(options.rightOn);
276
+ if (on) {
277
+ leftOn = on;
278
+ rightOn = on;
279
+ }
280
+ if ((leftOn && !rightOn) || (rightOn && !leftOn)) {
281
+ throw new TypeError("You should pass the column to join on as an argument.");
282
+ }
283
+ return wrap("join", other._df, leftOn, rightOn, how, suffix, coalesce, validate);
284
+ },
285
+ joinAsof(other, options) {
286
+ return this.lazy()
287
+ .joinAsof(other.lazy(), options)
288
+ .collectSync();
289
+ },
290
+ lazy: () => (0, dataframe_1._LazyDataFrame)(_df.lazy()),
291
+ limit: (length = 5) => wrap("head", length),
292
+ max(axis = 0) {
293
+ if (axis === 1) {
294
+ return (0, series_1._Series)(_df.hmax());
295
+ }
296
+ return this.lazy().max().collectSync();
297
+ },
298
+ mean(axis = 0, nullStrategy = "ignore") {
299
+ if (axis === 1) {
300
+ return (0, series_1._Series)(_df.hmean(nullStrategy));
301
+ }
302
+ return this.lazy().mean().collectSync();
303
+ },
304
+ median() {
305
+ return this.lazy().median().collectSync();
306
+ },
307
+ unpivot(ids, values, options) {
308
+ options = {
309
+ variableName: null,
310
+ valueName: null,
311
+ ...options,
312
+ };
313
+ return wrap("unpivot", (0, utils_1.columnOrColumns)(ids), (0, utils_1.columnOrColumns)(values), options.variableName, options.valueName);
314
+ },
315
+ min(axis = 0) {
316
+ if (axis === 1) {
317
+ return (0, series_1._Series)(_df.hmin());
318
+ }
319
+ return this.lazy().min().collectSync();
320
+ },
321
+ nChunks() {
322
+ return _df.nChunks();
323
+ },
324
+ nullCount() {
325
+ return wrap("nullCount");
326
+ },
327
+ partitionBy(by, strict = false, includeKey = true, mapFn = (df) => df) {
328
+ by = Array.isArray(by) ? by : [by];
329
+ return _df
330
+ .partitionBy(by, strict, includeKey)
331
+ .map((d) => mapFn((0, exports._DataFrame)(d)));
332
+ },
333
+ pivot(values, options) {
334
+ if (values && !options) {
335
+ options = values;
336
+ }
337
+ let { values: values_, index, on, maintainOrder = true, sortColumns = false, aggregateFunc = "first", separator, } = options;
338
+ values = values_ ?? values;
339
+ values = typeof values === "string" ? [values] : values;
340
+ index = typeof index === "string" ? [index] : index;
341
+ on = typeof on === "string" ? [on] : on;
342
+ let fn;
343
+ if (expr_1.Expr.isExpr(aggregateFunc)) {
344
+ fn = aggregateFunc;
345
+ }
346
+ else {
347
+ fn =
348
+ {
349
+ first: (0, functions_2.element)().first(),
350
+ sum: (0, functions_2.element)().sum(),
351
+ max: (0, functions_2.element)().max(),
352
+ min: (0, functions_2.element)().min(),
353
+ mean: (0, functions_2.element)().mean(),
354
+ median: (0, functions_2.element)().median(),
355
+ last: (0, functions_2.element)().last(),
356
+ count: (0, functions_2.element)().count(),
357
+ }[aggregateFunc] ??
358
+ new Error(`Unknown aggregate function ${aggregateFunc}`);
359
+ if (fn instanceof Error) {
360
+ throw fn;
361
+ }
362
+ }
363
+ return (0, exports._DataFrame)(_df.pivotExpr(values, on, index, fn, maintainOrder, sortColumns, separator));
364
+ },
365
+ quantile(quantile) {
366
+ return this.lazy().quantile(quantile).collectSync();
367
+ },
368
+ rechunk() {
369
+ return wrap("rechunk");
370
+ },
371
+ rename(mapping) {
372
+ const df = this.clone();
373
+ for (const [column, new_col] of Object.entries(mapping)) {
374
+ df.inner().rename(column, new_col);
375
+ }
376
+ return df;
377
+ },
378
+ replaceAtIdx(index, newColumn) {
379
+ _df.replaceAtIdx(index, newColumn.inner());
380
+ return this;
381
+ },
382
+ rows(callback) {
383
+ if (callback) {
384
+ return _df.toRowsCb(callback);
385
+ }
386
+ return _df.toRows();
387
+ },
388
+ sample(opts, frac, withReplacement = false, seed) {
389
+ if (arguments.length === 0) {
390
+ return wrap("sampleN", (0, series_1.Series)("", [1]).inner(), withReplacement, false, seed);
391
+ }
392
+ if (opts?.n !== undefined || opts?.frac !== undefined) {
393
+ return this.sample(opts.n, opts.frac, opts.withReplacement, seed);
394
+ }
395
+ if (typeof opts === "number") {
396
+ return wrap("sampleN", (0, series_1.Series)("", [opts]).inner(), withReplacement, false, seed);
397
+ }
398
+ if (typeof frac === "number") {
399
+ return wrap("sampleFrac", (0, series_1.Series)("", [frac]).inner(), withReplacement, false, seed);
400
+ }
401
+ throw new TypeError("must specify either 'frac' or 'n'");
402
+ },
403
+ select(...selection) {
404
+ const hasExpr = selection.flat().some((s) => expr_1.Expr.isExpr(s));
405
+ if (hasExpr) {
406
+ return (0, exports._DataFrame)(_df).lazy().select(selection).collectSync();
407
+ }
408
+ return wrap("select", (0, utils_1.columnOrColumnsStrict)(selection));
409
+ },
410
+ shift: (opt) => wrap("shift", opt?.periods ?? opt),
411
+ shiftAndFill(n, fillValue) {
412
+ if (typeof n === "number" && fillValue) {
413
+ return (0, exports._DataFrame)(_df).lazy().shiftAndFill(n, fillValue).collectSync();
414
+ }
415
+ return (0, exports._DataFrame)(_df)
416
+ .lazy()
417
+ .shiftAndFill(n.n, n.fillValue)
418
+ .collectSync();
419
+ },
420
+ shrinkToFit(inPlace = false) {
421
+ if (inPlace) {
422
+ _df.shrinkToFit();
423
+ }
424
+ else {
425
+ const d = this.clone();
426
+ d.inner().shrinkToFit();
427
+ return d;
428
+ }
429
+ },
430
+ slice(opts, length) {
431
+ if (typeof opts === "number") {
432
+ return wrap("slice", opts, length);
433
+ }
434
+ return wrap("slice", opts.offset, opts.length);
435
+ },
436
+ sort(arg, descending = false, nullsLast = false, maintainOrder = false) {
437
+ if (arg?.by !== undefined) {
438
+ return this.sort(arg.by, arg.descending ?? false, arg.nullsLast, arg.maintainOrder);
439
+ }
440
+ if (Array.isArray(arg) || expr_1.Expr.isExpr(arg)) {
441
+ return (0, exports._DataFrame)(_df)
442
+ .lazy()
443
+ .sort(arg, descending, nullsLast, maintainOrder)
444
+ .collectSync({ noOptimization: true });
445
+ }
446
+ return wrap("sort", arg, descending, nullsLast, maintainOrder);
447
+ },
448
+ std() {
449
+ return this.lazy().std().collectSync();
450
+ },
451
+ sum(axis = 0, nullStrategy = "ignore") {
452
+ if (axis === 1) {
453
+ return (0, series_1._Series)(_df.hsum(nullStrategy));
454
+ }
455
+ return this.lazy().sum().collectSync();
456
+ },
457
+ tail: (length = 5) => wrap("tail", length),
458
+ serialize(format) {
459
+ return _df.serialize(format);
460
+ },
461
+ writeCSV(dest, options) {
462
+ options = { ...exports.writeCsvDefaultOptions, ...options };
463
+ if (dest instanceof node_stream_1.Writable || typeof dest === "string") {
464
+ return _df.writeCsv(dest, options);
465
+ }
466
+ const buffers = [];
467
+ const writeStream = new node_stream_1.Stream.Writable({
468
+ write(chunk, _encoding, callback) {
469
+ buffers.push(chunk);
470
+ callback(null);
471
+ },
472
+ });
473
+ _df.writeCsv(writeStream, dest ?? options);
474
+ writeStream.end("");
475
+ return Buffer.concat(buffers);
476
+ },
477
+ toRecords() {
478
+ return _df.toObjects();
479
+ },
480
+ toHTML() {
481
+ let htmlTable = "<table>";
482
+ // Add table headers
483
+ htmlTable += "<thead><tr>";
484
+ for (const field of this.getColumns()) {
485
+ htmlTable += `<th>${(0, html_1.escapeHTML)(field.name)}</th>`;
486
+ }
487
+ htmlTable += "</tr></thead>";
488
+ // Add table data
489
+ htmlTable += "<tbody>";
490
+ for (const row of this.toRecords()) {
491
+ htmlTable += "<tr>";
492
+ for (const field of this.getColumns()) {
493
+ htmlTable += `<td>${(0, html_1.escapeHTML)(String(row[field.name]))}</td>`;
494
+ }
495
+ htmlTable += "</tr>";
496
+ }
497
+ htmlTable += "</tbody></table>";
498
+ return htmlTable;
499
+ },
500
+ toDataResource() {
501
+ const data = this.toRecords();
502
+ const fields = this.getColumns().map((column) => ({
503
+ name: column.name,
504
+ type: mapPolarsTypeToJSONSchema(column.dtype),
505
+ }));
506
+ return { data, schema: { fields } };
507
+ },
508
+ toObject() {
509
+ return this.getColumns().reduce((acc, curr) => {
510
+ acc[curr.name] = curr.toArray();
511
+ return acc;
512
+ }, {});
513
+ },
514
+ withRowIndex(name = "index", offset = 0) {
515
+ return wrap("withRowIndex", name, offset);
516
+ },
517
+ writeJSON(dest, options = { format: "lines" }) {
518
+ if (dest instanceof node_stream_1.Writable || typeof dest === "string") {
519
+ return _df.writeJson(dest, options);
520
+ }
521
+ const buffers = [];
522
+ const writeStream = new node_stream_1.Stream.Writable({
523
+ write(chunk, _encoding, callback) {
524
+ buffers.push(chunk);
525
+ callback(null);
526
+ },
527
+ });
528
+ _df.writeJson(writeStream, { ...options, ...dest });
529
+ writeStream.end("");
530
+ return Buffer.concat(buffers);
531
+ },
532
+ writeParquet(dest, options = { compression: "uncompressed" }) {
533
+ if (dest instanceof node_stream_1.Writable || typeof dest === "string") {
534
+ return _df.writeParquet(dest, options.compression);
535
+ }
536
+ const buffers = [];
537
+ const writeStream = new node_stream_1.Stream.Writable({
538
+ write(chunk, _encoding, callback) {
539
+ buffers.push(chunk);
540
+ callback(null);
541
+ },
542
+ });
543
+ _df.writeParquet(writeStream, dest?.compression ?? options?.compression);
544
+ writeStream.end("");
545
+ return Buffer.concat(buffers);
546
+ },
547
+ writeAvro(dest, options = { compression: "uncompressed" }) {
548
+ if (dest instanceof node_stream_1.Writable || typeof dest === "string") {
549
+ return _df.writeAvro(dest, options.compression);
550
+ }
551
+ const buffers = [];
552
+ const writeStream = new node_stream_1.Stream.Writable({
553
+ write(chunk, _encoding, callback) {
554
+ buffers.push(chunk);
555
+ callback(null);
556
+ },
557
+ });
558
+ _df.writeAvro(writeStream, dest?.compression ?? options?.compression);
559
+ writeStream.end("");
560
+ return Buffer.concat(buffers);
561
+ },
562
+ writeIPC(dest, options = { compression: "uncompressed" }) {
563
+ if (dest instanceof node_stream_1.Writable || typeof dest === "string") {
564
+ return _df.writeIpc(dest, options.compression);
565
+ }
566
+ const buffers = [];
567
+ const writeStream = new node_stream_1.Stream.Writable({
568
+ write(chunk, _encoding, callback) {
569
+ buffers.push(chunk);
570
+ callback(null);
571
+ },
572
+ });
573
+ _df.writeIpc(writeStream, dest?.compression ?? options?.compression);
574
+ writeStream.end("");
575
+ return Buffer.concat(buffers);
576
+ },
577
+ writeIPCStream(dest, options = { compression: "uncompressed" }) {
578
+ if (dest instanceof node_stream_1.Writable || typeof dest === "string") {
579
+ return _df.writeIpcStream(dest, options.compression);
580
+ }
581
+ const buffers = [];
582
+ const writeStream = new node_stream_1.Stream.Writable({
583
+ write(chunk, _encoding, callback) {
584
+ buffers.push(chunk);
585
+ callback(null);
586
+ },
587
+ });
588
+ _df.writeIpcStream(writeStream, dest?.compression ?? options?.compression);
589
+ writeStream.end("");
590
+ return Buffer.concat(buffers);
591
+ },
592
+ toSeries: (index = 0) => (0, series_1._Series)(_df.selectAtIdx(index)),
593
+ toStruct(name) {
594
+ return (0, series_1._Series)(_df.toStruct(name));
595
+ },
596
+ toString() {
597
+ return _df.toString();
598
+ },
599
+ transpose(options) {
600
+ const includeHeader = options?.includeHeader ?? false;
601
+ const headeName = options?.headerName ?? "column";
602
+ const keep_names_as = includeHeader ? headeName : undefined;
603
+ if (options?.columnNames) {
604
+ function takeNItems(iterable, n) {
605
+ const result = [];
606
+ let i = 0;
607
+ for (const item of iterable) {
608
+ if (i >= n) {
609
+ break;
610
+ }
611
+ result.push(item);
612
+ i++;
613
+ }
614
+ return result;
615
+ }
616
+ options.columnNames = Array.isArray(options.columnNames)
617
+ ? options.columnNames.slice(0, this.height)
618
+ : takeNItems(options.columnNames, this.height);
619
+ }
620
+ if (!options?.columnNames) {
621
+ return wrap("transpose", keep_names_as, undefined);
622
+ }
623
+ return wrap("transpose", keep_names_as, options.columnNames);
624
+ },
625
+ unique(opts, keep = "any", maintainOrder = false) {
626
+ // no arguments -> signal call with defaults
627
+ if (arguments.length === 0)
628
+ return wrap("unique", null, keep, maintainOrder);
629
+ // string -> single-element array
630
+ if (typeof opts === "string")
631
+ return wrap("unique", [opts], keep, maintainOrder);
632
+ // array -> use as-is
633
+ if (Array.isArray(opts))
634
+ return wrap("unique", opts, keep, maintainOrder);
635
+ // object -> merge defaults, normalize subset to array (if present)
636
+ if (opts && typeof opts === "object") {
637
+ const o = { keep, maintainOrder, ...opts };
638
+ const subset = o.subset ? [o.subset].flat(3) : undefined;
639
+ return wrap("unique", subset, o.keep, o.maintainOrder);
640
+ }
641
+ },
642
+ unnest(columns, separator) {
643
+ columns = Array.isArray(columns) ? columns : [columns];
644
+ return (0, exports._DataFrame)(_df.unnest(columns, separator));
645
+ },
646
+ var() {
647
+ return this.lazy().var().collectSync();
648
+ },
649
+ map: (fn) => map((0, exports._DataFrame)(_df), fn),
650
+ row(idx) {
651
+ return _df.toRow(idx);
652
+ },
653
+ vstack: (other) => wrap("vstack", other.inner()),
654
+ withColumn(column) {
655
+ if (series_1.Series.isSeries(column)) {
656
+ return wrap("withColumn", column.inner());
657
+ }
658
+ return this.withColumns(column);
659
+ },
660
+ withColumns(...columns) {
661
+ if ((0, utils_1.isSeriesArray)(columns)) {
662
+ return columns.reduce((acc, curr) => acc.withColumn(curr), (0, exports._DataFrame)(_df));
663
+ }
664
+ return this.lazy()
665
+ .withColumns(...columns)
666
+ .collectSync({ noOptimization: true });
667
+ },
668
+ withColumnRenamed(opt, replacement) {
669
+ if (typeof opt === "string") {
670
+ return this.rename({ [opt]: replacement });
671
+ }
672
+ return this.rename({ [opt.existing]: opt.replacement });
673
+ },
674
+ withRowCount(name = "row_nr") {
675
+ return wrap("withRowCount", name);
676
+ },
677
+ where(predicate) {
678
+ return this.filter(predicate);
679
+ },
680
+ add: (other) => wrap("add", prepareOtherArg(other).inner()),
681
+ sub: (other) => wrap("sub", prepareOtherArg(other).inner()),
682
+ div: (other) => wrap("div", prepareOtherArg(other).inner()),
683
+ mul: (other) => wrap("mul", prepareOtherArg(other).inner()),
684
+ rem: (other) => wrap("rem", prepareOtherArg(other).inner()),
685
+ plus: (other) => wrap("add", prepareOtherArg(other).inner()),
686
+ minus: (other) => wrap("sub", prepareOtherArg(other).inner()),
687
+ divideBy: (other) => wrap("div", prepareOtherArg(other).inner()),
688
+ multiplyBy: (other) => wrap("mul", prepareOtherArg(other).inner()),
689
+ modulo: (other) => wrap("rem", prepareOtherArg(other).inner()),
690
+ };
691
+ return new Proxy(df, {
692
+ get(target, prop, receiver) {
693
+ if (typeof prop === "string" && target.columns.includes(prop)) {
694
+ return target.getColumn(prop);
695
+ }
696
+ if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) {
697
+ return target.row(Number(prop));
698
+ }
699
+ return Reflect.get(target, prop, receiver);
700
+ },
701
+ set(target, prop, receiver) {
702
+ if (series_1.Series.isSeries(receiver)) {
703
+ if (typeof prop === "string" && target.columns.includes(prop)) {
704
+ const idx = target.columns.indexOf(prop);
705
+ target.replaceAtIdx(idx, receiver.alias(prop));
706
+ return true;
707
+ }
708
+ }
709
+ Reflect.set(target, prop, receiver);
710
+ return true;
711
+ },
712
+ has(target, p) {
713
+ if (p === jupyterDisplay) {
714
+ return true;
715
+ }
716
+ return target.columns.includes(p);
717
+ },
718
+ ownKeys(target) {
719
+ return target.columns;
720
+ },
721
+ getOwnPropertyDescriptor(target, prop) {
722
+ return {
723
+ configurable: true,
724
+ enumerable: true,
725
+ value: target.getColumn(prop),
726
+ };
727
+ },
728
+ });
729
+ };
730
+ exports._DataFrame = _DataFrame;
731
+ function DataFrameConstructor(data, options) {
732
+ if ((!data || Object.keys(data).length === 0) && options?.schema) {
733
+ return (0, exports._DataFrame)((0, construction_1.jsDataFrameFromSeries)(Object.keys(options.schema).map((key) => (0, series_1.Series)(key, [], options.schema[key]).inner())));
734
+ }
735
+ if (!data) {
736
+ return (0, exports._DataFrame)(objToDF({}));
737
+ }
738
+ if (Array.isArray(data)) {
739
+ return (0, exports._DataFrame)((0, construction_1.arrayToJsDataFrame)(data, options));
740
+ }
741
+ return (0, exports._DataFrame)(objToDF(data, options));
742
+ }
743
+ function objToDF(obj, options) {
744
+ let columns;
745
+ if (options?.schema && options?.schemaOverrides) {
746
+ throw new Error("Cannot use both 'schema' and 'schemaOverrides'");
747
+ }
748
+ // explicit schema
749
+ if (options?.schema) {
750
+ const schema = options.schema;
751
+ const schemaKeys = Object.keys(options.schema);
752
+ const values = Object.values(obj);
753
+ if (schemaKeys.length !== values.length) {
754
+ throw new Error("The number of columns in the schema does not match the number of columns in the data");
755
+ }
756
+ columns = values.map((values, idx) => {
757
+ const name = schemaKeys[idx];
758
+ const dtype = schema[name];
759
+ return (0, series_1.Series)(name, values, dtype).inner();
760
+ });
761
+ }
762
+ else {
763
+ columns = Object.entries(obj).map(([name, values]) => {
764
+ if (series_1.Series.isSeries(values)) {
765
+ return values.rename(name).inner();
766
+ }
767
+ // schema overrides
768
+ if (options?.schemaOverrides) {
769
+ const dtype = options.schemaOverrides[name];
770
+ if (dtype) {
771
+ return (0, series_1.Series)(name, values, dtype).inner();
772
+ }
773
+ }
774
+ return (0, series_1.Series)(name, values).inner();
775
+ });
776
+ }
777
+ return (0, construction_1.jsDataFrameFromSeries)(columns);
778
+ }
779
+ const isDataFrame = (anyVal) => anyVal?.[Symbol.toStringTag] === "DataFrame";
780
+ exports.DataFrame = Object.assign(DataFrameConstructor, {
781
+ isDataFrame,
782
+ deserialize: (buf, fmt) => (0, exports._DataFrame)(polars_internal_1.default.JsDataFrame.deserialize(buf, fmt)),
783
+ });