@jrc03c/make-key 0.0.30 → 0.0.32

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.
@@ -0,0 +1,4297 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
25
+ mod2
26
+ ));
27
+ var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
28
+
29
+ // src/index.mjs
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ makeKey: () => makeKey4
33
+ });
34
+ module.exports = __toCommonJS(src_exports);
35
+
36
+ // node_modules/@jrc03c/js-math-tools/dist/js-math-tools.import.mjs
37
+ var __defProp2 = Object.defineProperty;
38
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
39
+ var __publicField = (obj, key, value) => {
40
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
41
+ return value;
42
+ };
43
+ function isNumber(x) {
44
+ return typeof x === "number" && !isNaN(x) || typeof x === "bigint";
45
+ }
46
+ var isBrowser = new Function(`
47
+ try {
48
+ return this === window
49
+ } catch(e) {}
50
+
51
+ try {
52
+ return typeof importScripts !== "undefined"
53
+ } catch(e) {}
54
+
55
+ return false
56
+ `);
57
+ var MathError = class extends Error {
58
+ constructor(message) {
59
+ if (isBrowser()) {
60
+ super(message);
61
+ } else {
62
+ super("\n\n\x1B[31m" + message + "\n\x1B[0m");
63
+ }
64
+ }
65
+ };
66
+ function assert(isTrue, message) {
67
+ if (!isTrue)
68
+ throw new MathError(message);
69
+ }
70
+ var arrayTypes = [
71
+ Array,
72
+ ArrayBuffer,
73
+ BigInt64Array,
74
+ BigUint64Array,
75
+ Float32Array,
76
+ Float64Array,
77
+ Int16Array,
78
+ Int32Array,
79
+ Int8Array,
80
+ Uint16Array,
81
+ Uint32Array,
82
+ Uint8Array,
83
+ Uint8ClampedArray
84
+ ];
85
+ function isUndefined(x) {
86
+ return x === null || typeof x === "undefined";
87
+ }
88
+ var typeStrings = arrayTypes.map((s2) => s2.name);
89
+ function isArray(obj) {
90
+ try {
91
+ if (obj instanceof Array) {
92
+ return true;
93
+ }
94
+ if (!isUndefined(obj.constructor)) {
95
+ return arrayTypes.indexOf(obj.constructor) > -1 || typeStrings.indexOf(obj.constructor.name) > -1;
96
+ }
97
+ return false;
98
+ } catch (e) {
99
+ return false;
100
+ }
101
+ }
102
+ function isDataFrame(x) {
103
+ try {
104
+ return !!x._symbol && x._symbol === Symbol.for("@jrc03c/js-math-tools/dataframe");
105
+ } catch (e) {
106
+ return false;
107
+ }
108
+ }
109
+ function isFunction(fn) {
110
+ return typeof fn === "function";
111
+ }
112
+ function isObject(x) {
113
+ return typeof x === "object" && !isUndefined(x) && !isArray(x);
114
+ }
115
+ function isSeries(x) {
116
+ try {
117
+ return !!x._symbol && x._symbol === Symbol.for("@jrc03c/js-math-tools/series");
118
+ } catch (e) {
119
+ return false;
120
+ }
121
+ }
122
+ function indexOf(x, fn) {
123
+ if (isDataFrame(x)) {
124
+ const index = indexOf(x.values, fn);
125
+ if (index.length > 0 && isNumber(index[0]) && index[0] >= 0 && index[0] < x.index.length) {
126
+ index[0] = x.index[index[0]];
127
+ }
128
+ if (index.length > 1 && isNumber(index[1]) && index[1] >= 0 && index[1] < x.columns.length) {
129
+ index[1] = x.columns[index[1]];
130
+ }
131
+ return index;
132
+ }
133
+ if (isSeries(x)) {
134
+ const index = indexOf(x.values, fn);
135
+ if (index.length > 0 && isNumber(index[0]) && index[0] >= 0 && index[0] < x.index.length) {
136
+ index[0] = x.index[index[0]];
137
+ }
138
+ return index;
139
+ }
140
+ assert(isObject(x) || isArray(x), "You must pass (1) an object, array, Series, or DataFrame and (2) a function or value into the `indexOf` function!");
141
+ if (!isFunction(fn)) {
142
+ const value = fn;
143
+ fn = (v) => v === value;
144
+ }
145
+ function helper5(x2, fn2, checked) {
146
+ checked = checked || [];
147
+ if (checked.indexOf(x2) > -1) {
148
+ return null;
149
+ }
150
+ if (isObject(x2)) {
151
+ checked.push(x2);
152
+ const keys = Object.keys(x2).concat(Object.getOwnPropertySymbols(x2));
153
+ for (let i = 0; i < keys.length; i++) {
154
+ const key = keys[i];
155
+ const value = x2[key];
156
+ if (fn2(value)) {
157
+ return [key];
158
+ }
159
+ const results = helper5(value, fn2, checked);
160
+ if (results && results.length > 0) {
161
+ return [key].concat(results);
162
+ }
163
+ }
164
+ } else if (isArray(x2)) {
165
+ checked.push(x2);
166
+ for (let i = 0; i < x2.length; i++) {
167
+ const value = x2[i];
168
+ if (fn2(value)) {
169
+ return [i];
170
+ }
171
+ const results = helper5(value, fn2, checked);
172
+ if (results && results.length > 0) {
173
+ return [i].concat(results);
174
+ }
175
+ }
176
+ } else {
177
+ if (fn2(x2)) {
178
+ return [];
179
+ }
180
+ }
181
+ return null;
182
+ }
183
+ function safeFn(v) {
184
+ try {
185
+ return fn(v);
186
+ } catch (e) {
187
+ return false;
188
+ }
189
+ }
190
+ const paths = helper5(x, safeFn);
191
+ if (paths && paths.length > 0) {
192
+ return paths;
193
+ } else {
194
+ return null;
195
+ }
196
+ }
197
+ function copy(x) {
198
+ function helper5(x2) {
199
+ if (typeof x2 === "object") {
200
+ if (x2 === null) {
201
+ return null;
202
+ }
203
+ if (isArray(x2)) {
204
+ if (!(x2 instanceof Array)) {
205
+ return x2.slice();
206
+ }
207
+ return x2.map((v) => copy(v));
208
+ }
209
+ if (isSeries(x2)) {
210
+ const out3 = x2.copy();
211
+ out3.values = copy(out3.values);
212
+ return out3;
213
+ }
214
+ if (isDataFrame(x2)) {
215
+ const out3 = x2.copy();
216
+ out3.values = copy(x2.values);
217
+ return out3;
218
+ }
219
+ if (x2 instanceof Date) {
220
+ return new Date(x2.getTime());
221
+ }
222
+ x2 = decycle(x2);
223
+ const out2 = {};
224
+ Object.keys(x2).concat(Object.getOwnPropertySymbols(x2)).forEach((key) => {
225
+ out2[key] = copy(x2[key]);
226
+ });
227
+ return out2;
228
+ } else {
229
+ return x2;
230
+ }
231
+ }
232
+ return helper5(decycle(x));
233
+ }
234
+ function decycle(x) {
235
+ function helper5(x2, checked, currentPath) {
236
+ checked = checked || [];
237
+ currentPath = currentPath || "";
238
+ if (checked.indexOf(x2) > -1) {
239
+ const parts = currentPath.split("/").slice(currentPath.startsWith("/") ? 1 : 0);
240
+ const isANestedCopy = parts.some((v, i) => {
241
+ const subParts = parts.slice(0, parts.length - i - 1);
242
+ let temp = orig;
243
+ subParts.forEach((part) => {
244
+ temp = temp[part];
245
+ });
246
+ return temp === x2;
247
+ });
248
+ if (isANestedCopy) {
249
+ const pathToCopy = orig === x2 ? "/" : "/" + indexOf(orig, x2).join("/");
250
+ return `<reference to "${pathToCopy}">`;
251
+ }
252
+ }
253
+ if (typeof x2 === "object") {
254
+ if (x2 === null)
255
+ return null;
256
+ checked.push(x2);
257
+ if (isArray(x2)) {
258
+ if (typeof x2.constructor !== "undefined" && x2.constructor.name !== "Array") {
259
+ return x2.slice();
260
+ }
261
+ return x2.map((v, i) => helper5(v, checked, currentPath + "/" + i));
262
+ } else {
263
+ Object.keys(x2).concat(Object.getOwnPropertySymbols(x2)).forEach((key) => {
264
+ x2[key] = helper5(x2[key], checked, currentPath + "/" + key.toString());
265
+ });
266
+ return x2;
267
+ }
268
+ } else {
269
+ return x2;
270
+ }
271
+ }
272
+ const orig = x;
273
+ let out2 = helper5(orig);
274
+ if (isDataFrame(x)) {
275
+ const temp = x.copy();
276
+ temp._values = out2.values;
277
+ temp._columns = out2.columns;
278
+ temp._index = out2.index;
279
+ out2 = temp;
280
+ }
281
+ if (isSeries(x)) {
282
+ const temp = x.copy();
283
+ temp.name = out2.name;
284
+ temp._values = out2.values;
285
+ temp._index = out2.index;
286
+ out2 = temp;
287
+ }
288
+ return out2;
289
+ }
290
+ function isDate(x) {
291
+ return x instanceof Date && x.toString() !== "Invalid Date";
292
+ }
293
+ var numberTypes = ["number", "int", "float", "bigint"];
294
+ function isEqual(a, b) {
295
+ function helper5(a2, b2) {
296
+ const aType = typeof a2;
297
+ const bType = typeof b2;
298
+ if (aType !== bType && !numberTypes.includes(aType) && !numberTypes.includes(bType))
299
+ return false;
300
+ if (aType === "undefined" && bType === "undefined")
301
+ return true;
302
+ if (aType === "boolean")
303
+ return a2 === b2;
304
+ if (aType === "symbol")
305
+ return a2 === b2;
306
+ if (aType === "number" || aType === "bigint") {
307
+ try {
308
+ const aString = a2.toString();
309
+ const bString = b2.toString();
310
+ return aString === bString;
311
+ } catch (e) {
312
+ return false;
313
+ }
314
+ }
315
+ if (aType === "string")
316
+ return a2 === b2;
317
+ if (aType === "function")
318
+ return a2 === b2;
319
+ if (aType === "object") {
320
+ if (a2 === null || b2 === null) {
321
+ return a2 === null && b2 === null;
322
+ } else {
323
+ if (isDate(a2)) {
324
+ if (isDate(b2)) {
325
+ return a2.getTime() === b2.getTime();
326
+ } else {
327
+ return false;
328
+ }
329
+ } else if (isDate(b2)) {
330
+ return false;
331
+ }
332
+ if (a2 instanceof RegExp && b2 instanceof RegExp) {
333
+ return a2.toString() === b2.toString();
334
+ }
335
+ if (isArray(a2) !== isArray(b2)) {
336
+ return false;
337
+ }
338
+ const aKeys = Object.keys(a2).concat(Object.getOwnPropertySymbols(a2));
339
+ const bKeys = Object.keys(b2).concat(Object.getOwnPropertySymbols(b2));
340
+ if (aKeys.length !== bKeys.length)
341
+ return false;
342
+ for (let i = 0; i < aKeys.length; i++) {
343
+ const key = aKeys[i];
344
+ if (!helper5(a2[key], b2[key]))
345
+ return false;
346
+ }
347
+ return true;
348
+ }
349
+ }
350
+ }
351
+ try {
352
+ return helper5(a, b);
353
+ } catch (e) {
354
+ return helper5(decycle(a), decycle(b));
355
+ }
356
+ }
357
+ function makeKey(n) {
358
+ const alpha = "abcdefg1234567890";
359
+ let out2 = "";
360
+ while (out2.length < n)
361
+ out2 += alpha[Math.floor(Math.random() * alpha.length)];
362
+ return out2;
363
+ }
364
+ var NULL_KEY = makeKey(16);
365
+ var UNDEFINED_KEY = makeKey(16);
366
+ var INFINITY_KEY = makeKey(16);
367
+ var MINUS_INFINITY_KEY = makeKey(16);
368
+ var SYMBOL_KEY = makeKey(16);
369
+ var Counter = class {
370
+ constructor() {
371
+ this.clear();
372
+ }
373
+ get counts() {
374
+ return this.values.map((v) => this.get(v));
375
+ }
376
+ get values() {
377
+ return Object.values(this.valuesDict);
378
+ }
379
+ clear() {
380
+ this.countsDict = {};
381
+ this.valuesDict = {};
382
+ return this;
383
+ }
384
+ count(x) {
385
+ for (const v of x) {
386
+ if (isArray(v)) {
387
+ this.count(v);
388
+ } else {
389
+ this.increment(v);
390
+ }
391
+ }
392
+ return this;
393
+ }
394
+ delete(value) {
395
+ const key = this.getStandardizedKey(value);
396
+ delete this.countsDict[key];
397
+ delete this.valuesDict[key];
398
+ return this;
399
+ }
400
+ get(value) {
401
+ return this.countsDict[this.getStandardizedKey(value)] || 0;
402
+ }
403
+ getStandardizedKey(value) {
404
+ return typeof value === "object" && value === null ? NULL_KEY : isUndefined(value) ? UNDEFINED_KEY : isFunction(value) ? value.toString() : typeof value === "symbol" ? value.toString() + " - " + SYMBOL_KEY : value === Infinity ? INFINITY_KEY : value === -Infinity ? MINUS_INFINITY_KEY : typeof value === "bigint" ? value.toString() : isDataFrame(value) ? value.toJSONString() : isSeries(value) ? JSON.stringify(value.toObject()) : JSON.stringify(value);
405
+ }
406
+ has(value) {
407
+ return !isUndefined(this.countsDict[this.getStandardizedKey(value)]);
408
+ }
409
+ increment(value) {
410
+ return this.set(value, this.get(value) + 1);
411
+ }
412
+ set(value, count2) {
413
+ const key = this.getStandardizedKey(value);
414
+ this.countsDict[key] = count2;
415
+ this.valuesDict[key] = value;
416
+ return this;
417
+ }
418
+ toArray() {
419
+ return this.values.map((v) => ({ value: v, count: this.get(v) }));
420
+ }
421
+ toObject() {
422
+ const out2 = {};
423
+ this.values.forEach((value) => {
424
+ out2[value] = this.get(value);
425
+ });
426
+ return out2;
427
+ }
428
+ };
429
+ function flatten(arr) {
430
+ if (isDataFrame(arr) || isSeries(arr)) {
431
+ return flatten(arr.values);
432
+ }
433
+ assert(isArray(arr), "The `flatten` function only works on arrays, Series, and DataFrames!");
434
+ function helper5(arr2) {
435
+ let out2 = [];
436
+ arr2.forEach((child) => {
437
+ if (isArray(child)) {
438
+ out2 = out2.concat(helper5(child));
439
+ } else {
440
+ out2.push(child);
441
+ }
442
+ });
443
+ return out2;
444
+ }
445
+ return helper5(arr);
446
+ }
447
+ function stats(x, options) {
448
+ options = options || {};
449
+ const counts = new Counter();
450
+ const out2 = {};
451
+ const xflat = flatten(x);
452
+ const xnums = [];
453
+ let max2 = -Infinity;
454
+ let min2 = Infinity;
455
+ let resultsShouldIncludeBigInts = false;
456
+ let sum2 = 0;
457
+ for (const v of xflat) {
458
+ if (typeof v === "bigint") {
459
+ resultsShouldIncludeBigInts = true;
460
+ }
461
+ if (!options.shouldDropNaNs || isNumber(v)) {
462
+ try {
463
+ if (v > max2) {
464
+ max2 = v;
465
+ }
466
+ if (v < min2) {
467
+ min2 = v;
468
+ }
469
+ sum2 += Number(v);
470
+ xnums.push(v);
471
+ } catch (e) {
472
+ max2 = NaN;
473
+ min2 = NaN;
474
+ sum2 = NaN;
475
+ }
476
+ }
477
+ counts.increment(v);
478
+ }
479
+ const mean2 = sum2 / xnums.length;
480
+ out2.counts = counts;
481
+ out2.max = max2;
482
+ out2.mean = mean2;
483
+ out2.min = min2;
484
+ out2.n = xflat.length;
485
+ out2.sum = sum2;
486
+ if (isNaN(out2.mean)) {
487
+ out2.max = NaN;
488
+ out2.min = NaN;
489
+ }
490
+ if (options.shouldDropNaNs) {
491
+ out2.nWithoutNaNs = xnums.length;
492
+ }
493
+ if (options.mode) {
494
+ const sortedCountPairs = Array.from(counts.values.map((v) => [v, counts.get(v)])).toSorted((a, b) => b[1] - a[1]);
495
+ const highestCount = sortedCountPairs[0][1];
496
+ const mode2 = [];
497
+ for (const pair of sortedCountPairs) {
498
+ if (pair[1] == highestCount) {
499
+ mode2.push(pair[0]);
500
+ } else {
501
+ break;
502
+ }
503
+ }
504
+ out2.mode = mode2.toSorted();
505
+ }
506
+ if (options.median) {
507
+ if (isNaN(mean2)) {
508
+ out2.median = NaN;
509
+ } else {
510
+ const xnumsSorted = xnums.toSorted((a, b) => Number(a) - Number(b));
511
+ const middle = Math.floor(xnumsSorted.length / 2);
512
+ if (xnumsSorted.length % 2 === 0) {
513
+ const left = xnumsSorted[middle - 1];
514
+ const right = xnumsSorted[middle];
515
+ out2.median = (Number(left) + Number(right)) / 2;
516
+ if (resultsShouldIncludeBigInts && typeof left === "bigint" && typeof right === "bigint") {
517
+ try {
518
+ out2.median = BigInt(out2.median);
519
+ } catch (e) {
520
+ }
521
+ }
522
+ } else {
523
+ out2.median = xnumsSorted[middle];
524
+ }
525
+ }
526
+ }
527
+ if (options.stdev || options.variance) {
528
+ let variance2 = 0;
529
+ for (const v of xnums) {
530
+ variance2 += Math.pow(Number(v) - mean2, 2);
531
+ }
532
+ variance2 /= xnums.length;
533
+ const stdev2 = Math.sqrt(variance2);
534
+ out2.stdev = stdev2;
535
+ out2.variance = variance2;
536
+ }
537
+ if (resultsShouldIncludeBigInts) {
538
+ try {
539
+ out2.sum = BigInt(out2.sum);
540
+ } catch (e) {
541
+ }
542
+ try {
543
+ out2.mean = BigInt(out2.mean);
544
+ } catch (e) {
545
+ }
546
+ if (options.mode) {
547
+ out2.mode = out2.mode.map((v) => {
548
+ try {
549
+ return BigInt(v);
550
+ } catch (e) {
551
+ return v;
552
+ }
553
+ });
554
+ }
555
+ }
556
+ return out2;
557
+ }
558
+ function count(arr, matcher) {
559
+ const { counts } = stats(arr);
560
+ if (!isUndefined(matcher)) {
561
+ if (isFunction(matcher)) {
562
+ counts.values.forEach((v) => {
563
+ if (!matcher(v)) {
564
+ counts.delete(v);
565
+ }
566
+ });
567
+ } else {
568
+ counts.values.forEach((v) => {
569
+ if (!isEqual(v, matcher)) {
570
+ counts.delete(v);
571
+ }
572
+ });
573
+ }
574
+ }
575
+ return counts;
576
+ }
577
+ function helper(x) {
578
+ if (isDataFrame(x) || isSeries(x)) {
579
+ return helper(x.values);
580
+ }
581
+ if (isArray(x)) {
582
+ let hasArrayValues = false;
583
+ let hasNonArrayValues = false;
584
+ let arrayLength = null;
585
+ for (const v of x) {
586
+ if (helper(v)) {
587
+ return true;
588
+ }
589
+ if (isArray(v)) {
590
+ if (arrayLength === null) {
591
+ arrayLength = v.length;
592
+ } else if (v.length !== arrayLength) {
593
+ return true;
594
+ }
595
+ hasArrayValues = true;
596
+ } else {
597
+ hasNonArrayValues = true;
598
+ }
599
+ if (hasArrayValues && hasNonArrayValues) {
600
+ return true;
601
+ }
602
+ }
603
+ }
604
+ return false;
605
+ }
606
+ function isJagged(x) {
607
+ return helper(decycle(x));
608
+ }
609
+ function isNested(x) {
610
+ if (isDataFrame(x) || isSeries(x)) {
611
+ return isNested(x.values);
612
+ }
613
+ assert(isArray(x), "The `isNested` function only works on arrays, Series, and DataFrames!");
614
+ for (let i = 0; i < x.length; i++) {
615
+ if (isArray(x[i])) {
616
+ return true;
617
+ }
618
+ }
619
+ return false;
620
+ }
621
+ var error = "You must pass a natural number or a one-dimensional array of natural numbers into the `ndarray` function!";
622
+ function ndarray(shape2) {
623
+ assert(!isUndefined(shape2), error);
624
+ if (!isArray(shape2))
625
+ shape2 = [shape2];
626
+ assert(!isNested(shape2), error);
627
+ assert(shape2.length > 0, error);
628
+ let s2 = shape2[0];
629
+ if (typeof s2 === "bigint")
630
+ s2 = Number(s2);
631
+ assert(isNumber(s2), error);
632
+ assert(s2 >= 0, error);
633
+ assert(Math.floor(s2) === s2, error);
634
+ assert(s2 !== Infinity, "We can't create an array containing an infinite number of values!");
635
+ if (shape2.length === 1) {
636
+ const out2 = [];
637
+ for (let i = 0; i < s2; i++)
638
+ out2.push(void 0);
639
+ return out2;
640
+ } else {
641
+ const out2 = [];
642
+ for (let i = 0; i < s2; i++) {
643
+ out2.push(ndarray(shape2.slice(1)));
644
+ }
645
+ return out2;
646
+ }
647
+ }
648
+ function reverse(arr) {
649
+ if (isDataFrame(arr) || isSeries(arr)) {
650
+ const out3 = arr.copy();
651
+ out3.values = reverse(out3.values);
652
+ out3.index = reverse(out3.index);
653
+ return out3;
654
+ }
655
+ assert(isArray(arr), "The `reverse` function only works on arrays, Series, and DataFrames!");
656
+ const out2 = [];
657
+ for (let i = arr.length - 1; i >= 0; i--)
658
+ out2.push(arr[i]);
659
+ return out2;
660
+ }
661
+ function range(a, b, step = 1) {
662
+ assert(!isUndefined(a) && !isUndefined(b) && !isUndefined(step), "You must pass two numbers and optionally a step value to the `range` function!");
663
+ assert(isNumber(a) && isNumber(b) && isNumber(step), "You must pass two numbers and optionally a step value to the `range` function!");
664
+ assert(step > 0, "The step value must be greater than 0! (NOTE: The step value is a magnitude; it does not indicate direction.)");
665
+ let shouldReverse = false;
666
+ const shouldIncludeBigInts = typeof a === "bigint" || typeof b === "bigint" || typeof step === "bigint";
667
+ a = Number(a);
668
+ b = Number(b);
669
+ step = Number(step);
670
+ if (a > b) {
671
+ shouldReverse = true;
672
+ const buffer = a;
673
+ a = b + step;
674
+ b = buffer + step;
675
+ }
676
+ let out2 = [];
677
+ for (let i = a; i < b; i += step) {
678
+ if (shouldIncludeBigInts) {
679
+ try {
680
+ out2.push(BigInt(i));
681
+ } catch (e) {
682
+ out2.push(i);
683
+ }
684
+ } else {
685
+ out2.push(i);
686
+ }
687
+ }
688
+ if (shouldReverse)
689
+ out2 = reverse(out2);
690
+ return out2;
691
+ }
692
+ function makeKey2(n) {
693
+ const alpha = "abcdefg1234567890";
694
+ let out2 = "";
695
+ while (out2.length < n)
696
+ out2 += alpha[Math.floor(Math.random() * alpha.length)];
697
+ return out2;
698
+ }
699
+ var NULL_KEY2 = makeKey2(256);
700
+ var UNDEFINED_KEY2 = makeKey2(256);
701
+ var INFINITY_KEY2 = makeKey2(256);
702
+ var MINUS_INFINITY_KEY2 = makeKey2(256);
703
+ var SYMBOL_KEY2 = makeKey2(256);
704
+ function set(arr) {
705
+ if (isDataFrame(arr) || isSeries(arr)) {
706
+ return set(arr.values);
707
+ }
708
+ assert(isArray(arr), "The `set` function only works on arrays, Series, and DataFrames!");
709
+ const out2 = [];
710
+ const temp = {};
711
+ flatten(arr).forEach((item) => {
712
+ const key = typeof item === "object" && item === null ? NULL_KEY2 : isUndefined(item) ? UNDEFINED_KEY2 : isFunction(item) ? item.toString() : typeof item === "symbol" ? item.toString() + " - " + SYMBOL_KEY2 : item === Infinity ? INFINITY_KEY2 : item === -Infinity ? MINUS_INFINITY_KEY2 : typeof item === "bigint" ? item.toString() : isDataFrame(item) ? item.toJSONString() : isSeries(item) ? JSON.stringify(item.toObject()) : JSON.stringify(item);
713
+ if (!temp[key])
714
+ out2.push(item);
715
+ temp[key] = true;
716
+ });
717
+ return out2;
718
+ }
719
+ function helper2(x) {
720
+ if (isArray(x)) {
721
+ const childShapes = helper2(x[0]);
722
+ return [x.length].concat(childShapes || []);
723
+ } else {
724
+ return void 0;
725
+ }
726
+ }
727
+ function shape(x) {
728
+ if (isDataFrame(x) || isSeries(x)) {
729
+ return shape(x.values);
730
+ }
731
+ assert(isArray(x), "The `shape` function only works on arrays, Series, and DataFrames!");
732
+ return helper2(x);
733
+ }
734
+ function dfAppend(df, x, axis) {
735
+ if (isUndefined(axis)) {
736
+ axis = 0;
737
+ }
738
+ assert(axis === 0 || axis === 1 || axis === "vertical" || axis === "horizontal", 'The only valid axis values for use when appending data to a DataFrame are 0, 1, "vertical", and "horizontal". Note that 0 == "horizontal" and 1 == "vertical".');
739
+ if (isArray(x)) {
740
+ assert(!isJagged(x), "The array of data you're trying to append to this DataFrame is jagged!");
741
+ const xShape = shape(x);
742
+ if (xShape.length === 1) {
743
+ if (axis === 0) {
744
+ const out2 = df.copy();
745
+ out2._values.push(x);
746
+ const maxRowLength = Math.max(df.shape[1], xShape[0]);
747
+ out2._values.forEach((row) => {
748
+ while (row.length < maxRowLength) {
749
+ row.push(void 0);
750
+ }
751
+ });
752
+ while (out2._index.length < out2._values.length) {
753
+ out2._index.push("row" + out2._index.length);
754
+ }
755
+ while (out2._columns.length < maxRowLength) {
756
+ out2._columns.push("col" + out2._columns.length);
757
+ }
758
+ return out2;
759
+ } else {
760
+ const maxColLength = Math.max(df.shape[0], xShape[0]);
761
+ const out2 = df.copy();
762
+ range(0, maxColLength).forEach((i) => {
763
+ if (i >= out2._values.length) {
764
+ out2._values.push(ndarray(df.shape[1]));
765
+ }
766
+ out2._values[i].push(x[i]);
767
+ });
768
+ while (out2._index.length < out2._values.length) {
769
+ out2._index.push("row" + out2._index.length);
770
+ }
771
+ while (out2._columns.length < out2._values[0].length) {
772
+ out2._columns.push("col" + out2._columns.length);
773
+ }
774
+ return out2;
775
+ }
776
+ } else if (xShape.length === 2) {
777
+ if (axis === 0) {
778
+ const maxRowLength = Math.max(...x.map((row) => row.length).concat([df.shape[1]]));
779
+ const out2 = df.copy();
780
+ out2._values = out2._values.concat(x).map((row) => {
781
+ while (row.length < maxRowLength) {
782
+ row.push(void 0);
783
+ }
784
+ return row;
785
+ });
786
+ while (out2._index.length < out2._values.length) {
787
+ out2._index.push("row" + out2._index.length);
788
+ }
789
+ while (out2._columns.length < maxRowLength) {
790
+ out2._columns.push("col" + out2._columns.length);
791
+ }
792
+ return out2;
793
+ } else {
794
+ const maxRowLength = Math.max(...x.map((row) => row.length)) + df.shape[1];
795
+ const maxColLength = Math.max(df.shape[0], xShape[0]);
796
+ const out2 = df.copy();
797
+ range(0, maxColLength).forEach((i) => {
798
+ if (i >= out2._values.length) {
799
+ out2._values.push(ndarray(df.shape[1]));
800
+ }
801
+ out2._values[i] = out2._values[i].concat(x[i]);
802
+ while (out2._values[i].length < maxRowLength) {
803
+ out2._values[i].push(void 0);
804
+ }
805
+ });
806
+ while (out2._index.length < out2._values.length) {
807
+ out2._index.push("row" + out2._index.length);
808
+ }
809
+ while (out2._columns.length < maxRowLength) {
810
+ out2._columns.push("col" + out2._columns.length);
811
+ }
812
+ return out2;
813
+ }
814
+ } else {
815
+ throw new MathError("Only 1- and 2-dimensional arrays can be appended to a DataFrame!");
816
+ }
817
+ } else if (isSeries(x)) {
818
+ const out2 = dfAppend(df, x.values, axis);
819
+ if (axis === 0) {
820
+ out2.index[out2.index.length - 1] = out2.index.indexOf(x.name) > -1 ? x.name + " (2)" : x.name;
821
+ } else {
822
+ out2.columns[out2.columns.length - 1] = out2.columns.indexOf(x.name) > -1 ? x.name + " (2)" : x.name;
823
+ }
824
+ return out2;
825
+ } else if (isDataFrame(x)) {
826
+ if (axis === 0) {
827
+ const out2 = df.copy();
828
+ const maxRowLength = set(out2._columns.concat(x._columns)).length;
829
+ out2._values.forEach((row) => {
830
+ while (row.length < maxRowLength) {
831
+ row.push(void 0);
832
+ }
833
+ });
834
+ x.apply((row) => {
835
+ const rowCopy = row.copy();
836
+ const temp = [];
837
+ out2._columns.forEach((col) => {
838
+ const index = rowCopy._index.indexOf(col);
839
+ if (index > -1) {
840
+ temp.push(rowCopy._values[index]);
841
+ rowCopy._values.splice(index, 1);
842
+ rowCopy._index.splice(index, 1);
843
+ } else {
844
+ temp.push(void 0);
845
+ }
846
+ });
847
+ out2._values.push(temp.concat(rowCopy._values));
848
+ }, 1);
849
+ out2._columns = out2._columns.concat(x._columns.filter((c) => out2._columns.indexOf(c) < 0));
850
+ while (out2._index.length < out2._values.length) {
851
+ const newRowName = "row" + out2._index.length;
852
+ out2._index.push(newRowName + (df._index.indexOf(newRowName) > -1 ? " (2)" : ""));
853
+ }
854
+ return out2;
855
+ } else {
856
+ const out2 = df.copy();
857
+ out2._index.forEach((rowName, i) => {
858
+ const xIndex = x._index.indexOf(rowName);
859
+ if (xIndex > -1) {
860
+ out2._values[i] = out2._values[i].concat(x._values[xIndex]);
861
+ } else {
862
+ out2._values[i] = out2._values[i].concat(ndarray(x.shape[1]));
863
+ }
864
+ });
865
+ x._index.forEach((rowName, i) => {
866
+ const outIndex = out2._index.indexOf(rowName);
867
+ if (outIndex < 0) {
868
+ out2._index.push(rowName);
869
+ out2._values.push(ndarray(out2._columns.length).concat(x._values[i]));
870
+ }
871
+ });
872
+ out2._columns = out2._columns.concat(x._columns.map((c) => c + (out2._columns.indexOf(c) > -1 ? " (2)" : "")));
873
+ return out2;
874
+ }
875
+ } else {
876
+ throw new MathError("Only 1- or 2-dimensional arrays, Series, and DataFrames can be appended to a DataFrame!");
877
+ }
878
+ }
879
+ function dfApply(DataFrame2, Series2, df, fn, axis) {
880
+ axis = axis || 0;
881
+ assert(isFunction(fn), "The first parameter to the `apply` method must be a function.");
882
+ assert(axis === 0 || axis === 1, "The second parameter to the `apply` method (the `axis`) must be 0 or 1.");
883
+ if (axis === 0) {
884
+ const temp = {};
885
+ let shouldReturnADataFrame;
886
+ df.columns.forEach((colName, i) => {
887
+ const series = new Series2(df.values.map((row) => row[i]));
888
+ series.name = colName;
889
+ series.index = df.index;
890
+ const value = fn(series, i, df);
891
+ if (value instanceof Series2) {
892
+ temp[colName] = value.values;
893
+ } else {
894
+ temp[colName] = value;
895
+ }
896
+ if (isUndefined(shouldReturnADataFrame)) {
897
+ shouldReturnADataFrame = value instanceof Series2 || isArray(value);
898
+ }
899
+ });
900
+ if (shouldReturnADataFrame) {
901
+ const out2 = new DataFrame2(temp);
902
+ out2.index = df.index;
903
+ return out2;
904
+ } else {
905
+ const out2 = new Series2(df.columns.map((colName) => temp[colName]));
906
+ out2.index = df.columns;
907
+ return out2;
908
+ }
909
+ } else if (axis === 1) {
910
+ let shouldReturnADataFrame;
911
+ const temp = df.values.map((row, i) => {
912
+ const series = new Series2(row);
913
+ series.name = df.index[i];
914
+ series.index = df.columns;
915
+ const value = fn(series, i, df);
916
+ if (isUndefined(shouldReturnADataFrame)) {
917
+ shouldReturnADataFrame = value instanceof Series2 || isArray(value);
918
+ }
919
+ if (value instanceof Series2) {
920
+ return value.values;
921
+ } else {
922
+ return value;
923
+ }
924
+ });
925
+ if (shouldReturnADataFrame) {
926
+ const out2 = new DataFrame2(temp);
927
+ out2.index = df.index;
928
+ out2.columns = df.columns;
929
+ return out2;
930
+ } else {
931
+ const out2 = new Series2(temp);
932
+ out2.index = df.index;
933
+ return out2;
934
+ }
935
+ }
936
+ }
937
+ function isString(s2) {
938
+ return typeof s2 === "string";
939
+ }
940
+ function dfAssign(DataFrame2, Series2, df, p1, p2) {
941
+ const isDataFrame2 = (x) => x instanceof DataFrame2;
942
+ const isSeries2 = (x) => x instanceof Series2;
943
+ if (!isUndefined(p2)) {
944
+ assert(isString(p1), "If passing two arguments into the `assign` method, then the first argument must be a string name!");
945
+ assert(isArray(p2) && !isJagged(p2) && shape(p2).length === 1, "If passing two arguments into the `assign` method, then the second argument must be a 1-dimensional array!");
946
+ const out2 = df.append(p2, 1);
947
+ out2.columns[out2.columns.length - 1] = p1;
948
+ return out2;
949
+ } else {
950
+ if (isDataFrame2(p1)) {
951
+ return df.append(p1, 1);
952
+ } else if (isSeries2(p1)) {
953
+ return df.append(p1, 1);
954
+ } else if (isObject(p1)) {
955
+ const maxColumnLength = Math.max(...Object.keys(p1).concat(Object.getOwnPropertySymbols(p1)).map((key) => p1[key].length));
956
+ Object.keys(p1).concat(Object.getOwnPropertySymbols(p1)).forEach((key) => {
957
+ while (p1[key].length < maxColumnLength) {
958
+ p1[key].push(void 0);
959
+ }
960
+ });
961
+ return df.append(new DataFrame2(p1), 1);
962
+ } else {
963
+ throw new MathError("You must pass a DataFrame, Series, or object into the `assign` method!");
964
+ }
965
+ }
966
+ }
967
+ function dfCopy(DataFrame2, df) {
968
+ if (df.isEmpty)
969
+ return new DataFrame2();
970
+ const out2 = new DataFrame2(copy(df.values));
971
+ out2.columns = df.columns.slice();
972
+ out2.index = df.index.slice();
973
+ return out2;
974
+ }
975
+ function dfDrop(DataFrame2, Series2, df, rows, cols) {
976
+ if (isUndefined(rows))
977
+ rows = [];
978
+ if (isUndefined(cols))
979
+ cols = [];
980
+ if (isString(rows) || isNumber(rows))
981
+ rows = [rows];
982
+ if (isString(cols) || isNumber(cols))
983
+ cols = [cols];
984
+ assert(isArray(rows), "The `drop` method only works on 1-dimensional arrays of numerical indices and/or strings.");
985
+ assert(isArray(cols), "The `drop` method only works on 1-dimensional arrays of numerical indices and/or strings.");
986
+ assert(shape(rows).length === 1, "The `drop` method only works on 1-dimensional arrays of numerical indices and/or strings.");
987
+ assert(shape(cols).length === 1, "The `drop` method only works on 1-dimensional arrays of numerical indices and/or strings.");
988
+ let outIndex, outColumns;
989
+ df.index.forEach((row, i) => {
990
+ if (rows.indexOf(row) < 0 && rows.indexOf(i) < 0) {
991
+ if (!outIndex)
992
+ outIndex = [];
993
+ outIndex.push(row);
994
+ }
995
+ });
996
+ df.columns.forEach((col, i) => {
997
+ if (cols.indexOf(col) < 0 && cols.indexOf(i) < 0) {
998
+ if (!outColumns)
999
+ outColumns = [];
1000
+ outColumns.push(col);
1001
+ }
1002
+ });
1003
+ let out2 = df.get(outIndex, outColumns);
1004
+ if (out2 instanceof Series2) {
1005
+ let temp = new DataFrame2();
1006
+ temp = temp.assign(out2);
1007
+ if (df.index.indexOf(out2.name) > -1)
1008
+ temp = temp.transpose();
1009
+ out2 = temp;
1010
+ }
1011
+ return out2;
1012
+ }
1013
+ function isInteger(x) {
1014
+ return isNumber(x) && (x >= 0 ? Math.floor(x) === x : Math.ceil(x) === x);
1015
+ }
1016
+ function isWholeNumber(x) {
1017
+ return isInteger(x) && x >= 0;
1018
+ }
1019
+ function dfDropMissing(DataFrame2, Series2, df, axis, condition, threshold) {
1020
+ axis = axis || 0;
1021
+ assert(axis === 0 || axis === 1, "The first parameter of the `dropMissing` method (the `axis`) must be 0 or 1.");
1022
+ threshold = threshold || 0;
1023
+ assert(isWholeNumber(threshold), "The third parameter of the `dropMissing` method (the `threshold`) should be a whole number (meaning that data should be dropped if it contains more than `threshold` null values).");
1024
+ condition = threshold > 0 ? "none" : condition || "any";
1025
+ assert(condition === "any" || condition === "all" || condition === "none", "The second parameter of the `dropMissing` method (the `condition` parameter, which indicates the condition under which data should be dropped) should be 'any' or 'all' (meaning that if 'any' of the data contains null values, then it should be dropped; or that if 'all' of the data contains null values, then it should be dropped).");
1026
+ function helper5(values) {
1027
+ if (threshold > 0) {
1028
+ let count2 = 0;
1029
+ for (let i = 0; i < values.length; i++) {
1030
+ const value = values[i];
1031
+ if (isUndefined(value))
1032
+ count2++;
1033
+ if (count2 >= threshold)
1034
+ return [];
1035
+ }
1036
+ } else if (condition === "any") {
1037
+ for (let i = 0; i < values.length; i++) {
1038
+ const value = values[i];
1039
+ if (isUndefined(value))
1040
+ return [];
1041
+ }
1042
+ } else if (condition === "all") {
1043
+ for (let i = 0; i < values.length; i++) {
1044
+ const value = values[i];
1045
+ if (!isUndefined(value))
1046
+ return values;
1047
+ }
1048
+ return [];
1049
+ }
1050
+ return values;
1051
+ }
1052
+ let out2 = df.copy();
1053
+ const tempID = Math.random().toString();
1054
+ if (axis === 0) {
1055
+ out2 = out2.assign(tempID, out2.index);
1056
+ const newValues = out2.values.map(helper5).filter((row) => row.length > 0);
1057
+ if (shape(newValues).length < 2)
1058
+ return new DataFrame2();
1059
+ out2.values = newValues;
1060
+ let newIndex = out2.get(null, tempID);
1061
+ if (isUndefined(newIndex))
1062
+ return new DataFrame2();
1063
+ if (isString(newIndex))
1064
+ newIndex = [newIndex];
1065
+ if (newIndex instanceof Series2)
1066
+ newIndex = newIndex.values;
1067
+ out2.index = newIndex;
1068
+ out2 = out2.drop(null, tempID);
1069
+ } else if (axis === 1) {
1070
+ const temp = {};
1071
+ out2.columns.forEach((colName, i) => {
1072
+ const values = out2.values.map((row) => row[i]);
1073
+ const newValues = helper5(values);
1074
+ if (newValues.length > 0) {
1075
+ temp[colName] = newValues;
1076
+ }
1077
+ });
1078
+ if (Object.keys(temp).length + Object.getOwnPropertySymbols(temp).length === 0) {
1079
+ return new DataFrame2();
1080
+ }
1081
+ const newOut = new DataFrame2(temp);
1082
+ newOut.index = out2.index;
1083
+ return newOut;
1084
+ }
1085
+ return out2;
1086
+ }
1087
+ function dropNaN(x) {
1088
+ if (isDataFrame(x) || isSeries(x)) {
1089
+ return x.dropNaN(...Object.values(arguments).slice(1));
1090
+ }
1091
+ assert(isArray(x), "The `dropNaN` function only works on arrays, Series, and DataFrames!");
1092
+ const out2 = [];
1093
+ x.forEach((v) => {
1094
+ try {
1095
+ return out2.push(dropNaN(v));
1096
+ } catch (e) {
1097
+ if (isNumber(v)) {
1098
+ return out2.push(v);
1099
+ }
1100
+ }
1101
+ });
1102
+ return out2;
1103
+ }
1104
+ function dfDropNaN(DataFrame2, df, axis, condition, threshold) {
1105
+ axis = axis || 0;
1106
+ assert(axis === 0 || axis === 1, "The first parameter of the `dropNaN` method (the `axis`) must be 0 or 1.");
1107
+ threshold = threshold || 0;
1108
+ assert(isWholeNumber(threshold), "The third parameter of the `dropNaN` method (the `threshold`) should be a whole number (meaning that data should be dropped if it contains more than `threshold` NaN values).");
1109
+ condition = threshold > 0 ? "none" : condition || "any";
1110
+ assert(condition === "any" || condition === "all" || condition === "none", "The second parameter of the `dropNaN` method (the `condition` parameter, which indicates the condition under which data should be dropped) should be 'any' or 'all' (meaning that if 'any' of the data contains NaN values, then it should be dropped; or that if 'all' of the data contains NaN values, then it should be dropped).");
1111
+ function helper5(values) {
1112
+ const numericalValues = dropNaN(values);
1113
+ if (threshold > 0)
1114
+ return values.length - numericalValues.length < threshold;
1115
+ if (condition === "any")
1116
+ return numericalValues.length === values.length;
1117
+ if (condition === "all")
1118
+ return numericalValues.length > 0;
1119
+ return true;
1120
+ }
1121
+ const out2 = df.copy();
1122
+ if (axis === 0) {
1123
+ const rowsToKeep = out2.index.filter((row) => {
1124
+ const values = out2.get(row, null).values;
1125
+ return helper5(values);
1126
+ });
1127
+ if (rowsToKeep.length > 0)
1128
+ return out2.get(rowsToKeep, null);
1129
+ else
1130
+ return new DataFrame2();
1131
+ } else if (axis === 1) {
1132
+ const colsToKeep = out2.columns.filter((col) => {
1133
+ const values = out2.get(null, col).values;
1134
+ return helper5(values);
1135
+ });
1136
+ if (colsToKeep.length > 0)
1137
+ return out2.get(null, colsToKeep);
1138
+ else
1139
+ return new DataFrame2();
1140
+ }
1141
+ return out2;
1142
+ }
1143
+ function arrayToObject(x) {
1144
+ const out2 = {};
1145
+ flatten(x).forEach((value, i) => {
1146
+ out2[value] = i;
1147
+ });
1148
+ return out2;
1149
+ }
1150
+ function undoArrayToObject(obj) {
1151
+ return Object.keys(obj).concat(Object.getOwnPropertySymbols(obj)).sort((a, b) => obj[a] - obj[b]);
1152
+ }
1153
+ function dfFilter(DataFrame2, Series2, df, fn, axis) {
1154
+ assert(isFunction(fn), "The `filter` method takes a single parameter: a function that is used to filter the values.");
1155
+ if (isUndefined(axis))
1156
+ axis = 0;
1157
+ assert(axis === 0 || axis === 1, "The `axis` parameter to the `filter` method must be 0 or 1.");
1158
+ let out2 = df.copy();
1159
+ if (out2.isEmpty)
1160
+ return out2;
1161
+ const index = arrayToObject(out2.index);
1162
+ const columns = arrayToObject(out2.columns);
1163
+ if (axis === 0) {
1164
+ let count2 = 0;
1165
+ const newValues = out2.values.filter((row, i) => {
1166
+ const series = new Series2(row);
1167
+ series.name = df.index[i];
1168
+ series.index = df.columns;
1169
+ const shouldKeep = fn(series, i, df);
1170
+ if (shouldKeep) {
1171
+ count2++;
1172
+ } else {
1173
+ delete index[out2.index[i]];
1174
+ }
1175
+ return shouldKeep;
1176
+ });
1177
+ if (count2 === 0) {
1178
+ return new DataFrame2();
1179
+ }
1180
+ if (count2 === 1) {
1181
+ const temp = new Series2(newValues[0]);
1182
+ temp.name = undoArrayToObject(index)[0];
1183
+ temp.index = undoArrayToObject(columns);
1184
+ return temp;
1185
+ }
1186
+ out2.values = newValues;
1187
+ out2.index = undoArrayToObject(index);
1188
+ } else if (axis === 1) {
1189
+ out2 = out2.transpose();
1190
+ let count2 = 0;
1191
+ const newValues = out2.values.filter((row, i) => {
1192
+ const series = new Series2(row);
1193
+ series.name = df.columns[i];
1194
+ series.index = df.index;
1195
+ const shouldKeep = fn(series, i, df);
1196
+ if (shouldKeep) {
1197
+ count2++;
1198
+ } else {
1199
+ delete columns[out2.index[i]];
1200
+ }
1201
+ return shouldKeep;
1202
+ });
1203
+ if (count2 === 0) {
1204
+ return new DataFrame2();
1205
+ }
1206
+ if (count2 === 1) {
1207
+ const temp = new Series2(newValues[0]);
1208
+ temp.name = undoArrayToObject(columns)[0];
1209
+ temp.index = undoArrayToObject(index);
1210
+ return temp;
1211
+ }
1212
+ out2.values = newValues;
1213
+ out2.index = undoArrayToObject(columns);
1214
+ out2 = out2.transpose();
1215
+ }
1216
+ return out2;
1217
+ }
1218
+ function dfGet(df, rows, cols) {
1219
+ if (isString(rows) || isNumber(rows))
1220
+ rows = [rows];
1221
+ if (isString(cols) || isNumber(cols))
1222
+ cols = [cols];
1223
+ for (const i in rows) {
1224
+ if (typeof rows[i] === "bigint") {
1225
+ rows[i] = Number(rows[i]);
1226
+ }
1227
+ }
1228
+ for (const i in cols) {
1229
+ if (typeof cols[i] === "bigint") {
1230
+ cols[i] = Number(cols[i]);
1231
+ }
1232
+ }
1233
+ const types = set((rows || []).concat(cols || []).map((v) => typeof v));
1234
+ assert(types.length <= 2, "Only whole numbers and/or strings are allowed in `get` arrays!");
1235
+ if (types.length === 1) {
1236
+ assert(types[0] === "string" || types[0] === "number", "Only whole numbers and/or strings are allowed in `get` arrays!");
1237
+ }
1238
+ if (types.length === 2) {
1239
+ assert(types.indexOf("string") > -1, "Only whole numbers and/or strings are allowed in `get` arrays!");
1240
+ assert(types.indexOf("number") > -1, "Only whole numbers and/or strings are allowed in `get` arrays!");
1241
+ }
1242
+ if (!isUndefined(rows)) {
1243
+ rows = rows.map((r) => {
1244
+ if (isString(r)) {
1245
+ assert(df.index.indexOf(r) > -1, `Row "${r}" does not exist!`);
1246
+ return r;
1247
+ }
1248
+ if (isNumber(r)) {
1249
+ assert(r >= 0, `Index ${r} is out of bounds!`);
1250
+ assert(Math.floor(r) === r, `Row numbers must be integers!`);
1251
+ assert(r < df.index.length, `Index ${r} is out of bounds!`);
1252
+ return df.index[r];
1253
+ }
1254
+ });
1255
+ }
1256
+ if (!isUndefined(cols)) {
1257
+ cols = cols.map((c) => {
1258
+ if (isString(c)) {
1259
+ assert(df.columns.indexOf(c) > -1, `Column "${c}" does not exist!`);
1260
+ return c;
1261
+ }
1262
+ if (isNumber(c)) {
1263
+ assert(c >= 0, `Column ${c} is out of bounds!`);
1264
+ assert(Math.floor(c) === c, `Column numbers must be integers!`);
1265
+ assert(c < df.columns.length, `Column ${c} is out of bounds!`);
1266
+ return df.columns[c];
1267
+ }
1268
+ });
1269
+ }
1270
+ return df.getSubsetByNames(rows, cols);
1271
+ }
1272
+ function alphaSort(a, b) {
1273
+ try {
1274
+ if (a < b)
1275
+ return -1;
1276
+ if (a > b)
1277
+ return 1;
1278
+ return 0;
1279
+ } catch (e) {
1280
+ a = typeof a === "object" && a !== null ? JSON.stringify(a) : a.toString();
1281
+ b = typeof b === "object" && b !== null ? JSON.stringify(b) : b.toString();
1282
+ if (a < b)
1283
+ return -1;
1284
+ if (a > b)
1285
+ return 1;
1286
+ return 0;
1287
+ }
1288
+ }
1289
+ function sort(arr, fn) {
1290
+ if (isUndefined(fn))
1291
+ fn = alphaSort;
1292
+ if (isDataFrame(arr) || isSeries(arr)) {
1293
+ return arr.sort(...Object.values(arguments).slice(1));
1294
+ }
1295
+ assert(isArray(arr), "The `sort` function only works on arrays, Series, and DataFrames!");
1296
+ assert(isFunction(fn), "The second parameter of the `sort` function must be a comparison function!");
1297
+ const out2 = arr.slice();
1298
+ out2.sort(fn);
1299
+ return out2;
1300
+ }
1301
+ function camelify(text) {
1302
+ const temp = text.toLowerCase();
1303
+ let out2 = "";
1304
+ for (let i = 0; i < temp.length; i++) {
1305
+ const char = temp[i];
1306
+ if (char.match(/[a-z0-9]/g)) {
1307
+ out2 += char;
1308
+ } else {
1309
+ out2 += " ";
1310
+ }
1311
+ }
1312
+ const words = out2.split(" ").filter((word) => word.length > 0);
1313
+ return words[0] + words.slice(1).map((word) => word[0].toUpperCase() + word.substring(1)).join("");
1314
+ }
1315
+ function dfGetDummies(DataFrame2, df, columns) {
1316
+ if (isUndefined(columns)) {
1317
+ columns = df.columns;
1318
+ } else if (isString(columns)) {
1319
+ columns = [columns];
1320
+ }
1321
+ const temp = {};
1322
+ columns.forEach((col) => {
1323
+ assert(isString(col), "You must pass either a string or a one-dimensional array of strings into the `getDummies` (AKA `oneHotEncode`) method!");
1324
+ const colIndex = df.columns.indexOf(col);
1325
+ assert(colIndex > -1, `The given DataFrame does not have a column called "${col}"!`);
1326
+ const values = df.values.map((row) => row[colIndex]);
1327
+ const valuesSet = sort(set(values));
1328
+ values.forEach((value) => {
1329
+ valuesSet.forEach((orig) => {
1330
+ const colName = col + "_" + camelify(orig.toString());
1331
+ if (!temp[colName]) {
1332
+ temp[colName] = [];
1333
+ }
1334
+ if (value === orig) {
1335
+ temp[colName].push(1);
1336
+ } else {
1337
+ temp[colName].push(0);
1338
+ }
1339
+ });
1340
+ });
1341
+ });
1342
+ const out2 = new DataFrame2(temp);
1343
+ out2.index = df.index;
1344
+ return out2;
1345
+ }
1346
+ function dfGetSubsetByIndices(df, rowIndices, colIndices) {
1347
+ const dataShape = df.shape;
1348
+ if (isUndefined(rowIndices))
1349
+ rowIndices = range(0, dataShape[0]);
1350
+ if (isUndefined(colIndices))
1351
+ colIndices = range(0, dataShape[1]);
1352
+ if (isNumber(rowIndices))
1353
+ rowIndices = [rowIndices];
1354
+ if (isNumber(colIndices))
1355
+ colIndices = [colIndices];
1356
+ assert(isArray(rowIndices) && isArray(colIndices), "The `rowIndices` and `colIndices` parameters must be 1-dimensional arrays of whole numbers.");
1357
+ assert(shape(rowIndices).length === 1 && shape(colIndices).length === 1, "The `rowIndices` and `colIndices` parameters must be 1-dimensional arrays of whole numbers.");
1358
+ assert(rowIndices.length > 0, "The `rowIndices` array must contain at least one index.");
1359
+ assert(colIndices.length > 0, "The `colIndices` array must contain at least one index.");
1360
+ rowIndices.forEach((rowIndex) => {
1361
+ assert(isWholeNumber(rowIndex), "The `rowIndices` and `colIndices` parameters must be 1-dimensional arrays of whole numbers.");
1362
+ assert(rowIndex < df.index.length, `The row index ${rowIndex} is out of bounds.`);
1363
+ });
1364
+ colIndices.forEach((colIndex) => {
1365
+ assert(isWholeNumber(colIndex), "The `rowIndices` and `colIndices` parameters must be 1-dimensional arrays of whole numbers.");
1366
+ assert(colIndex < df.columns.length, `The column index ${colIndex} is out of bounds.`);
1367
+ });
1368
+ const rows = rowIndices.map((i) => df.index[i]);
1369
+ const cols = colIndices.map((i) => df.columns[i]);
1370
+ return df.getSubsetByNames(rows, cols);
1371
+ }
1372
+ function dfGetSubsetByNames(DataFrame2, Series2, df, rows, cols) {
1373
+ if (isUndefined(rows))
1374
+ rows = df.index;
1375
+ if (isUndefined(cols))
1376
+ cols = df.columns;
1377
+ if (isString(rows))
1378
+ rows = [rows];
1379
+ if (isString(cols))
1380
+ cols = [cols];
1381
+ assert(isArray(rows) && isArray(cols), "The `rows` and `cols` parameters must be 1-dimensional arrays of strings.");
1382
+ assert(shape(rows).length === 1 && shape(cols).length === 1, "The `rows` and `cols` parameters must be 1-dimensional arrays of strings.");
1383
+ assert(rows.length > 0, "The `rows` array must contain at least one row name.");
1384
+ assert(cols.length > 0, "The `cols` array must contain at least one column name.");
1385
+ rows.forEach((row) => {
1386
+ assert(isString(row), "The `rows` and `cols` parameters must be 1-dimensional arrays of strings.");
1387
+ assert(df.index.indexOf(row) > -1, `The row name "${row}" does not exist in the list of rows.`);
1388
+ });
1389
+ cols.forEach((col) => {
1390
+ assert(isString(col), "The `rows` and `cols` parameters must be 1-dimensional arrays of strings.");
1391
+ assert(df.columns.indexOf(col) > -1, `The column name "${col}" does not exist in the list of columns.`);
1392
+ });
1393
+ const values = rows.map((row) => {
1394
+ return cols.map((col) => {
1395
+ return df.values[df.index.indexOf(row)][df.columns.indexOf(col)];
1396
+ });
1397
+ });
1398
+ if (rows.length === 1 && cols.length === 1) {
1399
+ return values[0][0];
1400
+ }
1401
+ if (rows.length === 1) {
1402
+ const out3 = new Series2(values[0]);
1403
+ out3.name = rows[0];
1404
+ out3.index = cols;
1405
+ return out3;
1406
+ }
1407
+ if (cols.length === 1) {
1408
+ const out3 = new Series2(values.map((v) => v[0]));
1409
+ out3.name = cols[0];
1410
+ out3.index = rows;
1411
+ return out3;
1412
+ }
1413
+ const out2 = new DataFrame2(values);
1414
+ out2.columns = cols;
1415
+ out2.index = rows;
1416
+ return out2;
1417
+ }
1418
+ function dfPrint(DataFrame2, Series2, df) {
1419
+ function truncate(s2, maxLength2) {
1420
+ if (isString(s2)) {
1421
+ if (s2.length > maxLength2) {
1422
+ return s2.substring(0, maxLength2 - 3) + "...";
1423
+ } else {
1424
+ return s2;
1425
+ }
1426
+ } else {
1427
+ return s2;
1428
+ }
1429
+ }
1430
+ if (df.isEmpty) {
1431
+ console.table({});
1432
+ console.log("Shape:", [0, 0], "\n");
1433
+ return df;
1434
+ }
1435
+ const maxRows = typeof window === "undefined" ? 20 : 10;
1436
+ const halfMaxRows = Math.floor(maxRows / 2);
1437
+ const maxColumns = typeof process === "undefined" ? 10 : Math.floor(process.stdout.columns / 24) - 1;
1438
+ const halfMaxColumns = Math.floor(maxColumns / 2);
1439
+ const tempRows = maxRows > df.index.length ? null : range(0, halfMaxRows).concat(range(df.index.length - halfMaxRows, df.index.length));
1440
+ const tempColumns = maxColumns > df.columns.length ? null : range(0, halfMaxColumns).concat(range(df.columns.length - halfMaxColumns, df.columns.length));
1441
+ let temp = df.get(tempRows, tempColumns);
1442
+ if (temp instanceof Series2) {
1443
+ if (df.shape[0] === 1) {
1444
+ temp = new DataFrame2([temp.values]);
1445
+ temp.index = df.index;
1446
+ temp.columns = new Series2(df.columns).get(tempColumns).values;
1447
+ } else if (df.shape[1] === 1) {
1448
+ temp = new DataFrame2([temp.values]).transpose();
1449
+ temp.index = new Series2(df.index).get(tempRows).values;
1450
+ temp.columns = df.columns;
1451
+ }
1452
+ }
1453
+ if (maxRows <= df.index.length) {
1454
+ temp._index.splice(halfMaxRows, 0, "...");
1455
+ temp._values.splice(halfMaxRows, 0, range(0, temp.columns.length).map(() => "..."));
1456
+ }
1457
+ if (maxColumns <= df.columns.length) {
1458
+ temp._columns.splice(halfMaxColumns, 0, "...");
1459
+ temp._values = temp._values.map((row) => {
1460
+ row.splice(halfMaxColumns, 0, "...");
1461
+ return row;
1462
+ });
1463
+ }
1464
+ const maxLength = 28;
1465
+ if (temp instanceof Series2) {
1466
+ temp.values = temp.values.map((value) => truncate(value, maxLength));
1467
+ temp.name = truncate(temp.name, maxLength);
1468
+ temp.index = temp.index.map((row) => truncate(row, maxLength));
1469
+ } else {
1470
+ temp.values = temp.values.map((row) => {
1471
+ return row.map((value) => truncate(value, maxLength));
1472
+ });
1473
+ temp.columns = temp.columns.map((col) => truncate(col, maxLength));
1474
+ temp.index = temp.index.map((row) => truncate(row, maxLength));
1475
+ }
1476
+ console.table(temp.toDetailedObject());
1477
+ console.log("Shape:", df.shape, "\n");
1478
+ return df;
1479
+ }
1480
+ function leftPad(x, maxLength) {
1481
+ assert(isNumber(x), "The `leftPad` function only works on numbers!");
1482
+ let out2 = x.toString();
1483
+ while (out2.length < maxLength)
1484
+ out2 = "0" + out2;
1485
+ return out2;
1486
+ }
1487
+ function dfResetIndex(df, shouldSkipCopying) {
1488
+ const out2 = shouldSkipCopying ? df : df.copy();
1489
+ out2.index = range(0, df.shape[0]).map((i) => {
1490
+ return "row" + leftPad(i, (out2.index.length - 1).toString().length);
1491
+ });
1492
+ return out2;
1493
+ }
1494
+ function product(arr, shouldDropNaNs) {
1495
+ if (isDataFrame(arr) || isSeries(arr)) {
1496
+ return product(arr.values, shouldDropNaNs);
1497
+ }
1498
+ assert(isArray(arr), "The `product` function only works on arrays, Series, and DataFrames!");
1499
+ try {
1500
+ if (arr.length === 0)
1501
+ return NaN;
1502
+ const temp = flatten(arr);
1503
+ let resultShouldBeABigInt = false;
1504
+ let out2 = 1;
1505
+ for (let v of temp) {
1506
+ if (!isNumber(v)) {
1507
+ if (shouldDropNaNs) {
1508
+ v = 1;
1509
+ } else {
1510
+ return NaN;
1511
+ }
1512
+ }
1513
+ if (typeof v === "bigint") {
1514
+ resultShouldBeABigInt = true;
1515
+ v = Number(v);
1516
+ }
1517
+ out2 *= v;
1518
+ }
1519
+ if (resultShouldBeABigInt) {
1520
+ try {
1521
+ return BigInt(out2);
1522
+ } catch (e) {
1523
+ }
1524
+ }
1525
+ return out2;
1526
+ } catch (e) {
1527
+ return NaN;
1528
+ }
1529
+ }
1530
+ function isNaturalNumber(x) {
1531
+ return isInteger(x) && x > 0;
1532
+ }
1533
+ function reshape(x, newShape) {
1534
+ if (isDataFrame(x) || isSeries(x)) {
1535
+ return reshape(x.values, newShape);
1536
+ }
1537
+ assert(isArray(x), "The first argument passed into the `reshape` function must be an array!");
1538
+ if (isNumber(newShape))
1539
+ newShape = [newShape];
1540
+ assert(isArray(newShape), "The second argument passed into the `reshape` function must be a whole number or a one-dimensional array of whole numbers!");
1541
+ assert(shape(newShape).length === 1, "The first argument passed into the `reshape` function must be a whole number or a one-dimensional array of whole numbers!");
1542
+ newShape = newShape.map((v) => {
1543
+ if (typeof v === "bigint") {
1544
+ v = Number(v);
1545
+ }
1546
+ assert(isNaturalNumber(v), "The first argument passed into the `reshape` function must be a whole number or a one-dimensional array of whole numbers!");
1547
+ return Number(v);
1548
+ });
1549
+ if (newShape.length === 0) {
1550
+ return flatten(x);
1551
+ }
1552
+ const temp = flatten(x);
1553
+ if (newShape.length === 1 && newShape[0] === temp.length) {
1554
+ return temp;
1555
+ }
1556
+ assert(product(newShape) === temp.length, "The new shape doesn't match the number of values available in `x` (the first argument passed into the `reshape` function)!");
1557
+ const out2 = [];
1558
+ const step = Math.floor(temp.length / newShape[0]);
1559
+ for (let i = 0; i < newShape[0]; i++) {
1560
+ const row = temp.slice(i * step, (i + 1) * step);
1561
+ out2.push(reshape(row, newShape.slice(1)));
1562
+ }
1563
+ return out2;
1564
+ }
1565
+ var MAX = Math.pow(2, 64);
1566
+ var s = [];
1567
+ seed(Math.floor(Math.random() * MAX));
1568
+ function splitmix64(state, n) {
1569
+ state = uint(state);
1570
+ function helper5() {
1571
+ state += uint("0x9e3779b97f4a7c15");
1572
+ let z = copy(state);
1573
+ z = (z ^ z >> BigInt(30)) * uint("0xbf58476d1ce4e5b9");
1574
+ z = (z ^ z >> BigInt(27)) * uint("0x94d049bb133111eb");
1575
+ return z ^ z >> BigInt(31);
1576
+ }
1577
+ const out2 = [];
1578
+ for (let i = 0; i < n; i++)
1579
+ out2.push(helper5());
1580
+ return out2;
1581
+ }
1582
+ function uint(x) {
1583
+ return BigInt.asUintN(64, BigInt(x));
1584
+ }
1585
+ function rotl(x, k) {
1586
+ x = uint(x);
1587
+ k = BigInt(k);
1588
+ return uint(uint(x << k) | uint(x >> uint(BigInt(64) - k)));
1589
+ }
1590
+ function seed(val) {
1591
+ if (typeof val === "bigint") {
1592
+ val = Number(val);
1593
+ }
1594
+ if (!isUndefined(val)) {
1595
+ assert(isNumber(val), "If passing a value into the `seed` function, then that value must be an integer!");
1596
+ const temp = splitmix64(Math.floor(val), 4);
1597
+ s[0] = temp[0];
1598
+ s[1] = temp[1];
1599
+ s[2] = temp[2];
1600
+ s[3] = temp[3];
1601
+ } else {
1602
+ return copy(s);
1603
+ }
1604
+ }
1605
+ function next() {
1606
+ const result = uint(rotl(s[0] + s[3], 23) + s[0]);
1607
+ const t = uint(s[1] << BigInt(17));
1608
+ s[2] = uint(s[2] ^ s[0]);
1609
+ s[3] = uint(s[3] ^ s[1]);
1610
+ s[1] = uint(s[1] ^ s[2]);
1611
+ s[0] = uint(s[0] ^ s[3]);
1612
+ s[2] = uint(s[2] ^ t);
1613
+ s[3] = rotl(s[3], 45);
1614
+ return Math.floor(Number(result)) / MAX;
1615
+ }
1616
+ function random(shape2) {
1617
+ if (isUndefined(shape2))
1618
+ return next();
1619
+ if (!isArray(shape2))
1620
+ shape2 = [shape2];
1621
+ return reshape(ndarray(product(shape2)).map(next), shape2);
1622
+ }
1623
+ function shuffle(arr) {
1624
+ if (isDataFrame(arr) || isSeries(arr)) {
1625
+ return arr.shuffle(...Object.values(arguments).slice(1));
1626
+ }
1627
+ assert(isArray(arr), "The `shuffle` function only works on arrays, Series, and DataFrames!");
1628
+ const out2 = [];
1629
+ const temp = arr.slice();
1630
+ for (let i = 0; i < arr.length; i++) {
1631
+ const index = Math.floor(random() * temp.length);
1632
+ out2.push(temp.splice(index, 1)[0]);
1633
+ }
1634
+ return out2;
1635
+ }
1636
+ function dfShuffle(df, axis) {
1637
+ if (isUndefined(axis))
1638
+ axis = 0;
1639
+ assert(axis === 0 || axis === 1, "The `axis` parameter to the `shuffle` must be 0, 1, or undefined.");
1640
+ return df.get(axis === 0 ? shuffle(df.index) : null, axis === 1 ? shuffle(df.columns) : null);
1641
+ }
1642
+ function isBoolean(x) {
1643
+ return typeof x === "boolean";
1644
+ }
1645
+ function dfSort(df, a, b) {
1646
+ if (isFunction(a)) {
1647
+ return dfSortByFunction(df, a, b);
1648
+ } else {
1649
+ return dfSortByColumns(df, a, b);
1650
+ }
1651
+ }
1652
+ function dfSortByFunction(df, fn, axis) {
1653
+ axis = isUndefined(axis) ? 0 : axis;
1654
+ assert(isFunction(fn), "When sorting a DataFrame using a function, the first argument to the `sort` method must be a function!");
1655
+ assert(isNumber(axis), "When sorting a DataFrame using a function, the second argument to the `sort` method must be null, undefined, 0, or 1 to indicate the axis along which the data should be sorted! An axis of 0 means that the rows will be sorted relative to each other, whereas an axis of 1 means that the columns will be sorted relative to each other.");
1656
+ if (axis === 0) {
1657
+ const index = sort(df.index, (a, b) => {
1658
+ return fn(df.get(a, null), df.get(b, null));
1659
+ });
1660
+ return df.get(index, null);
1661
+ } else {
1662
+ const columns = sort(df.columns, (a, b) => {
1663
+ return fn(df.get(null, a), df.get(null, b));
1664
+ });
1665
+ return df.get(null, columns);
1666
+ }
1667
+ }
1668
+ function dfSortByColumns(df, cols, directions) {
1669
+ let out2 = df.copy();
1670
+ const indexID = random().toString();
1671
+ out2 = out2.assign(indexID, out2.index);
1672
+ if (isUndefined(cols)) {
1673
+ cols = [indexID];
1674
+ directions = [true];
1675
+ }
1676
+ if (isNumber(cols) || isString(cols)) {
1677
+ cols = [cols];
1678
+ if (isBoolean(directions) || isString(directions))
1679
+ directions = [directions];
1680
+ }
1681
+ assert(isArray(cols), "The first parameter of the `sort` method must be (1) a string or index representing a column name or index, respectively; (2) a 1-dimensional array of strings and/or indices; or (3) null.");
1682
+ assert(shape(cols).length === 1, "The first parameter of the `sort` method must be (1) a string or index representing a column name or index, respectively; (2) a 1-dimensional array of strings and/or indices; or (3) null.");
1683
+ if (isUndefined(directions))
1684
+ directions = range(0, cols.length).map(() => true);
1685
+ assert(isArray(directions), "The second parameter of the `sort` method must be (1) a string or boolean representing the sort direction ('ascending' / 'descending', or true / false); (2) a 1-dimensional array of strings and/or booleans; or (3) null.");
1686
+ assert(shape(directions).length === 1, "The second parameter of the `sort` method must be (1) a string or boolean representing the sort direction ('ascending' / 'descending', or true / false); (2) a 1-dimensional array of strings and/or booleans; or (3) null.");
1687
+ assert(cols.length === directions.length, "The arrays passed into the `sort` method must be equal in length.");
1688
+ cols = cols.map((col) => {
1689
+ assert(isString(col) || isNumber(col), "Column references can either be column names (as strings) or column indices (as whole numbers).");
1690
+ if (isString(col)) {
1691
+ const index = out2.columns.indexOf(col);
1692
+ assert(index > -1, `The column "${col}" does not exist!`);
1693
+ return index;
1694
+ }
1695
+ if (isNumber(col)) {
1696
+ assert(isWholeNumber(col), "Column indices must be whole numbers!");
1697
+ assert(col < out2.columns.length, `The index ${col} is out of bounds!`);
1698
+ return col;
1699
+ }
1700
+ });
1701
+ directions = directions.map((dir) => {
1702
+ assert(isString(dir) || isBoolean(dir), "Direction references can either be strings ('ascending' or 'descending') or booleans (true or false).");
1703
+ if (isString(dir)) {
1704
+ const value = dir.trim().toLowerCase();
1705
+ assert(value === "ascending" || value === "descending", "Direction references can either be strings ('ascending' or 'descending') or booleans (true or false).");
1706
+ return value === "ascending";
1707
+ }
1708
+ if (isBoolean(dir)) {
1709
+ return dir;
1710
+ }
1711
+ });
1712
+ out2.values = sort(out2.values, (a, b) => {
1713
+ let counter = 0;
1714
+ while (a[cols[counter]] === b[cols[counter]] && counter < cols.length) {
1715
+ counter++;
1716
+ }
1717
+ const isAscending = directions[counter];
1718
+ if (a[cols[counter]] === b[cols[counter]])
1719
+ return 0;
1720
+ if (a[cols[counter]] < b[cols[counter]])
1721
+ return isAscending ? -1 : 1;
1722
+ if (a[cols[counter]] > b[cols[counter]])
1723
+ return isAscending ? 1 : -1;
1724
+ });
1725
+ const indexNumber = out2.columns.indexOf(indexID);
1726
+ out2.index = out2.values.map((row) => row[indexNumber]);
1727
+ out2 = out2.dropColumns(indexID);
1728
+ return out2;
1729
+ }
1730
+ function dfToDetailedObject(df, axis) {
1731
+ if (isUndefined(axis)) {
1732
+ axis = 0;
1733
+ } else {
1734
+ assert(axis === 0 || axis === 1, "The axis parameter of the `toDetailedObject` method must be undefined, 0, or 1. An axis of 0 indicates that the returned object should be organized first by rows and then by columns. An axis of 1 indicates that the returned object should be organized first by columns and then by rows.");
1735
+ }
1736
+ const out2 = {};
1737
+ if (axis === 0) {
1738
+ df.index.forEach((rowName, i) => {
1739
+ const temp = {};
1740
+ df.columns.forEach((colName, j) => {
1741
+ temp[colName] = df.values[i][j];
1742
+ });
1743
+ out2[rowName] = temp;
1744
+ });
1745
+ } else {
1746
+ df.columns.forEach((colName, j) => {
1747
+ const temp = {};
1748
+ df.index.forEach((rowName, i) => {
1749
+ temp[rowName] = df.values[i][j];
1750
+ });
1751
+ out2[colName] = temp;
1752
+ });
1753
+ }
1754
+ return out2;
1755
+ }
1756
+ function dfToJSONString(df, axis) {
1757
+ return JSON.stringify(df.toObject(axis));
1758
+ }
1759
+ async function dfToJSON(df, filename, axis) {
1760
+ const out2 = dfToJSONString(df, axis);
1761
+ let downloadedInBrowser = false;
1762
+ let wroteToDiskInNode = false;
1763
+ let browserError, nodeError;
1764
+ try {
1765
+ let newFilename = filename;
1766
+ if (filename.includes("/")) {
1767
+ const parts = filename.split("/");
1768
+ newFilename = parts[parts.length - 1];
1769
+ }
1770
+ const a = document.createElement("a");
1771
+ a.href = `data:application/json;charset=utf-8,${encodeURIComponent(out2)}`;
1772
+ a.download = newFilename;
1773
+ a.dispatchEvent(new MouseEvent("click"));
1774
+ downloadedInBrowser = true;
1775
+ } catch (e) {
1776
+ browserError = e;
1777
+ }
1778
+ try {
1779
+ const fs = await import("node:fs");
1780
+ const path = await import("node:path");
1781
+ fs.writeFileSync(path.resolve(filename), out2, "utf8");
1782
+ wroteToDiskInNode = true;
1783
+ } catch (e) {
1784
+ nodeError = e;
1785
+ }
1786
+ if (!downloadedInBrowser && !wroteToDiskInNode) {
1787
+ if (typeof window !== "undefined") {
1788
+ throw new MathError(browserError);
1789
+ } else if (typeof module !== "undefined") {
1790
+ throw new MathError(nodeError);
1791
+ } else {
1792
+ throw new MathError("I don't know what's going wrong, but it doesn't seem like you're in Node or the browser, and we couldn't download and/or write the file to disk!");
1793
+ }
1794
+ }
1795
+ return df;
1796
+ }
1797
+ function dfToObject(df) {
1798
+ const out2 = {};
1799
+ df.columns.forEach((col) => {
1800
+ out2[col] = df.get(col).values;
1801
+ });
1802
+ return out2;
1803
+ }
1804
+ function transpose(arr) {
1805
+ if (isDataFrame(arr) || isSeries(arr)) {
1806
+ return arr.transpose();
1807
+ }
1808
+ assert(isArray(arr), "The `transpose` function only works on arrays, Series, and DataFrames!");
1809
+ const theShape = shape(arr);
1810
+ assert(theShape.length <= 2, "I'm not smart enough to know how to transpose arrays that have more than 2 dimensions. Sorry for the inconvenience! Please only pass 1- or 2-dimensional arrays into the `transpose` function!");
1811
+ if (theShape.length === 1) {
1812
+ return reverse(arr);
1813
+ } else if (theShape.length === 2) {
1814
+ const out2 = ndarray(reverse(theShape));
1815
+ for (let row = 0; row < theShape[0]; row++) {
1816
+ for (let col = 0; col < theShape[1]; col++) {
1817
+ out2[col][row] = arr[row][col];
1818
+ }
1819
+ }
1820
+ return out2;
1821
+ }
1822
+ }
1823
+ function seriesAppend(Series2, series, x) {
1824
+ if (isSeries(x)) {
1825
+ return new Series2(series.values.concat(x.values));
1826
+ }
1827
+ if (isArray(x)) {
1828
+ const xShape = shape(x);
1829
+ assert(xShape.length === 1 && !isNested(xShape), "Only vectors can be appended to Series!");
1830
+ const out2 = series.copy();
1831
+ x.forEach((v, i) => {
1832
+ out2._values.push(v);
1833
+ out2._index.push("item" + (series.values.length + i));
1834
+ });
1835
+ return out2;
1836
+ }
1837
+ return seriesAppend(series, [x]);
1838
+ }
1839
+ function seriesApply(series, fn) {
1840
+ assert(isFunction(fn), "The parameter to the `apply` method must be a function.");
1841
+ const out2 = series.copy();
1842
+ out2._values = out2._values.map((v, i) => fn(v, i));
1843
+ return out2;
1844
+ }
1845
+ function seriesDropMissing(series) {
1846
+ const out2 = series.copy();
1847
+ const outIndex = [];
1848
+ out2._values = out2.values.filter((v, i) => {
1849
+ if (isUndefined(v)) {
1850
+ return false;
1851
+ } else {
1852
+ outIndex.push(out2.index[i]);
1853
+ return true;
1854
+ }
1855
+ });
1856
+ out2._index = outIndex;
1857
+ return out2;
1858
+ }
1859
+ function seriesDropNaN(Series2, series) {
1860
+ const index = [];
1861
+ const values = [];
1862
+ series.values.forEach((value, i) => {
1863
+ if (isNumber(value)) {
1864
+ values.push(value);
1865
+ index.push(series.index[i]);
1866
+ }
1867
+ });
1868
+ const out2 = new Series2(values);
1869
+ out2.name = series.name;
1870
+ out2.index = index;
1871
+ return out2;
1872
+ }
1873
+ function seriesFilter(Series2, series, fn) {
1874
+ let out2 = series.copy();
1875
+ const index = copy(out2.index);
1876
+ const indicesToRemove = [];
1877
+ const newValues = out2.values.filter((value, i) => {
1878
+ const shouldKeep = fn(value, i, out2.values);
1879
+ if (!shouldKeep)
1880
+ indicesToRemove.push(out2.index[i]);
1881
+ return shouldKeep;
1882
+ });
1883
+ indicesToRemove.forEach((i) => {
1884
+ index.splice(index.indexOf(i), 1);
1885
+ });
1886
+ if (newValues.length === 0) {
1887
+ out2 = new Series2();
1888
+ out2.name = series.name;
1889
+ return out2;
1890
+ }
1891
+ out2.values = newValues;
1892
+ out2.index = index;
1893
+ return out2;
1894
+ }
1895
+ function seriesGet(series, indices) {
1896
+ if (isString(indices) || isNumber(indices))
1897
+ indices = [indices];
1898
+ for (const i in indices) {
1899
+ if (typeof indices[i] === "bigint") {
1900
+ indices[i] = Number(indices[i]);
1901
+ }
1902
+ }
1903
+ const types = set((indices || []).map((v) => typeof v));
1904
+ assert(types.length <= 2, "Only whole numbers and/or strings are allowed in `get` arrays!");
1905
+ if (types.length === 1) {
1906
+ assert(types[0] === "string" || types[0] === "number", "Only whole numbers and/or strings are allowed in `get` arrays!");
1907
+ }
1908
+ if (types.length === 2) {
1909
+ assert(types.indexOf("string") > -1, "Only whole numbers and/or strings are allowed in `get` arrays!");
1910
+ assert(types.indexOf("number") > -1, "Only whole numbers and/or strings are allowed in `get` arrays!");
1911
+ }
1912
+ if (!isUndefined(indices)) {
1913
+ indices = indices.map((i) => {
1914
+ if (typeof i === "string") {
1915
+ assert(series.index.indexOf(i) > -1, `Index "${i}" does not exist!`);
1916
+ return i;
1917
+ }
1918
+ if (typeof i === "number") {
1919
+ assert(i >= 0, `Index ${i} is out of bounds!`);
1920
+ assert(Math.floor(i) === i, `Indices must be integers!`);
1921
+ assert(i < series.index.length, `Index ${i} is out of bounds!`);
1922
+ return series.index[i];
1923
+ }
1924
+ });
1925
+ }
1926
+ return series.getSubsetByNames(indices);
1927
+ }
1928
+ function seriesGetSubsetByIndices(series, indices) {
1929
+ const dataShape = series.shape;
1930
+ if (isUndefined(indices))
1931
+ indices = range(0, dataShape[0]);
1932
+ assert(isArray(indices), "The `indices` array must be 1-dimensional array of whole numbers.");
1933
+ assert(shape(indices).length === 1, "The `indices` array must be a 1-dimensional array of whole numbers.");
1934
+ assert(indices.length > 0, "The `indices` array must contain at least one index.");
1935
+ indices.forEach((index) => {
1936
+ assert(isWholeNumber(index), "The `indices` array must be a 1-dimensional array of whole numbers.");
1937
+ assert(index < series.index.length, `The row index ${index} is out of bounds.`);
1938
+ });
1939
+ const rows = indices.map((i) => series.index[i]);
1940
+ return series.getSubsetByNames(rows);
1941
+ }
1942
+ function seriesGetSubsetByNames(Series2, series, indices) {
1943
+ if (isUndefined(indices))
1944
+ indices = series.index;
1945
+ assert(isArray(indices), "The `indices` array must be a 1-dimensional array of strings.");
1946
+ assert(shape(indices).length === 1, "The `indices` array must be a 1-dimensional array of strings.");
1947
+ assert(indices.length > 0, "The `indices` array must contain at least one index name.");
1948
+ indices.forEach((name) => {
1949
+ assert(isString(name), "The `indices` array must contain only strings.");
1950
+ assert(series.index.indexOf(name) > -1, `The name "${name}" does not exist in the index.`);
1951
+ });
1952
+ const values = indices.map((name) => {
1953
+ return series.values[series.index.indexOf(name)];
1954
+ });
1955
+ if (values.length === 1)
1956
+ return values[0];
1957
+ const out2 = new Series2(values);
1958
+ out2.index = indices;
1959
+ out2.name = series.name;
1960
+ return out2;
1961
+ }
1962
+ function seriesPrint(series) {
1963
+ let temp = series.copy();
1964
+ const maxRows = typeof window === "undefined" ? 20 : 10;
1965
+ if (temp.index.length > maxRows) {
1966
+ temp = temp.get(range(0, maxRows / 2).concat(range(temp.index.length - maxRows / 2, temp.index.length)));
1967
+ const tempIndex = copy(temp.index);
1968
+ tempIndex.splice(Math.floor(tempIndex.length / 2), 0, "...");
1969
+ temp.values.push("...");
1970
+ temp.index.push("...");
1971
+ temp = temp.get(tempIndex);
1972
+ }
1973
+ const out2 = {};
1974
+ temp.values.forEach((value, i) => {
1975
+ const obj = {};
1976
+ obj[temp.name] = value;
1977
+ out2[temp.index[i]] = obj;
1978
+ });
1979
+ console.table(out2);
1980
+ console.log("Shape:", series.shape, "\n");
1981
+ return series;
1982
+ }
1983
+ function seriesShuffle(series) {
1984
+ const out2 = series.copy();
1985
+ return out2.get(shuffle(out2.index));
1986
+ }
1987
+ function seriesSort(Series2, series, fn) {
1988
+ fn = fn || ((a, b) => a < b ? -1 : 1);
1989
+ assert(isUndefined(fn) || isFunction(fn), "You must pass undefined, null, or a comparison function as the second argument to the `sort` method!");
1990
+ const pairs = transpose([series.values, series.index]);
1991
+ const temp = sort(pairs, (aPair, bPair) => {
1992
+ return fn(aPair[0], bPair[0]);
1993
+ });
1994
+ const newValues = [];
1995
+ const newIndex = [];
1996
+ temp.forEach((pair) => {
1997
+ newValues.push(pair[0]);
1998
+ newIndex.push(pair[1]);
1999
+ });
2000
+ const out2 = new Series2();
2001
+ out2._values = newValues;
2002
+ out2._index = newIndex;
2003
+ out2.name = series.name;
2004
+ return out2;
2005
+ }
2006
+ function seriesSortByIndex(Series2, series) {
2007
+ let temp = transpose([series.values, series.index]);
2008
+ temp = transpose(sort(temp, (a, b) => {
2009
+ if (a[1] === b[1])
2010
+ return 0;
2011
+ if (a[1] < b[1])
2012
+ return -1;
2013
+ if (a[1] > b[1])
2014
+ return 1;
2015
+ }));
2016
+ const out2 = new Series2(temp[0]);
2017
+ out2.index = temp[1];
2018
+ out2.name = series.name;
2019
+ return out2;
2020
+ }
2021
+ function seriesToObject(series) {
2022
+ const out2 = {};
2023
+ out2[series.name] = {};
2024
+ series.index.forEach((index, i) => {
2025
+ out2[series.name][index] = series.values[i];
2026
+ });
2027
+ return out2;
2028
+ }
2029
+ var SERIES_SYMBOL = Symbol.for("@jrc03c/js-math-tools/series");
2030
+ function createSeriesClass(DataFrame2) {
2031
+ class Series2 {
2032
+ static [Symbol.hasInstance](x) {
2033
+ try {
2034
+ return !!x._symbol && x._symbol === SERIES_SYMBOL;
2035
+ } catch (e) {
2036
+ return false;
2037
+ }
2038
+ }
2039
+ constructor(data) {
2040
+ this.name = "data";
2041
+ Object.defineProperty(this, "_symbol", {
2042
+ configurable: false,
2043
+ enumerable: false,
2044
+ writable: false,
2045
+ value: SERIES_SYMBOL
2046
+ });
2047
+ Object.defineProperty(this, "_values", {
2048
+ value: [],
2049
+ configurable: true,
2050
+ enumerable: false,
2051
+ writable: true
2052
+ });
2053
+ Object.defineProperty(this, "values", {
2054
+ configurable: true,
2055
+ enumerable: true,
2056
+ get() {
2057
+ return this._values;
2058
+ },
2059
+ set(x) {
2060
+ assert(isArray(x), "The new values must be a 1-dimensional array!");
2061
+ const dataShape = shape(x);
2062
+ assert(dataShape.length === 1, "The new array of values must be 1-dimensional!");
2063
+ if (dataShape[0] < this._index.length) {
2064
+ this._index = this._index.slice(0, dataShape[0]);
2065
+ } else if (dataShape[0] > this._index.length) {
2066
+ this._index = this._index.concat(range(this._index.length, dataShape[0]).map((i) => {
2067
+ return "item" + leftPad(i, (x.length - 1).toString().length);
2068
+ }));
2069
+ }
2070
+ this._values = x;
2071
+ }
2072
+ });
2073
+ Object.defineProperty(this, "_index", {
2074
+ value: [],
2075
+ configurable: true,
2076
+ enumerable: false,
2077
+ writable: true
2078
+ });
2079
+ Object.defineProperty(this, "index", {
2080
+ configurable: true,
2081
+ enumerable: true,
2082
+ get() {
2083
+ return this._index;
2084
+ },
2085
+ set(x) {
2086
+ assert(isArray(x), "The new index must be a 1-dimensional array of strings!");
2087
+ assert(x.length === this.shape[0], "The new index must be the same length as the old index!");
2088
+ assert(shape(x).length === 1, "The new index must be a 1-dimensional array of strings!");
2089
+ x.forEach((value) => {
2090
+ assert(isString(value), "All of the row names must be strings!");
2091
+ });
2092
+ this._index = x;
2093
+ }
2094
+ });
2095
+ if (data) {
2096
+ if (data instanceof Series2) {
2097
+ this.name = data.name;
2098
+ this.values = copy(data.values);
2099
+ this.index = copy(data.index);
2100
+ } else if (isArray(data)) {
2101
+ const dataShape = shape(data);
2102
+ assert(dataShape.length === 1, "When passing an array into the constructor of a Series, the array must be 1-dimensional!");
2103
+ this.values = data;
2104
+ } else if (data instanceof Object) {
2105
+ const keys = Object.keys(data).concat(Object.getOwnPropertySymbols(data)).map((v) => v.toString());
2106
+ assert(keys.length === 1, "When passing an object into the constructor of a Series, the object must have only 1 key-value pair, where the key is the name of the data and the value is the 1-dimensional array of values!");
2107
+ const name = keys[0];
2108
+ const values = data[name];
2109
+ assert(shape(values).length === 1, "When passing an object into the constructor of a Series, the object must have only 1 key-value pair, where the key is the name of the data and the value is the 1-dimensional array of values!");
2110
+ this.name = name;
2111
+ this.values = values.slice();
2112
+ }
2113
+ }
2114
+ }
2115
+ get shape() {
2116
+ return shape(this.values);
2117
+ }
2118
+ get length() {
2119
+ return this.shape[0];
2120
+ }
2121
+ get isEmpty() {
2122
+ return this.values.filter((v) => !isUndefined(v)).length === 0;
2123
+ }
2124
+ clear() {
2125
+ const out2 = this.copy();
2126
+ out2.values.forEach((v, i) => {
2127
+ out2.values[i] = void 0;
2128
+ });
2129
+ return out2;
2130
+ }
2131
+ get(indices) {
2132
+ return seriesGet(this, indices);
2133
+ }
2134
+ getSubsetByNames(indices) {
2135
+ return seriesGetSubsetByNames(Series2, this, indices);
2136
+ }
2137
+ getSubsetByIndices(indices) {
2138
+ return seriesGetSubsetByIndices(this, indices);
2139
+ }
2140
+ loc(indices) {
2141
+ return this.getSubsetByNames(indices);
2142
+ }
2143
+ iloc(indices) {
2144
+ return this.getSubsetByIndices(indices);
2145
+ }
2146
+ reverse() {
2147
+ const out2 = new Series2(reverse(this.values));
2148
+ out2.index = reverse(this.index);
2149
+ out2.name = this.name;
2150
+ return out2;
2151
+ }
2152
+ resetIndex() {
2153
+ const out2 = this.copy();
2154
+ out2.index = range(0, this.shape[0]).map((i) => {
2155
+ return "item" + leftPad(i, (out2.index.length - 1).toString().length);
2156
+ });
2157
+ return out2;
2158
+ }
2159
+ copy() {
2160
+ const out2 = new Series2();
2161
+ out2._values = copy(this.values);
2162
+ out2._index = copy(this.index);
2163
+ out2.name = this.name;
2164
+ return out2;
2165
+ }
2166
+ append(x) {
2167
+ return seriesAppend(Series2, this, x);
2168
+ }
2169
+ apply(fn) {
2170
+ return seriesApply(this, fn);
2171
+ }
2172
+ concat(x) {
2173
+ return this.append(x);
2174
+ }
2175
+ dropMissing(condition, threshold) {
2176
+ return seriesDropMissing(this, condition, threshold);
2177
+ }
2178
+ dropNaN() {
2179
+ return seriesDropNaN(Series2, this);
2180
+ }
2181
+ toObject() {
2182
+ return seriesToObject(this);
2183
+ }
2184
+ print() {
2185
+ return seriesPrint(this);
2186
+ }
2187
+ shuffle() {
2188
+ return seriesShuffle(this);
2189
+ }
2190
+ sort(direction) {
2191
+ return seriesSort(Series2, this, direction);
2192
+ }
2193
+ sortByIndex() {
2194
+ return seriesSortByIndex(Series2, this);
2195
+ }
2196
+ filter(fn) {
2197
+ return seriesFilter(Series2, this, fn);
2198
+ }
2199
+ toDataFrame() {
2200
+ const out2 = new DataFrame2(transpose([this.values]));
2201
+ out2.columns = [this.name];
2202
+ out2.index = this.index;
2203
+ return out2;
2204
+ }
2205
+ transpose() {
2206
+ const out2 = this.copy();
2207
+ out2.values = reverse(out2.values);
2208
+ out2.index = reverse(out2.index);
2209
+ return out2;
2210
+ }
2211
+ getDummies() {
2212
+ return this.toDataFrame().getDummies();
2213
+ }
2214
+ oneHotEncode() {
2215
+ return this.getDummies();
2216
+ }
2217
+ }
2218
+ return Series2;
2219
+ }
2220
+ var DATAFRAME_SYMBOL = Symbol.for("@jrc03c/js-math-tools/dataframe");
2221
+ function makeKey3(n) {
2222
+ const alpha = "abcdefghijklmnopqrstuvwxyz1234567890";
2223
+ let out2 = "";
2224
+ for (let i = 0; i < n; i++)
2225
+ out2 += alpha[Math.floor(random() * alpha.length)];
2226
+ return out2;
2227
+ }
2228
+ var DataFrame = class {
2229
+ static [Symbol.hasInstance](x) {
2230
+ try {
2231
+ return !!x._symbol && x._symbol === DATAFRAME_SYMBOL;
2232
+ } catch (e) {
2233
+ return false;
2234
+ }
2235
+ }
2236
+ constructor(data) {
2237
+ Object.defineProperty(this, "_symbol", {
2238
+ configurable: false,
2239
+ enumerable: false,
2240
+ writable: false,
2241
+ value: DATAFRAME_SYMBOL
2242
+ });
2243
+ Object.defineProperty(this, "_values", {
2244
+ value: [],
2245
+ configurable: true,
2246
+ enumerable: false,
2247
+ writable: true
2248
+ });
2249
+ Object.defineProperty(this, "values", {
2250
+ configurable: true,
2251
+ enumerable: true,
2252
+ get() {
2253
+ if (this._values.length === 0 || !isUndefined(this._values[0]) && this._values[0].length === 0) {
2254
+ return [[]];
2255
+ }
2256
+ return this._values;
2257
+ },
2258
+ set(x) {
2259
+ assert(isArray(x), "The new values must be a 2-dimensional array!");
2260
+ const dataShape = shape(x);
2261
+ assert(dataShape.length === 2, "The new array of values must be 2-dimensional!");
2262
+ if (dataShape[0] < this._index.length) {
2263
+ this._index = this._index.slice(0, dataShape[0]);
2264
+ } else if (dataShape[0] > this._index.length) {
2265
+ this._index = this._index.concat(range(this._index.length, dataShape[0]).map((i) => {
2266
+ return "row" + leftPad(i, (dataShape[0] - 1).toString().length);
2267
+ }));
2268
+ }
2269
+ if (dataShape[1] < this._columns.length) {
2270
+ this._columns = this._columns.slice(0, dataShape[1]);
2271
+ } else if (dataShape[1] > this._columns.length) {
2272
+ this._columns = this._columns.concat(range(this._columns.length, dataShape[1]).map((i) => {
2273
+ return "col" + leftPad(i, (dataShape[1] - 1).toString().length);
2274
+ }));
2275
+ }
2276
+ this._values = x;
2277
+ }
2278
+ });
2279
+ Object.defineProperty(this, "_columns", {
2280
+ value: [],
2281
+ configurable: true,
2282
+ enumerable: false,
2283
+ writable: true
2284
+ });
2285
+ Object.defineProperty(this, "columns", {
2286
+ configurable: true,
2287
+ enumerable: true,
2288
+ get() {
2289
+ return this._columns;
2290
+ },
2291
+ set(x) {
2292
+ assert(isArray(x), "The new columns list must be a 1-dimensional array of strings!");
2293
+ assert(this.isEmpty || x.length === this.shape[1], "The new columns list must be the same length as the old columns list!");
2294
+ assert(shape(x).length === 1, "The new columns list must be a 1-dimensional array of strings!");
2295
+ x = x.map((v) => {
2296
+ if (typeof v !== "string") {
2297
+ v = JSON.stringify(v) || v.toString();
2298
+ }
2299
+ if (v.trim().length === 0) {
2300
+ return "untitled_" + makeKey3(8);
2301
+ }
2302
+ return v.trim();
2303
+ });
2304
+ const counts = (() => {
2305
+ const temp = count(x);
2306
+ const out2 = {};
2307
+ temp.values.forEach((v) => {
2308
+ out2[v] = temp.get(v);
2309
+ });
2310
+ return out2;
2311
+ })();
2312
+ x = x.map((v) => {
2313
+ if (counts[v] > 1) {
2314
+ return v + "_" + makeKey3(8);
2315
+ }
2316
+ return v;
2317
+ });
2318
+ this._columns = x;
2319
+ }
2320
+ });
2321
+ Object.defineProperty(this, "_index", {
2322
+ value: [],
2323
+ configurable: true,
2324
+ enumerable: false,
2325
+ writable: true
2326
+ });
2327
+ Object.defineProperty(this, "index", {
2328
+ configurable: true,
2329
+ enumerable: true,
2330
+ get() {
2331
+ return this._index;
2332
+ },
2333
+ set(x) {
2334
+ assert(isArray(x), "The new index must be a 1-dimensional array of strings!");
2335
+ assert(this.isEmpty || x.length === this.shape[0], "The new index must be the same length as the old index!");
2336
+ assert(shape(x).length === 1, "The new index must be a 1-dimensional array of strings!");
2337
+ x = x.map((v) => {
2338
+ if (typeof v !== "string") {
2339
+ v = JSON.stringify(v) || v.toString();
2340
+ }
2341
+ if (v.trim().length === 0) {
2342
+ return "untitled_" + makeKey3(8);
2343
+ }
2344
+ return v.trim();
2345
+ });
2346
+ const counts = (() => {
2347
+ const temp = count(x);
2348
+ const out2 = {};
2349
+ temp.values.forEach((v) => {
2350
+ out2[v] = temp.get(v);
2351
+ });
2352
+ return out2;
2353
+ })();
2354
+ x = x.map((v) => {
2355
+ if (counts[v] > 1) {
2356
+ return v + "_" + makeKey3(8);
2357
+ }
2358
+ return v;
2359
+ });
2360
+ this._index = x;
2361
+ }
2362
+ });
2363
+ assert(isUndefined(data) || isObject(data) || isArray(data), "The `data` passed into the constructor of a DataFrame must be either (1) an object where the key-value pairs are (respectively) column names and 1-dimensional arrays of values, or (2) a 2-dimensional array of values.");
2364
+ if (data) {
2365
+ if (data instanceof DataFrame) {
2366
+ this.values = copy(data.values);
2367
+ this.columns = copy(data.columns);
2368
+ this.index = copy(data.index);
2369
+ } else if (isArray(data)) {
2370
+ const dataShape = shape(data);
2371
+ assert(dataShape.length === 2, "The `data` array passed into the constructor of a DataFrame must be 2-dimensional!");
2372
+ assert(!isJagged(data), "The 2-dimensional array passed into the constructor of a DataFrame must not contain sub-arrays (i.e., rows) of different lengths!");
2373
+ this.values = data;
2374
+ } else {
2375
+ this._columns = Object.keys(data).concat(Object.getOwnPropertySymbols(data)).map((v) => v.toString());
2376
+ const temp = [];
2377
+ let lastColName = null;
2378
+ let lastColLength = null;
2379
+ this._columns.forEach((col) => {
2380
+ if (isUndefined(lastColLength)) {
2381
+ lastColName = col;
2382
+ lastColLength = data[col].length;
2383
+ }
2384
+ assert(data[col].length === lastColLength, `The object passed into the DataFrame constructor contains arrays of different lengths! The key "${lastColName}" points to an array containing ${lastColLength} items, and the key "${col}" points to an array containing ${data[col].length} items.`);
2385
+ lastColLength = data[col].length;
2386
+ const values = data[col];
2387
+ temp.push(values);
2388
+ });
2389
+ this._values = transpose(temp);
2390
+ const dataShape = shape(this.values);
2391
+ this._index = range(0, dataShape[0]).map((i) => {
2392
+ return "row" + leftPad(i, (dataShape[0] - 1).toString().length);
2393
+ });
2394
+ }
2395
+ }
2396
+ }
2397
+ get shape() {
2398
+ return shape(this.values);
2399
+ }
2400
+ get length() {
2401
+ return this.shape[0];
2402
+ }
2403
+ get width() {
2404
+ return this.shape[1];
2405
+ }
2406
+ get rows() {
2407
+ return this.index;
2408
+ }
2409
+ set rows(rows) {
2410
+ this.index = rows;
2411
+ }
2412
+ get isEmpty() {
2413
+ return this.values.length === 0 || this.values.every((row) => row.length === 0);
2414
+ }
2415
+ clear() {
2416
+ const out2 = new DataFrame(ndarray(this.shape));
2417
+ out2.columns = this.columns.slice();
2418
+ out2.index = this.index.slice();
2419
+ return out2;
2420
+ }
2421
+ get(rows, cols) {
2422
+ if (arguments.length === 0) {
2423
+ return this;
2424
+ }
2425
+ if (arguments.length === 1) {
2426
+ try {
2427
+ return this.get(null, rows);
2428
+ } catch (e) {
2429
+ return this.get(rows, null);
2430
+ }
2431
+ }
2432
+ return dfGet(this, rows, cols);
2433
+ }
2434
+ getSubsetByNames(rows, cols) {
2435
+ return dfGetSubsetByNames(DataFrame, Series, this, rows, cols);
2436
+ }
2437
+ getSubsetByIndices(rowIndices, colIndices) {
2438
+ return dfGetSubsetByIndices(this, rowIndices, colIndices);
2439
+ }
2440
+ getDummies(columns) {
2441
+ return dfGetDummies(DataFrame, this, columns);
2442
+ }
2443
+ oneHotEncode(columns) {
2444
+ return dfGetDummies(DataFrame, this, columns);
2445
+ }
2446
+ transpose() {
2447
+ const out2 = new DataFrame(transpose(this.values));
2448
+ out2.columns = this.index.slice();
2449
+ out2.index = this.columns.slice();
2450
+ return out2;
2451
+ }
2452
+ get T() {
2453
+ return this.transpose();
2454
+ }
2455
+ resetIndex(shouldSkipCopying) {
2456
+ return dfResetIndex(this, shouldSkipCopying);
2457
+ }
2458
+ copy() {
2459
+ return dfCopy(DataFrame, this);
2460
+ }
2461
+ assign(p1, p2) {
2462
+ return dfAssign(DataFrame, Series, this, p1, p2);
2463
+ }
2464
+ apply(fn, axis) {
2465
+ return dfApply(DataFrame, Series, this, fn, axis);
2466
+ }
2467
+ dropMissing(axis, condition, threshold) {
2468
+ return dfDropMissing(DataFrame, Series, this, axis, condition, threshold);
2469
+ }
2470
+ dropNaN(axis, condition, threshold) {
2471
+ return dfDropNaN(DataFrame, this, axis, condition, threshold);
2472
+ }
2473
+ drop(rows, cols) {
2474
+ return dfDrop(DataFrame, Series, this, rows, cols);
2475
+ }
2476
+ dropColumns(columns) {
2477
+ return this.drop(null, columns);
2478
+ }
2479
+ dropRows(rows) {
2480
+ return this.drop(rows, null);
2481
+ }
2482
+ toDetailedObject(axis) {
2483
+ return dfToDetailedObject(this, axis);
2484
+ }
2485
+ toObject() {
2486
+ return dfToObject(this);
2487
+ }
2488
+ toJSONString(axis) {
2489
+ return dfToJSONString(this, axis);
2490
+ }
2491
+ saveAsJSON(filename, axis) {
2492
+ return dfToJSON(this, filename, axis);
2493
+ }
2494
+ print() {
2495
+ return dfPrint(DataFrame, Series, this);
2496
+ }
2497
+ sort(cols, directions) {
2498
+ return dfSort(this, cols, directions);
2499
+ }
2500
+ sortByIndex() {
2501
+ return this.sort();
2502
+ }
2503
+ filter(fn, axis) {
2504
+ return dfFilter(DataFrame, Series, this, fn, axis);
2505
+ }
2506
+ shuffle(axis) {
2507
+ return dfShuffle(this, axis);
2508
+ }
2509
+ append(x, axis) {
2510
+ return dfAppend(this, x, axis);
2511
+ }
2512
+ concat(x, axis) {
2513
+ return this.append(x, axis);
2514
+ }
2515
+ join(x, axis) {
2516
+ return this.append(x, axis);
2517
+ }
2518
+ toString() {
2519
+ return JSON.stringify(this);
2520
+ }
2521
+ };
2522
+ var Series = createSeriesClass(DataFrame);
2523
+ function max(arr, shouldDropNaNs) {
2524
+ return stats(arr, { shouldDropNaNs }).max;
2525
+ }
2526
+ function vectorize(fn) {
2527
+ assert(isFunction(fn), "You must pass a function into the `vectorize` function!");
2528
+ return function helper5() {
2529
+ let hasSeries, hasDataFrames;
2530
+ const series = [];
2531
+ const dataframes = [];
2532
+ const childArrays = Object.keys(arguments).filter((key) => {
2533
+ const arg = arguments[key];
2534
+ if (isArray(arg)) {
2535
+ return true;
2536
+ } else if (isSeries(arg)) {
2537
+ hasSeries = true;
2538
+ series.push(arg);
2539
+ return true;
2540
+ } else if (isDataFrame(arg)) {
2541
+ hasDataFrames = true;
2542
+ dataframes.push(arg);
2543
+ return true;
2544
+ } else {
2545
+ return false;
2546
+ }
2547
+ }).map((key) => arguments[key]);
2548
+ childArrays.slice(0, -1).forEach((s2, i) => {
2549
+ assert(isEqual(isArray(s2) ? shape(s2) : s2.shape, isArray(childArrays[i + 1]) ? shape(childArrays[i + 1]) : childArrays[i + 1].shape), `When passing multiple arrays into the \`${fn.name}\` function, all of the arrays must have the same shape!`);
2550
+ });
2551
+ if (childArrays.length > 0) {
2552
+ const maxLength = max(childArrays.map((a) => a.length ? a.length : a.values.length));
2553
+ const out2 = range(0, maxLength).map((i) => {
2554
+ const args = Object.keys(arguments).map((key) => {
2555
+ if (isArray(arguments[key])) {
2556
+ return arguments[key][i];
2557
+ } else if (isSeries(arguments[key])) {
2558
+ return arguments[key].values[i];
2559
+ } else if (isDataFrame(arguments[key])) {
2560
+ return arguments[key].values[i];
2561
+ } else {
2562
+ return arguments[key];
2563
+ }
2564
+ });
2565
+ return helper5(...args);
2566
+ });
2567
+ if (hasDataFrames) {
2568
+ try {
2569
+ if (dataframes.length === 1 && isEqual(shape(dataframes[0]), shape(out2))) {
2570
+ const temp = new DataFrame(out2);
2571
+ temp.index = dataframes[0].index.slice();
2572
+ temp.columns = dataframes[0].columns.slice();
2573
+ return temp;
2574
+ } else {
2575
+ return new DataFrame(out2);
2576
+ }
2577
+ } catch (e) {
2578
+ return out2;
2579
+ }
2580
+ }
2581
+ if (hasSeries) {
2582
+ try {
2583
+ if (series.length === 1 && series[0].length === out2.length) {
2584
+ const temp = new Series(out2);
2585
+ temp.name = series[0].name;
2586
+ temp.index = series[0].index.slice();
2587
+ return temp;
2588
+ } else {
2589
+ return new Series(out2);
2590
+ }
2591
+ } catch (e) {
2592
+ return out2;
2593
+ }
2594
+ }
2595
+ return out2;
2596
+ } else {
2597
+ return fn(...arguments);
2598
+ }
2599
+ };
2600
+ }
2601
+ function abs(x) {
2602
+ try {
2603
+ if (!isNumber(x))
2604
+ return NaN;
2605
+ if (typeof x === "bigint") {
2606
+ return x < 0 ? -x : x;
2607
+ } else {
2608
+ return Math.abs(x);
2609
+ }
2610
+ } catch (e) {
2611
+ return NaN;
2612
+ }
2613
+ }
2614
+ var vabs = vectorize(abs);
2615
+ function add() {
2616
+ try {
2617
+ let out2 = 0;
2618
+ let resultShouldBeABigInt = false;
2619
+ const x = Object.values(arguments);
2620
+ for (let v of x) {
2621
+ if (!isNumber(v))
2622
+ return NaN;
2623
+ if (typeof v === "bigint") {
2624
+ resultShouldBeABigInt = true;
2625
+ v = Number(v);
2626
+ }
2627
+ out2 += v;
2628
+ }
2629
+ if (resultShouldBeABigInt) {
2630
+ try {
2631
+ return BigInt(out2);
2632
+ } catch (e) {
2633
+ }
2634
+ }
2635
+ return out2;
2636
+ } catch (e) {
2637
+ return NaN;
2638
+ }
2639
+ }
2640
+ var vadd = vectorize(add);
2641
+ function apply(x, fn) {
2642
+ try {
2643
+ return fn(x);
2644
+ } catch (e) {
2645
+ return NaN;
2646
+ }
2647
+ }
2648
+ var vapply = vectorize(apply);
2649
+ function arccos(x) {
2650
+ try {
2651
+ if (!isNumber(x))
2652
+ return NaN;
2653
+ if (typeof x === "bigint") {
2654
+ x = Number(x);
2655
+ }
2656
+ return Math.acos(x);
2657
+ } catch (e) {
2658
+ return NaN;
2659
+ }
2660
+ }
2661
+ var varccos = vectorize(arccos);
2662
+ function arcsin(x) {
2663
+ try {
2664
+ if (!isNumber(x))
2665
+ return NaN;
2666
+ if (typeof x === "bigint") {
2667
+ x = Number(x);
2668
+ }
2669
+ return Math.asin(x);
2670
+ } catch (e) {
2671
+ return NaN;
2672
+ }
2673
+ }
2674
+ var varcsin = vectorize(arcsin);
2675
+ function arctan(x) {
2676
+ try {
2677
+ if (!isNumber(x))
2678
+ return NaN;
2679
+ if (typeof x === "bigint") {
2680
+ x = Number(x);
2681
+ }
2682
+ return Math.atan(x);
2683
+ } catch (e) {
2684
+ return NaN;
2685
+ }
2686
+ }
2687
+ var varctan = vectorize(arctan);
2688
+ function argmax(x, shouldDropNaNs) {
2689
+ if (isDataFrame(x)) {
2690
+ const index = argmax(x.values, shouldDropNaNs);
2691
+ return [x.index[index[0]], x.columns[index[1]]];
2692
+ }
2693
+ if (isSeries(x)) {
2694
+ const index = argmax(x.values, shouldDropNaNs);
2695
+ return x.index[index];
2696
+ }
2697
+ assert(isArray(x), "The `argmax` function only works on arrays, Series, and DataFrames!");
2698
+ try {
2699
+ const out2 = indexOf(x, max(x, shouldDropNaNs));
2700
+ if (out2) {
2701
+ if (out2.length === 0) {
2702
+ return void 0;
2703
+ } else if (out2.length === 1) {
2704
+ return out2[0];
2705
+ } else {
2706
+ return out2;
2707
+ }
2708
+ } else {
2709
+ return void 0;
2710
+ }
2711
+ } catch (e) {
2712
+ return void 0;
2713
+ }
2714
+ }
2715
+ function min(arr, shouldDropNaNs) {
2716
+ return stats(arr, { shouldDropNaNs }).min;
2717
+ }
2718
+ function argmin(x, shouldDropNaNs) {
2719
+ if (isDataFrame(x)) {
2720
+ const index = argmin(x.values, shouldDropNaNs);
2721
+ return [x.index[index[0]], x.columns[index[1]]];
2722
+ }
2723
+ if (isSeries(x)) {
2724
+ const index = argmin(x.values, shouldDropNaNs);
2725
+ return x.index[index];
2726
+ }
2727
+ assert(isArray(x), "The `argmin` function only works on arrays, Series, and DataFrames!");
2728
+ try {
2729
+ const out2 = indexOf(x, min(x, shouldDropNaNs));
2730
+ if (out2) {
2731
+ if (out2.length === 0) {
2732
+ return void 0;
2733
+ } else if (out2.length === 1) {
2734
+ return out2[0];
2735
+ } else {
2736
+ return out2;
2737
+ }
2738
+ } else {
2739
+ return void 0;
2740
+ }
2741
+ } catch (e) {
2742
+ return void 0;
2743
+ }
2744
+ }
2745
+ function cast(value, type) {
2746
+ if (isDataFrame(value) || isSeries(value)) {
2747
+ return value.apply((item) => cast(item, type));
2748
+ }
2749
+ if (isArray(value)) {
2750
+ return value.map((v) => cast(v, type));
2751
+ }
2752
+ if (type === "null") {
2753
+ return null;
2754
+ }
2755
+ if (type === "number") {
2756
+ if (isUndefined(value)) {
2757
+ return NaN;
2758
+ }
2759
+ const booleanValue = cast(value, "boolean");
2760
+ if (isBoolean(booleanValue)) {
2761
+ return booleanValue ? 1 : 0;
2762
+ }
2763
+ try {
2764
+ JSON.parse(value);
2765
+ } catch (e) {
2766
+ const dateValue = cast(value, "date");
2767
+ if (isDate(dateValue)) {
2768
+ return dateValue.getTime();
2769
+ }
2770
+ }
2771
+ const out2 = parseFloat(value);
2772
+ if (isNaN(out2))
2773
+ return NaN;
2774
+ return out2;
2775
+ }
2776
+ if (type === "int") {
2777
+ const out2 = cast(value, "number");
2778
+ return out2 >= 0 ? Math.floor(out2) : Math.ceil(out2);
2779
+ }
2780
+ if (type === "float") {
2781
+ return cast(value, "number");
2782
+ }
2783
+ if (type === "bigint") {
2784
+ if (typeof value === "bigint") {
2785
+ return value;
2786
+ }
2787
+ return BigInt(cast(value, "int"));
2788
+ }
2789
+ if (type === "boolean") {
2790
+ if (isBoolean(value)) {
2791
+ return value;
2792
+ }
2793
+ if (isNumber(value)) {
2794
+ if (value === 0) {
2795
+ return false;
2796
+ }
2797
+ if (value === 1) {
2798
+ return true;
2799
+ }
2800
+ return null;
2801
+ }
2802
+ try {
2803
+ const vBool = (typeof value === "object" ? value.toString() === "null" ? "false" : JSON.stringify(value) : value.toString()).trim().toLowerCase();
2804
+ if (vBool === "true" || vBool === "yes" || vBool === "y") {
2805
+ return true;
2806
+ }
2807
+ if (vBool === "false" || vBool === "no" || vBool === "n") {
2808
+ return false;
2809
+ }
2810
+ return null;
2811
+ } catch (e) {
2812
+ return null;
2813
+ }
2814
+ }
2815
+ if (type === "date") {
2816
+ if (isDate(value)) {
2817
+ return value;
2818
+ }
2819
+ if (isUndefined(value)) {
2820
+ return null;
2821
+ }
2822
+ const valueFloat = parseFloat(value);
2823
+ if (!isNaN(valueFloat)) {
2824
+ const out2 = new Date(value);
2825
+ if (!isDate(out2))
2826
+ return null;
2827
+ return out2;
2828
+ }
2829
+ const valueDate = Date.parse(value);
2830
+ if (!isNaN(valueDate)) {
2831
+ return new Date(valueDate);
2832
+ }
2833
+ return null;
2834
+ }
2835
+ if (type === "object") {
2836
+ if (isObject(value)) {
2837
+ return value;
2838
+ }
2839
+ const booleanValue = cast(value, "boolean");
2840
+ if (isBoolean(booleanValue)) {
2841
+ return null;
2842
+ }
2843
+ try {
2844
+ const numberValue = cast(value, "number");
2845
+ if (isNumber(numberValue)) {
2846
+ JSON.parse(value);
2847
+ return null;
2848
+ }
2849
+ } catch (e) {
2850
+ }
2851
+ const dateValue = cast(value, "date");
2852
+ if (dateValue) {
2853
+ return dateValue;
2854
+ }
2855
+ try {
2856
+ const out2 = JSON.parse(value);
2857
+ if (isArray(out2)) {
2858
+ return out2.map((v) => cast(v, type));
2859
+ } else {
2860
+ return out2;
2861
+ }
2862
+ } catch (e) {
2863
+ return null;
2864
+ }
2865
+ }
2866
+ if (type === "string") {
2867
+ if (isUndefined(value)) {
2868
+ if (isEqual(value, void 0)) {
2869
+ return "undefined";
2870
+ }
2871
+ return "null";
2872
+ }
2873
+ if (value instanceof Date) {
2874
+ return value.toJSON();
2875
+ }
2876
+ const valueString = (() => {
2877
+ if (typeof value === "object") {
2878
+ if (value === null) {
2879
+ return "null";
2880
+ } else {
2881
+ return JSON.stringify(value);
2882
+ }
2883
+ } else {
2884
+ return value.toString();
2885
+ }
2886
+ })();
2887
+ return valueString;
2888
+ }
2889
+ }
2890
+ function ceil(x) {
2891
+ try {
2892
+ if (!isNumber(x))
2893
+ return NaN;
2894
+ if (typeof x === "bigint")
2895
+ return x;
2896
+ return Math.ceil(x);
2897
+ } catch (e) {
2898
+ return NaN;
2899
+ }
2900
+ }
2901
+ var vceil = vectorize(ceil);
2902
+ function chop(x, threshold) {
2903
+ try {
2904
+ if (!isNumber(x))
2905
+ return NaN;
2906
+ if (typeof x === "bigint")
2907
+ return x;
2908
+ if (isUndefined(threshold)) {
2909
+ threshold = 1e-10;
2910
+ } else if (!isNumber(threshold)) {
2911
+ return NaN;
2912
+ }
2913
+ return vabs(x) < threshold ? 0 : x;
2914
+ } catch (e) {
2915
+ return NaN;
2916
+ }
2917
+ }
2918
+ var vchop = vectorize(chop);
2919
+ function int(x) {
2920
+ if (isDataFrame(x) || isSeries(x)) {
2921
+ const out2 = x.copy();
2922
+ out2.values = int(out2.values);
2923
+ return out2;
2924
+ }
2925
+ if (isArray(x)) {
2926
+ return x.map((v) => int(v));
2927
+ } else {
2928
+ try {
2929
+ const out2 = JSON.parse(x);
2930
+ if (isNumber(out2)) {
2931
+ return typeof out2 === "bigint" ? Number(out2) : out2 >= 0 ? Math.floor(out2) : Math.ceil(out2);
2932
+ }
2933
+ return NaN;
2934
+ } catch (e) {
2935
+ return NaN;
2936
+ }
2937
+ }
2938
+ }
2939
+ var vint = vectorize(int);
2940
+ function clamp(x, a, b) {
2941
+ try {
2942
+ if (!isNumber(x))
2943
+ return NaN;
2944
+ if (!isNumber(a))
2945
+ return NaN;
2946
+ if (!isNumber(b))
2947
+ return NaN;
2948
+ if (typeof x === "bigint") {
2949
+ return BigInt(clamp(vint(x), a, b));
2950
+ }
2951
+ if (x < a)
2952
+ return a;
2953
+ if (x > b)
2954
+ return b;
2955
+ return x;
2956
+ } catch (e) {
2957
+ return NaN;
2958
+ }
2959
+ }
2960
+ var vclamp = vectorize(clamp);
2961
+ function combinationsIterator(x, r) {
2962
+ function* helper5(x2, r2) {
2963
+ if (r2 > x2.length) {
2964
+ yield x2;
2965
+ } else if (r2 <= 0) {
2966
+ yield [];
2967
+ } else if (x2.length < 2) {
2968
+ yield x2;
2969
+ } else {
2970
+ for (let i = 0; i < x2.length; i++) {
2971
+ const item = x2[i];
2972
+ const after = x2.slice(i + 1);
2973
+ if (after.length < r2 - 1) {
2974
+ continue;
2975
+ }
2976
+ if (r2 - 1 >= 0) {
2977
+ for (const child of combinationsIterator(after, r2 - 1)) {
2978
+ yield [item].concat(child);
2979
+ }
2980
+ }
2981
+ }
2982
+ }
2983
+ }
2984
+ if (isDataFrame(x) || isSeries(x)) {
2985
+ return combinationsIterator(x.values, r);
2986
+ }
2987
+ assert(isArray(x), "The `combinations` function only works on arrays, Series, and DataFrames!");
2988
+ assert(isNumber(r) && vint(r) === r && r >= 0, "`r` must be a non-negative integer!");
2989
+ return helper5(flatten(x), r);
2990
+ }
2991
+ function combinations(x, r) {
2992
+ const out2 = [];
2993
+ for (const combo of combinationsIterator(x, r)) {
2994
+ out2.push(combo.slice());
2995
+ }
2996
+ return out2;
2997
+ }
2998
+ function intersect() {
2999
+ const arrays = Object.values(arguments).map((x) => {
3000
+ if (isDataFrame(x) || isSeries(x)) {
3001
+ return set(x.values);
3002
+ }
3003
+ assert(isArray(x), "The `intersect` function only works on arrays, Series, and DataFrames!");
3004
+ return set(x);
3005
+ });
3006
+ const all = set(arrays);
3007
+ return all.filter((v) => {
3008
+ return arrays.every((arr) => arr.findIndex((other) => isEqual(other, v)) > -1);
3009
+ });
3010
+ }
3011
+ var _IndexMatcher = class {
3012
+ constructor(mode2) {
3013
+ assert(isUndefined(mode2) || mode2 === _IndexMatcher.DROP_NAN_MODE || mode2 === _IndexMatcher.DROP_MISSING_MODE, "The `mode` value passed into the `IndexMatcher` constructor must be undefined or one of [IndexMatcher.DROP_NAN_MODE, IndexMatcher.DROP_MISSING_MODE]! (By default, the mode is `Indexer.DROP_MISSING_MODE`.)");
3014
+ this.mode = !isUndefined(mode2) ? mode2 : _IndexMatcher.DROP_NAN_MODE;
3015
+ this.index = null;
3016
+ }
3017
+ fit() {
3018
+ const indices = [];
3019
+ Object.values(arguments).forEach((x) => {
3020
+ if (isArray(x)) {
3021
+ const xshape = shape(x);
3022
+ if (xshape.length === 1) {
3023
+ x = new Series(x);
3024
+ } else if (xshape.length === 2) {
3025
+ x = new DataFrame(x);
3026
+ } else {
3027
+ throw new Error("The `IndexMatcher.fit` method only works on arrays, Series, and DataFrames!");
3028
+ }
3029
+ }
3030
+ assert(isDataFrame(x) || isSeries(x), "The `IndexMatcher.fit` method only works on arrays, Series, and DataFrames!");
3031
+ if (this.mode === _IndexMatcher.DROP_MISSING_MODE) {
3032
+ indices.push(x.dropMissing().index);
3033
+ } else {
3034
+ indices.push(x.dropNaN().index);
3035
+ }
3036
+ });
3037
+ this.index = intersect(...indices);
3038
+ return this;
3039
+ }
3040
+ transform() {
3041
+ assert(!!this.index, "The IndexMatcher hasn't been fitted yet! Please call the `fit` method before calling the `transform` method.");
3042
+ const out2 = Object.values(arguments).map((x) => {
3043
+ if (isArray(x)) {
3044
+ const xshape = shape(x);
3045
+ if (xshape.length === 1) {
3046
+ return new Series(x).get(this.index).values;
3047
+ } else if (xshape.length === 2) {
3048
+ return new DataFrame(x).get(this.index, null).values;
3049
+ } else {
3050
+ throw new Error("The `IndexMatcher.fit` method only works on arrays, Series, and DataFrames!");
3051
+ }
3052
+ }
3053
+ assert(isDataFrame(x) || isSeries(x), "The `IndexMatcher.fit` method only works on arrays, Series, and DataFrames!");
3054
+ return x.get(this.index, null);
3055
+ });
3056
+ return out2.length === 1 ? out2[0] : out2;
3057
+ }
3058
+ fitAndTransform() {
3059
+ return this.fit(...arguments).transform(...arguments);
3060
+ }
3061
+ };
3062
+ var IndexMatcher = _IndexMatcher;
3063
+ __publicField(IndexMatcher, "DROP_NAN_MODE", "DROP_NAN_MODE");
3064
+ __publicField(IndexMatcher, "DROP_MISSING_MODE", "DROP_MISSING_MODE");
3065
+ function covariance(x, y, shouldDropNaNs, shouldAlsoReturnStatsObjects) {
3066
+ if (isSeries(x)) {
3067
+ return covariance(x.values, y, shouldDropNaNs, shouldAlsoReturnStatsObjects);
3068
+ }
3069
+ if (isSeries(y)) {
3070
+ return covariance(x, y.values, shouldDropNaNs, shouldAlsoReturnStatsObjects);
3071
+ }
3072
+ assert(isArray(x) && isArray(y) && shape(x).length === 1 && shape(y).length === 1, "The `covariance` function only works on 1-dimensional arrays and Series!");
3073
+ assert(x.length === y.length, "The two arrays or Series passed into the `covariance` function must have the same length!");
3074
+ if (shouldDropNaNs) {
3075
+ return covariance(...new IndexMatcher().fitAndTransform(x, y), false, shouldAlsoReturnStatsObjects);
3076
+ }
3077
+ try {
3078
+ const xstats = stats(x, { stdev: shouldAlsoReturnStatsObjects });
3079
+ const ystats = stats(y, { stdev: shouldAlsoReturnStatsObjects });
3080
+ const mx = Number(xstats.mean);
3081
+ const my = Number(ystats.mean);
3082
+ if (!isNumber(mx) || !isNumber(my)) {
3083
+ return NaN;
3084
+ }
3085
+ const n = Math.max(x.length, y.length);
3086
+ let out2 = 0;
3087
+ for (let i = 0; i < n; i++) {
3088
+ let vx = x[i];
3089
+ let vy = y[i];
3090
+ if (!isNumber(vx))
3091
+ return NaN;
3092
+ if (!isNumber(vy))
3093
+ return NaN;
3094
+ if (typeof vx === "bigint") {
3095
+ vx = Number(vx);
3096
+ }
3097
+ if (typeof vy === "bigint") {
3098
+ vy = Number(vy);
3099
+ }
3100
+ out2 += (vx - mx) * (vy - my);
3101
+ }
3102
+ if (shouldAlsoReturnStatsObjects) {
3103
+ return [out2 / x.length, xstats, ystats];
3104
+ } else {
3105
+ return out2 / x.length;
3106
+ }
3107
+ } catch (e) {
3108
+ return NaN;
3109
+ }
3110
+ }
3111
+ function correl(x, y, shouldDropNaNs) {
3112
+ if (isSeries(x)) {
3113
+ return correl(x.values, y, shouldDropNaNs);
3114
+ }
3115
+ if (isSeries(y)) {
3116
+ return correl(x, y.values, shouldDropNaNs);
3117
+ }
3118
+ assert(isArray(x) && isArray(y) && shape(x).length === 1 && shape(y).length === 1, "The `correl` function only works on 1-dimensional arrays and Series!");
3119
+ assert(x.length === y.length, "The two arrays or Series passed into the `correl` function must have the same length!");
3120
+ try {
3121
+ const shouldAlsoReturnStatsObjects = true;
3122
+ const [num, xstats, ystats] = covariance(x, y, shouldDropNaNs, shouldAlsoReturnStatsObjects);
3123
+ const den = xstats.stdev * ystats.stdev;
3124
+ return num / den;
3125
+ } catch (e) {
3126
+ return NaN;
3127
+ }
3128
+ }
3129
+ function cos(x) {
3130
+ try {
3131
+ if (!isNumber(x))
3132
+ return NaN;
3133
+ if (typeof x === "bigint") {
3134
+ x = Number(x);
3135
+ }
3136
+ return Math.cos(x);
3137
+ } catch (e) {
3138
+ return NaN;
3139
+ }
3140
+ }
3141
+ var vcos = vectorize(cos);
3142
+ var dataTypes = Object.freeze({
3143
+ boolean: "boolean",
3144
+ date: "date",
3145
+ null: "null",
3146
+ number: "number",
3147
+ object: "object",
3148
+ string: "string"
3149
+ });
3150
+ function diff(a, b) {
3151
+ if (isDataFrame(a) || isSeries(a)) {
3152
+ return diff(a.values, b);
3153
+ }
3154
+ if (isDataFrame(b) || isSeries(b)) {
3155
+ return diff(a, b.values);
3156
+ }
3157
+ assert(isArray(a) && isArray(b), "The `diff` function only works on arrays, Series, and DataFrames!");
3158
+ const aTemp = set(a);
3159
+ const bTemp = set(b);
3160
+ const out2 = [];
3161
+ aTemp.forEach((item) => {
3162
+ if (bTemp.findIndex((other) => isEqual(other, item)) < 0) {
3163
+ out2.push(item);
3164
+ }
3165
+ });
3166
+ return out2;
3167
+ }
3168
+ function pow(x, p) {
3169
+ try {
3170
+ if (!isNumber(x))
3171
+ return NaN;
3172
+ if (!isNumber(p))
3173
+ return NaN;
3174
+ if (typeof x === "bigint" || typeof p === "bigint") {
3175
+ const out2 = pow(Number(x), Number(p));
3176
+ try {
3177
+ return BigInt(out2);
3178
+ } catch (e) {
3179
+ return out2;
3180
+ }
3181
+ }
3182
+ return Math.pow(x, p);
3183
+ } catch (e) {
3184
+ return NaN;
3185
+ }
3186
+ }
3187
+ var vpow = vectorize(pow);
3188
+ function sqrt(x) {
3189
+ try {
3190
+ if (!isNumber(x))
3191
+ return NaN;
3192
+ if (typeof x === "bigint") {
3193
+ const out2 = sqrt(Number(x));
3194
+ try {
3195
+ return BigInt(out2);
3196
+ } catch (e) {
3197
+ return out2;
3198
+ }
3199
+ }
3200
+ return Math.sqrt(x);
3201
+ } catch (e) {
3202
+ return NaN;
3203
+ }
3204
+ }
3205
+ var vsqrt = vectorize(sqrt);
3206
+ function multiply() {
3207
+ try {
3208
+ const x = Object.values(arguments);
3209
+ if (x.length === 0)
3210
+ return NaN;
3211
+ let resultShouldBeABigInt = false;
3212
+ let out2 = 1;
3213
+ for (let v of x) {
3214
+ if (!isNumber(v))
3215
+ return NaN;
3216
+ if (typeof v === "bigint") {
3217
+ resultShouldBeABigInt = true;
3218
+ v = Number(v);
3219
+ }
3220
+ out2 *= v;
3221
+ }
3222
+ if (resultShouldBeABigInt) {
3223
+ try {
3224
+ return BigInt(out2);
3225
+ } catch (e) {
3226
+ }
3227
+ }
3228
+ return out2;
3229
+ } catch (e) {
3230
+ return NaN;
3231
+ }
3232
+ }
3233
+ var vmultiply = vectorize(multiply);
3234
+ function scale() {
3235
+ return vmultiply(...arguments);
3236
+ }
3237
+ function subtract(a, b) {
3238
+ return vadd(a, scale(b, -1));
3239
+ }
3240
+ function sum(arr, shouldDropNaNs) {
3241
+ return stats(arr, { shouldDropNaNs }).sum;
3242
+ }
3243
+ function distance(a, b) {
3244
+ if (isNumber(a) && isNumber(b)) {
3245
+ return vabs(a - b);
3246
+ }
3247
+ if (isDataFrame(a) || isSeries(a)) {
3248
+ return distance(a.values, b);
3249
+ }
3250
+ if (isDataFrame(b) || isSeries(b)) {
3251
+ return distance(a, b.values);
3252
+ }
3253
+ if (isArray(a) && isArray(b)) {
3254
+ assert(isEqual(shape(a), shape(b)), "If passing two arrays, Series, or DataFrames into the `distance` function, then those objects must have the same shape!");
3255
+ }
3256
+ try {
3257
+ return vsqrt(sum(vpow(subtract(a, b), 2)));
3258
+ } catch (e) {
3259
+ return NaN;
3260
+ }
3261
+ }
3262
+ function divide(a, b) {
3263
+ return scale(a, vpow(b, -1));
3264
+ }
3265
+ function dot(a, b) {
3266
+ if (isDataFrame(a)) {
3267
+ const temp = dot(a.values, b);
3268
+ if (shape(temp).length === 1) {
3269
+ const out2 = new Series(temp);
3270
+ out2.name = isSeries(b) ? b.name : out2.name;
3271
+ out2.index = a.index.slice();
3272
+ return out2;
3273
+ } else {
3274
+ const out2 = new DataFrame(temp);
3275
+ out2.index = a.index.slice();
3276
+ if (isDataFrame(b)) {
3277
+ out2.columns = b.columns.slice();
3278
+ }
3279
+ return out2;
3280
+ }
3281
+ }
3282
+ if (isDataFrame(b)) {
3283
+ const temp = dot(a, b.values);
3284
+ if (shape(temp).length === 1) {
3285
+ const out2 = new Series(temp);
3286
+ out2.name = isSeries(a) ? a.name : out2.name;
3287
+ out2.index = b.columns.slice();
3288
+ return out2;
3289
+ } else {
3290
+ const out2 = new DataFrame(temp);
3291
+ out2.columns = b.columns.slice();
3292
+ return out2;
3293
+ }
3294
+ }
3295
+ if (isSeries(a)) {
3296
+ return dot(a.values, b);
3297
+ }
3298
+ if (isSeries(b)) {
3299
+ return dot(a, b.values);
3300
+ }
3301
+ assert(isArray(a) && isArray(b), "The `dot` function only works on arrays, Series, and DataFrames!");
3302
+ const aShape = shape(a);
3303
+ const bShape = shape(b);
3304
+ assert(aShape.length <= 2 && bShape.length <= 2, "I'm not smart enough to know how to get the dot-product of arrays that have more than 2 dimensions. Sorry for the inconvenience! Please only pass 1- or 2-dimensional arrays into the `dot` function!");
3305
+ assert(aShape[aShape.length - 1] === bShape[0], `There's a dimension misalignment in the two arrays you passed into the \`dot\` function. (${aShape[aShape.length - 1]} !== ${bShape[0]})`);
3306
+ if (aShape.length === 1 && bShape.length === 1) {
3307
+ return sum(scale(a, b));
3308
+ } else if (aShape.length === 1 && bShape.length === 2) {
3309
+ return transpose(b).map((col) => dot(a, col));
3310
+ } else if (aShape.length === 2 && bShape.length === 1) {
3311
+ return a.map((row) => dot(row, b));
3312
+ } else if (aShape.length === 2 && bShape.length === 2) {
3313
+ const bTranspose = transpose(b);
3314
+ const out2 = [];
3315
+ for (let i = 0; i < a.length; i++) {
3316
+ const row = [];
3317
+ for (let j = 0; j < bTranspose.length; j++) {
3318
+ row.push(dot(a[i], bTranspose[j]));
3319
+ }
3320
+ out2.push(row);
3321
+ }
3322
+ return out2;
3323
+ }
3324
+ }
3325
+ function dropMissing(x) {
3326
+ if (isDataFrame(x) || isSeries(x)) {
3327
+ return x.dropMissing(...Object.values(arguments).slice(1));
3328
+ }
3329
+ assert(isArray(x), "The `dropMissing` function only works on arrays, Series, and DataFrames!");
3330
+ const out2 = [];
3331
+ x.forEach((v) => {
3332
+ try {
3333
+ return out2.push(dropMissing(v));
3334
+ } catch (e) {
3335
+ if (!isUndefined(v)) {
3336
+ out2.push(v);
3337
+ }
3338
+ }
3339
+ });
3340
+ return out2;
3341
+ }
3342
+ function dropMissingPairwise(a, b) {
3343
+ if (isDataFrame(a) || isSeries(a)) {
3344
+ return dropMissingPairwise(a.values, b);
3345
+ }
3346
+ if (isDataFrame(b) || isSeries(b)) {
3347
+ return dropMissingPairwise(a, b.values);
3348
+ }
3349
+ assert(isArray(a) && isArray(b), "The `dropMissingPairwise` function only works on arrays, Series, and DataFrames!");
3350
+ assert(isEqual(shape(a), shape(b)), "The two arrays, Series, and/or DataFrames passed into the `dropMissingPairwise` function must have the same shape!");
3351
+ const aOut = [];
3352
+ const bOut = [];
3353
+ for (let i = 0; i < a.length; i++) {
3354
+ try {
3355
+ const [aChildren, bChildren] = dropMissingPairwise(a[i], b[i]);
3356
+ aOut.push(aChildren);
3357
+ bOut.push(bChildren);
3358
+ } catch (e) {
3359
+ if (!isUndefined(a[i]) && !isUndefined(b[i])) {
3360
+ aOut.push(a[i]);
3361
+ bOut.push(b[i]);
3362
+ }
3363
+ }
3364
+ }
3365
+ return [aOut, bOut];
3366
+ }
3367
+ function dropNaNPairwise(a, b) {
3368
+ if (isDataFrame(a) || isSeries(a)) {
3369
+ return dropNaNPairwise(a.values, b);
3370
+ }
3371
+ if (isDataFrame(b) || isSeries(b)) {
3372
+ return dropNaNPairwise(a, b.values);
3373
+ }
3374
+ assert(isArray(a) && isArray(b), "The `dropNaNPairwise` only works on arrays, Series, and DataFrames!");
3375
+ assert(isEqual(shape(a), shape(b)), "The two arrays, Series, and/or DataFrames passed into the `dropNaNPairwise` must have the same shape!");
3376
+ const aOut = [];
3377
+ const bOut = [];
3378
+ for (let i = 0; i < a.length; i++) {
3379
+ try {
3380
+ const [aChildren, bChildren] = dropNaNPairwise(a[i], b[i]);
3381
+ aOut.push(aChildren);
3382
+ bOut.push(bChildren);
3383
+ } catch (e) {
3384
+ if (isNumber(a[i]) && isNumber(b[i])) {
3385
+ aOut.push(a[i]);
3386
+ bOut.push(b[i]);
3387
+ }
3388
+ }
3389
+ }
3390
+ return [aOut, bOut];
3391
+ }
3392
+ function dropUndefined(x) {
3393
+ return dropMissing(x);
3394
+ }
3395
+ function every(x, fn) {
3396
+ if (isDataFrame(x) || isSeries(x)) {
3397
+ return every(x.values, fn);
3398
+ }
3399
+ assert(isArray(x), "The first argument passed into the `every` function must be an array, Series, or DataFrame!");
3400
+ assert(isFunction(fn), "The second argument passed into the `every` function must be a function!");
3401
+ for (const v of x) {
3402
+ if (isArray(v)) {
3403
+ if (!every(v, fn)) {
3404
+ return false;
3405
+ }
3406
+ } else {
3407
+ if (!fn(v)) {
3408
+ return false;
3409
+ }
3410
+ }
3411
+ }
3412
+ return true;
3413
+ }
3414
+ function exp(x) {
3415
+ try {
3416
+ if (!isNumber(x))
3417
+ return NaN;
3418
+ if (typeof x === "bigint") {
3419
+ if (x === 0n) {
3420
+ return 1n;
3421
+ } else {
3422
+ x = Number(x);
3423
+ }
3424
+ }
3425
+ return Math.exp(x);
3426
+ } catch (e) {
3427
+ return NaN;
3428
+ }
3429
+ }
3430
+ var vexp = vectorize(exp);
3431
+ function factorial(n) {
3432
+ try {
3433
+ if (typeof n === "bigint") {
3434
+ return BigInt(factorial(vint(n)));
3435
+ }
3436
+ if (n !== vint(n))
3437
+ return NaN;
3438
+ if (n <= 1)
3439
+ return 1;
3440
+ return n * factorial(n - 1);
3441
+ } catch (e) {
3442
+ return NaN;
3443
+ }
3444
+ }
3445
+ var vfactorial = vectorize(factorial);
3446
+ function find(x, fn) {
3447
+ if (isDataFrame(x)) {
3448
+ return find(x.values, fn);
3449
+ }
3450
+ if (isSeries(x)) {
3451
+ return find(x.values, fn);
3452
+ }
3453
+ assert(isObject(x) || isArray(x), "You must pass (1) an object, array, Series, or DataFrame and (2) a function or value into the `find` function!");
3454
+ if (!isFunction(fn)) {
3455
+ const value = fn;
3456
+ fn = (v) => v === value;
3457
+ }
3458
+ function helper5(x2, fn2, checked) {
3459
+ checked = checked || [];
3460
+ if (checked.indexOf(x2) > -1) {
3461
+ return null;
3462
+ }
3463
+ if (isObject(x2)) {
3464
+ checked.push(x2);
3465
+ const keys = Object.keys(x2).concat(Object.getOwnPropertySymbols(x2));
3466
+ for (let i = 0; i < keys.length; i++) {
3467
+ const key = keys[i];
3468
+ const value = x2[key];
3469
+ if (fn2(value)) {
3470
+ return value;
3471
+ }
3472
+ const result = helper5(value, fn2, checked);
3473
+ if (result) {
3474
+ return result;
3475
+ }
3476
+ }
3477
+ } else if (isArray(x2)) {
3478
+ checked.push(x2);
3479
+ for (let i = 0; i < x2.length; i++) {
3480
+ const value = x2[i];
3481
+ if (fn2(value)) {
3482
+ return value;
3483
+ }
3484
+ const result = helper5(value, fn2, checked);
3485
+ if (result) {
3486
+ return result;
3487
+ }
3488
+ }
3489
+ } else {
3490
+ if (fn2(x2)) {
3491
+ return x2;
3492
+ }
3493
+ }
3494
+ return null;
3495
+ }
3496
+ function safeFn(v) {
3497
+ try {
3498
+ return fn(v);
3499
+ } catch (e) {
3500
+ return false;
3501
+ }
3502
+ }
3503
+ return helper5(x, safeFn);
3504
+ }
3505
+ function findAll(x, fn) {
3506
+ if (isDataFrame(x)) {
3507
+ return findAll(x.values, fn);
3508
+ }
3509
+ if (isSeries(x)) {
3510
+ return findAll(x.values, fn);
3511
+ }
3512
+ assert(isObject(x) || isArray(x), "You must pass (1) an object, array, Series, or DataFrame and (2) a function or value into the `findAll` function!");
3513
+ if (!isFunction(fn)) {
3514
+ const value = fn;
3515
+ fn = (v) => v === value;
3516
+ }
3517
+ function helper5(x2, fn2, checked) {
3518
+ checked = checked || [];
3519
+ if (checked.indexOf(x2) > -1) {
3520
+ return null;
3521
+ }
3522
+ if (isObject(x2)) {
3523
+ checked.push(x2);
3524
+ const keys = Object.keys(x2).concat(Object.getOwnPropertySymbols(x2));
3525
+ const out2 = [];
3526
+ for (let i = 0; i < keys.length; i++) {
3527
+ const key = keys[i];
3528
+ const value = x2[key];
3529
+ let alreadyStoredThisValue = false;
3530
+ if (fn2(value)) {
3531
+ out2.push(value);
3532
+ alreadyStoredThisValue = true;
3533
+ }
3534
+ const results2 = helper5(value, fn2, checked);
3535
+ if (results2 && results2.length > 0) {
3536
+ results2.slice(alreadyStoredThisValue ? 1 : 0).forEach((r) => out2.push(r));
3537
+ }
3538
+ }
3539
+ return out2;
3540
+ } else if (isArray(x2)) {
3541
+ checked.push(x2);
3542
+ const out2 = [];
3543
+ for (let i = 0; i < x2.length; i++) {
3544
+ const value = x2[i];
3545
+ let alreadyStoredThisValue = false;
3546
+ if (fn2(value)) {
3547
+ out2.push(value);
3548
+ alreadyStoredThisValue = true;
3549
+ }
3550
+ const results2 = helper5(value, fn2, checked);
3551
+ if (results2 && results2.length > 0) {
3552
+ results2.slice(alreadyStoredThisValue ? 1 : 0).forEach((r) => out2.push(r));
3553
+ }
3554
+ }
3555
+ return out2;
3556
+ } else {
3557
+ if (fn2(x2)) {
3558
+ return [x2];
3559
+ }
3560
+ }
3561
+ return null;
3562
+ }
3563
+ function safeFn(v) {
3564
+ try {
3565
+ return fn(v);
3566
+ } catch (e) {
3567
+ return false;
3568
+ }
3569
+ }
3570
+ const results = helper5(x, safeFn);
3571
+ if (results && results.length > 0) {
3572
+ return results;
3573
+ } else {
3574
+ return null;
3575
+ }
3576
+ }
3577
+ function float(x) {
3578
+ try {
3579
+ if (x === "Infinity") {
3580
+ return Infinity;
3581
+ }
3582
+ if (x === "-Infinity") {
3583
+ return -Infinity;
3584
+ }
3585
+ const out2 = JSON.parse(x);
3586
+ if (isNumber(out2))
3587
+ return out2;
3588
+ return NaN;
3589
+ } catch (e) {
3590
+ return NaN;
3591
+ }
3592
+ }
3593
+ var vfloat = vectorize(float);
3594
+ function floor(x) {
3595
+ try {
3596
+ if (!isNumber(x))
3597
+ return NaN;
3598
+ if (typeof x === "bigint") {
3599
+ return x;
3600
+ }
3601
+ return Math.floor(x);
3602
+ } catch (e) {
3603
+ return NaN;
3604
+ }
3605
+ }
3606
+ var vfloor = vectorize(floor);
3607
+ function zeros(shape2) {
3608
+ if (isNumber(shape2))
3609
+ shape2 = [shape2];
3610
+ const out2 = [];
3611
+ const n = product(shape2);
3612
+ for (let i = 0; i < n; i++)
3613
+ out2.push(0);
3614
+ return reshape(out2, shape2);
3615
+ }
3616
+ function identity(size) {
3617
+ if (typeof size === "bigint") {
3618
+ size = vint(size);
3619
+ }
3620
+ assert(!isUndefined(size), "You must pass an integer greater than 0 (representing the size) into the `identity` function!");
3621
+ assert(isNumber(size), "You must pass an integer greater than 0 (representing the size) into the `identity` function!");
3622
+ assert(vint(size) === size, "You must pass an integer greater than 0 (representing the size) into the `identity` function!");
3623
+ assert(size > 0, "You must pass an integer greater than 0 (representing the size) into the `identity` function!");
3624
+ const out2 = zeros([size, size]);
3625
+ for (let i = 0; i < size; i++)
3626
+ out2[i][i] = 1;
3627
+ return out2;
3628
+ }
3629
+ var booleanValues = ["true", "false", "yes", "no"];
3630
+ var nullValues = ["null", "none", "nan", "na", "n/a", "", "undefined"];
3631
+ function checkIfInteger(results) {
3632
+ if (results.type === "number") {
3633
+ if (typeof results.value !== "undefined") {
3634
+ results.isInteger = vint(results.value) === results.value;
3635
+ } else {
3636
+ results.isInteger = every(results.values, (v) => isNumber(v) ? vint(v) === v : true);
3637
+ }
3638
+ }
3639
+ return results;
3640
+ }
3641
+ function inferType(arr) {
3642
+ if (isDataFrame(arr)) {
3643
+ const out2 = arr.copy();
3644
+ const results = inferType(arr.values);
3645
+ out2.values = results.values;
3646
+ return checkIfInteger({ type: results.type, values: out2 });
3647
+ }
3648
+ if (isSeries(arr)) {
3649
+ const out2 = arr.copy();
3650
+ const results = inferType(arr.values);
3651
+ out2.values = results.values;
3652
+ return checkIfInteger({ type: results.type, values: out2 });
3653
+ }
3654
+ if (!isArray(arr)) {
3655
+ const out2 = inferType([arr]);
3656
+ out2.value = out2.values[0];
3657
+ delete out2.values;
3658
+ return checkIfInteger(out2);
3659
+ }
3660
+ assert(isArray(arr), "The `inferType` function only works on arrays, Series, and DataFrames!");
3661
+ const types = flatten(arr).map((v) => {
3662
+ if (v === void 0)
3663
+ return "null";
3664
+ try {
3665
+ if (typeof v === "object") {
3666
+ const temp = new Date(v.getTime());
3667
+ if (isDate(temp)) {
3668
+ return "date";
3669
+ }
3670
+ }
3671
+ } catch (e) {
3672
+ }
3673
+ if (!isString(v)) {
3674
+ if (typeof v === "bigint") {
3675
+ v = v.toString() + "n";
3676
+ } else {
3677
+ v = JSON.stringify(v);
3678
+ }
3679
+ }
3680
+ const vLower = v.toLowerCase();
3681
+ const vLowerTrimmed = vLower.trim();
3682
+ if (nullValues.indexOf(vLowerTrimmed) > -1) {
3683
+ return "null";
3684
+ }
3685
+ if (booleanValues.indexOf(vLowerTrimmed) > -1) {
3686
+ return "boolean";
3687
+ }
3688
+ try {
3689
+ if (v.match(/^-?\d+n$/g)) {
3690
+ return "bigint";
3691
+ }
3692
+ const vParsed = JSON.parse(v);
3693
+ if (isNumber(vParsed)) {
3694
+ return "number";
3695
+ }
3696
+ if (typeof vParsed === "object") {
3697
+ if (isArray(vParsed))
3698
+ return "string";
3699
+ return "object";
3700
+ }
3701
+ return "string";
3702
+ } catch (e) {
3703
+ const vDate = new Date(v);
3704
+ if (isDate(vDate)) {
3705
+ return "date";
3706
+ }
3707
+ return "string";
3708
+ }
3709
+ });
3710
+ const counts = count(types);
3711
+ const sortedValues = counts.values.toSorted((a, b) => counts.get(b) - counts.get(a));
3712
+ const primaryType = sortedValues[0];
3713
+ return checkIfInteger({
3714
+ type: primaryType,
3715
+ values: vapply(arr, (v) => cast(v, primaryType))
3716
+ });
3717
+ }
3718
+ function inverse(x) {
3719
+ if (isDataFrame(x)) {
3720
+ const out2 = x.copy();
3721
+ out2.values = inverse(out2.values);
3722
+ return out2;
3723
+ }
3724
+ assert(isArray(x), "The `inverse` function only works on square 2-dimensional arrays or DataFrames!");
3725
+ const xShape = shape(x);
3726
+ assert(xShape.length === 2, "The array passed into the `inverse` function must be exactly two-dimensional and square!");
3727
+ assert(xShape[0] === xShape[1], "The array passed into the `inverse` function must be exactly two-dimensional and square!");
3728
+ assert(xShape[0] >= 0, "The array passed into the `inverse` function must be exactly two-dimensional and square!");
3729
+ if (xShape[0] === 0) {
3730
+ return x;
3731
+ } else if (xShape[0] === 1) {
3732
+ assert(x[0][0] !== 0, "This matrix cannot be inverted!");
3733
+ let v = x[0][0];
3734
+ if (typeof v === "bigint")
3735
+ v = Number(v);
3736
+ return 1 / v;
3737
+ } else if (xShape[0] === 2) {
3738
+ let a = x[0][0];
3739
+ let b = x[0][1];
3740
+ let c = x[1][0];
3741
+ let d = x[1][1];
3742
+ if (typeof a === "bigint")
3743
+ a = Number(a);
3744
+ if (typeof b === "bigint")
3745
+ b = Number(b);
3746
+ if (typeof c === "bigint")
3747
+ c = Number(c);
3748
+ if (typeof d === "bigint")
3749
+ d = Number(d);
3750
+ const det = a * d - b * c;
3751
+ assert(det !== 0, "This matrix cannot be inverted!");
3752
+ const out2 = [
3753
+ [d, -b],
3754
+ [-c, a]
3755
+ ];
3756
+ return scale(out2, 1 / det);
3757
+ } else if (xShape[0] > 1) {
3758
+ const times = (a, b) => isNumber(a) || isNumber(b) ? scale(a, b) : dot(a, b);
3759
+ for (let divider = 1; divider < xShape[0] - 1; divider++) {
3760
+ try {
3761
+ const A = x.slice(0, divider).map((row) => row.slice(0, divider));
3762
+ const B = x.slice(0, divider).map((row) => row.slice(divider, xShape[0]));
3763
+ const C = x.slice(divider, xShape[0]).map((row) => row.slice(0, divider));
3764
+ const D = x.slice(divider, xShape[0]).map((row) => row.slice(divider, xShape[0]));
3765
+ const AInv = inverse(A);
3766
+ const CompInv = inverse(vadd(D, times(-1, times(times(C, AInv), B))));
3767
+ const topLeft = vadd(AInv, times(times(times(times(AInv, B), CompInv), C), AInv));
3768
+ const topRight = times(-1, times(times(AInv, B), CompInv));
3769
+ const bottomLeft = times(-1, times(times(CompInv, C), AInv));
3770
+ const bottomRight = CompInv;
3771
+ const out2 = topLeft.map((row, i) => row.concat(topRight[i])).concat(bottomLeft.map((row, i) => row.concat(bottomRight[i])));
3772
+ return out2;
3773
+ } catch (e) {
3774
+ }
3775
+ }
3776
+ assert(false, "This matrix cannot be inverted!");
3777
+ }
3778
+ }
3779
+ var isBrowser2 = new Function(`
3780
+ try {
3781
+ return this === window
3782
+ } catch(e) {}
3783
+
3784
+ try {
3785
+ return !!importScripts
3786
+ } catch(e){}
3787
+
3788
+ return false
3789
+ `);
3790
+ function lerp(a, b, f) {
3791
+ try {
3792
+ if (!isNumber(a))
3793
+ return NaN;
3794
+ if (!isNumber(b))
3795
+ return NaN;
3796
+ if (!isNumber(f))
3797
+ return NaN;
3798
+ if (typeof a === "bigint" || typeof b === "bigint") {
3799
+ const out2 = lerp(Number(a), Number(b), f);
3800
+ try {
3801
+ return BigInt(out2);
3802
+ } catch (e) {
3803
+ return out2;
3804
+ }
3805
+ }
3806
+ return f * (b - a) + a;
3807
+ } catch (e) {
3808
+ return NaN;
3809
+ }
3810
+ }
3811
+ var vlerp = vectorize(lerp);
3812
+ function log(x, base) {
3813
+ try {
3814
+ base = isUndefined(base) ? Math.E : base;
3815
+ if (!isNumber(x))
3816
+ return NaN;
3817
+ if (!isNumber(base))
3818
+ return NaN;
3819
+ if (typeof x === "bigint" || typeof base === "bigint") {
3820
+ const out2 = log(Number(x), Number(base));
3821
+ try {
3822
+ return BigInt(out2);
3823
+ } catch (e) {
3824
+ return out2;
3825
+ }
3826
+ }
3827
+ return Math.log(x) / Math.log(base);
3828
+ } catch (e) {
3829
+ return NaN;
3830
+ }
3831
+ }
3832
+ var vlog = vectorize(log);
3833
+ function mean(arr, shouldDropNaNs) {
3834
+ return stats(arr, { shouldDropNaNs }).mean;
3835
+ }
3836
+ function median(arr, shouldDropNaNs) {
3837
+ return stats(arr, { shouldDropNaNs, median: true }).median;
3838
+ }
3839
+ function mod(a, b) {
3840
+ try {
3841
+ if (!isNumber(a))
3842
+ return NaN;
3843
+ if (!isNumber(b))
3844
+ return NaN;
3845
+ if (typeof a === "bigint" || typeof b === "bigint") {
3846
+ const out2 = mod(Number(a), Number(b));
3847
+ try {
3848
+ return BigInt(out2);
3849
+ } catch (e) {
3850
+ return out2;
3851
+ }
3852
+ }
3853
+ return a % b;
3854
+ } catch (e) {
3855
+ return NaN;
3856
+ }
3857
+ }
3858
+ var vmod = vectorize(mod);
3859
+ function mode(arr, shouldDropNaNs) {
3860
+ return stats(arr, { shouldDropNaNs, mode: true }).mode;
3861
+ }
3862
+ function helper3() {
3863
+ const u1 = random();
3864
+ const u2 = random();
3865
+ return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
3866
+ }
3867
+ function normal(shape2) {
3868
+ if (isUndefined(shape2))
3869
+ return helper3();
3870
+ return vapply(ndarray(shape2), helper3);
3871
+ }
3872
+ function ones(shape2) {
3873
+ return vapply(ndarray(shape2), () => 1);
3874
+ }
3875
+ function permutationsIterator(x, r) {
3876
+ function* helper5(x2, r2) {
3877
+ r2 = r2 || x2.length;
3878
+ if (x2.length === 1) {
3879
+ yield [x2];
3880
+ return;
3881
+ }
3882
+ for (const c of combinations(x2, r2)) {
3883
+ if (!c.slice)
3884
+ continue;
3885
+ const state = zeros(c.length);
3886
+ yield c;
3887
+ let i = 1;
3888
+ while (i < c.length) {
3889
+ if (state[i] < i) {
3890
+ if (i % 2 === 0) {
3891
+ const buf = c[0];
3892
+ c[0] = c[i];
3893
+ c[i] = buf;
3894
+ } else {
3895
+ const buf = c[state[i]];
3896
+ c[state[i]] = c[i];
3897
+ c[i] = buf;
3898
+ }
3899
+ yield c;
3900
+ state[i] += 1;
3901
+ i = 1;
3902
+ } else {
3903
+ state[i] = 0;
3904
+ i += 1;
3905
+ }
3906
+ }
3907
+ }
3908
+ }
3909
+ if (isDataFrame(x) || isSeries(x)) {
3910
+ return permutationsIterator(x.values, r);
3911
+ }
3912
+ assert(isArray(x), "The `permutations` function only works on arrays, Series, and DataFrames!");
3913
+ if (isUndefined(r)) {
3914
+ r = x.length;
3915
+ }
3916
+ assert(isNumber(r) && vint(r) === r && r >= 0, "`r` must be a non-negative integer!");
3917
+ return helper5(flatten(x), r);
3918
+ }
3919
+ function permutations(x, r) {
3920
+ const out2 = [];
3921
+ for (const perm of permutationsIterator(x, r)) {
3922
+ out2.push(perm.slice());
3923
+ }
3924
+ return out2;
3925
+ }
3926
+ function print() {
3927
+ Object.keys(arguments).forEach((key) => {
3928
+ const x = arguments[key];
3929
+ if (isArray(x)) {
3930
+ if (!isJagged(x)) {
3931
+ const xShape = shape(x);
3932
+ if (xShape.length === 1) {
3933
+ new Series(x).print();
3934
+ } else if (xShape.length == 2) {
3935
+ new DataFrame(x).print();
3936
+ } else {
3937
+ console.log(x);
3938
+ }
3939
+ } else {
3940
+ console.log(x);
3941
+ }
3942
+ } else if (isDataFrame(x) || isSeries(x)) {
3943
+ x.print();
3944
+ } else {
3945
+ console.log(x);
3946
+ }
3947
+ });
3948
+ }
3949
+ var helper4 = vectorize((x, a, b, c, d) => {
3950
+ try {
3951
+ let resultShouldBeABigInt = false;
3952
+ for (const v of [x, a, b, c, d]) {
3953
+ if (!isNumber(v)) {
3954
+ return NaN;
3955
+ }
3956
+ if (typeof v === "bigint") {
3957
+ resultShouldBeABigInt = true;
3958
+ }
3959
+ }
3960
+ if (resultShouldBeABigInt) {
3961
+ x = Number(x);
3962
+ a = Number(a);
3963
+ b = Number(b);
3964
+ c = Number(c);
3965
+ d = Number(d);
3966
+ }
3967
+ const num = (d - c) * (x - a);
3968
+ const den = b - a;
3969
+ if (den === 0)
3970
+ return NaN;
3971
+ const out2 = num / den + c;
3972
+ if (resultShouldBeABigInt) {
3973
+ try {
3974
+ return BigInt(out2);
3975
+ } catch (e) {
3976
+ }
3977
+ }
3978
+ return out2;
3979
+ } catch (e) {
3980
+ return NaN;
3981
+ }
3982
+ });
3983
+ function remap(x, a, b, c, d) {
3984
+ if (isArray(x) && isUndefined(c) && isUndefined(d)) {
3985
+ c = a;
3986
+ d = b;
3987
+ const results = stats(x);
3988
+ a = results.min;
3989
+ b = results.max;
3990
+ }
3991
+ return helper4(x, a, b, c, d);
3992
+ }
3993
+ function round(x) {
3994
+ try {
3995
+ if (!isNumber(x))
3996
+ return NaN;
3997
+ if (typeof x === "bigint")
3998
+ return x;
3999
+ return Math.round(x);
4000
+ } catch (e) {
4001
+ return NaN;
4002
+ }
4003
+ }
4004
+ var vround = vectorize(round);
4005
+ function sign(x) {
4006
+ try {
4007
+ if (!isNumber(x))
4008
+ return NaN;
4009
+ if (typeof x === "bigint")
4010
+ return BigInt(sign(Number(x)));
4011
+ if (x < 0)
4012
+ return -1;
4013
+ if (x > 0)
4014
+ return 1;
4015
+ return 0;
4016
+ } catch (e) {
4017
+ return NaN;
4018
+ }
4019
+ }
4020
+ var vsign = vectorize(sign);
4021
+ function sin(x) {
4022
+ try {
4023
+ if (!isNumber(x))
4024
+ return NaN;
4025
+ if (typeof x === "bigint") {
4026
+ x = Number(x);
4027
+ }
4028
+ return Math.sin(x);
4029
+ } catch (e) {
4030
+ return NaN;
4031
+ }
4032
+ }
4033
+ var vsin = vectorize(sin);
4034
+ function some(x, fn) {
4035
+ if (isDataFrame(x) || isSeries(x)) {
4036
+ return some(x.values, fn);
4037
+ }
4038
+ assert(isArray(x), "The first argument passed into the `some` function must be an array, Series, or DataFrame!");
4039
+ assert(isFunction(fn), "The second argument passed into the `some` function must be a function!");
4040
+ for (const v of x) {
4041
+ if (isArray(v)) {
4042
+ if (some(v, fn)) {
4043
+ return true;
4044
+ }
4045
+ } else {
4046
+ if (fn(v)) {
4047
+ return true;
4048
+ }
4049
+ }
4050
+ }
4051
+ return false;
4052
+ }
4053
+ function std(arr, shouldDropNaNs) {
4054
+ return stats(arr, { shouldDropNaNs, stdev: true }).stdev;
4055
+ }
4056
+ function stdev(x) {
4057
+ return std(x);
4058
+ }
4059
+ function tan(x) {
4060
+ try {
4061
+ if (!isNumber(x))
4062
+ return NaN;
4063
+ if (typeof x === "bigint") {
4064
+ x = Number(x);
4065
+ }
4066
+ return Math.tan(x);
4067
+ } catch (e) {
4068
+ return NaN;
4069
+ }
4070
+ }
4071
+ var vtan = vectorize(tan);
4072
+ function timeSync(fn, args) {
4073
+ assert(isFunction(fn), "`fn` must be a function!");
4074
+ const start = /* @__PURE__ */ new Date();
4075
+ if (args) {
4076
+ fn(...args);
4077
+ } else {
4078
+ fn();
4079
+ }
4080
+ return /* @__PURE__ */ new Date() - start;
4081
+ }
4082
+ async function timeAsync(fn, args) {
4083
+ assert(isFunction(fn), "`fn` must be a function!");
4084
+ const start = /* @__PURE__ */ new Date();
4085
+ if (args) {
4086
+ await fn(...args);
4087
+ } else {
4088
+ await fn();
4089
+ }
4090
+ return /* @__PURE__ */ new Date() - start;
4091
+ }
4092
+ function union() {
4093
+ return set([...arguments].map((v) => {
4094
+ if (isArray(v))
4095
+ return v;
4096
+ if (isDataFrame(v))
4097
+ return v.values;
4098
+ if (isSeries(v))
4099
+ return v.values;
4100
+ return [v];
4101
+ }));
4102
+ }
4103
+ function variance(arr, shouldDropNaNs) {
4104
+ return stats(arr, { shouldDropNaNs, variance: true }).variance;
4105
+ }
4106
+ function zip() {
4107
+ const out2 = [];
4108
+ const arrays = Object.values(arguments).map((arr) => {
4109
+ if (isDataFrame(arr) || isSeries(arr)) {
4110
+ arr = arr.values;
4111
+ }
4112
+ assert(isArray(arr), "The `zip` function only works on arrays, Series, and DataFrames!");
4113
+ return arr;
4114
+ });
4115
+ range(0, max(arrays.map((arr) => arr.length))).forEach((i) => {
4116
+ const row = [];
4117
+ arrays.forEach((arr) => {
4118
+ const value = arr[i];
4119
+ row.push(isUndefined(value) ? void 0 : value);
4120
+ });
4121
+ out2.push(row);
4122
+ });
4123
+ return out2;
4124
+ }
4125
+ var out = {
4126
+ abs: vabs,
4127
+ add: vadd,
4128
+ apply: vapply,
4129
+ arccos: varccos,
4130
+ arcsin: varcsin,
4131
+ arctan: varctan,
4132
+ argmax,
4133
+ argmin,
4134
+ assert,
4135
+ cast,
4136
+ ceil: vceil,
4137
+ chop: vchop,
4138
+ clamp: vclamp,
4139
+ combinations,
4140
+ combinationsIterator,
4141
+ copy,
4142
+ correl,
4143
+ cos: vcos,
4144
+ count,
4145
+ covariance,
4146
+ DataFrame,
4147
+ dataTypes,
4148
+ decycle,
4149
+ diff,
4150
+ distance,
4151
+ divide,
4152
+ dot,
4153
+ dropMissing,
4154
+ dropMissingPairwise,
4155
+ dropNaN,
4156
+ dropNaNPairwise,
4157
+ dropUndefined,
4158
+ every,
4159
+ exp: vexp,
4160
+ factorial: vfactorial,
4161
+ find,
4162
+ findAll,
4163
+ flatten,
4164
+ float: vfloat,
4165
+ floor: vfloor,
4166
+ identity,
4167
+ IndexMatcher,
4168
+ indexOf,
4169
+ inferType,
4170
+ int: vint,
4171
+ intersect,
4172
+ inverse,
4173
+ isArray,
4174
+ isBoolean,
4175
+ isBrowser: isBrowser2,
4176
+ isDataFrame,
4177
+ isDate,
4178
+ isEqual,
4179
+ isFunction,
4180
+ isJagged,
4181
+ isNested,
4182
+ isNumber,
4183
+ isObject,
4184
+ isSeries,
4185
+ isString,
4186
+ isUndefined,
4187
+ lerp: vlerp,
4188
+ log: vlog,
4189
+ MathError,
4190
+ max,
4191
+ mean,
4192
+ median,
4193
+ min,
4194
+ mod: vmod,
4195
+ mode,
4196
+ multiply: vmultiply,
4197
+ ndarray,
4198
+ normal,
4199
+ ones,
4200
+ permutations,
4201
+ permutationsIterator,
4202
+ pow: vpow,
4203
+ print,
4204
+ product,
4205
+ random,
4206
+ range,
4207
+ remap,
4208
+ reshape,
4209
+ reverse,
4210
+ round: vround,
4211
+ scale,
4212
+ seed,
4213
+ Series,
4214
+ set,
4215
+ shape,
4216
+ shuffle,
4217
+ sign: vsign,
4218
+ sin: vsin,
4219
+ some,
4220
+ sort,
4221
+ sqrt: vsqrt,
4222
+ stats,
4223
+ std,
4224
+ stdev,
4225
+ subtract,
4226
+ sum,
4227
+ tan: vtan,
4228
+ timeAsync,
4229
+ timeSync,
4230
+ time: timeSync,
4231
+ transpose,
4232
+ union,
4233
+ variance,
4234
+ vectorize,
4235
+ zeros,
4236
+ zip,
4237
+ dump() {
4238
+ const context = typeof globalThis !== "undefined" ? globalThis : typeof global !== "undefined" ? global : typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : void 0;
4239
+ if (!context) {
4240
+ throw new out.MathError("Cannot dump functions into global scope because none of `globalThis`, `global`, `window`, or `self` exist in the current context!");
4241
+ }
4242
+ Object.keys(out).forEach((key) => {
4243
+ try {
4244
+ Object.defineProperty(context, key, {
4245
+ configurable: false,
4246
+ enumerable: true,
4247
+ writable: false,
4248
+ value: out[key]
4249
+ });
4250
+ } catch (e) {
4251
+ context[key] = out[key];
4252
+ }
4253
+ });
4254
+ }
4255
+ };
4256
+ if (typeof window !== "undefined") {
4257
+ window.JSMathTools = out;
4258
+ }
4259
+
4260
+ // src/index.mjs
4261
+ function makeKey4(keyLength, keySeed, charset) {
4262
+ if (arguments.length === 2) {
4263
+ if (isNumber(arguments[1])) {
4264
+ charset = null;
4265
+ } else {
4266
+ charset = keySeed;
4267
+ keySeed = null;
4268
+ }
4269
+ }
4270
+ assert(
4271
+ isNumber(keyLength) && vint(keyLength) === keyLength,
4272
+ "`keyLength` must be an integer!"
4273
+ );
4274
+ if (keySeed) {
4275
+ assert(
4276
+ isNumber(keySeed) && vint(keySeed) === keySeed,
4277
+ "`keySeed` must be an integer!"
4278
+ );
4279
+ seed(keySeed);
4280
+ }
4281
+ if (charset) {
4282
+ assert(isString(charset), "`charset` must be a string!");
4283
+ }
4284
+ let out2 = "";
4285
+ charset = charset || "abcdefg1234567890";
4286
+ for (let i = 0; i < keyLength; i++) {
4287
+ out2 += charset[vint(random() * charset.length)];
4288
+ }
4289
+ return out2;
4290
+ }
4291
+ if (typeof window !== "undefined") {
4292
+ window.makeKey = makeKey4;
4293
+ }
4294
+ // Annotate the CommonJS export names for ESM import in node:
4295
+ 0 && (module.exports = {
4296
+ makeKey
4297
+ });