df-script 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +83 -34
  2. package/dist/api.d.ts +16 -8
  3. package/dist/api.js +11 -3
  4. package/dist/columnExpressions/ColumnExpr.d.ts +26 -364
  5. package/dist/columnExpressions/ColumnExpr.js +199 -3
  6. package/dist/columnExpressions/ExprBase.d.ts +8 -11
  7. package/dist/columnExpressions/ExprBase.js +20 -61
  8. package/dist/columnExpressions/constants.d.ts +2 -0
  9. package/dist/columnExpressions/constants.js +3 -1
  10. package/dist/columnExpressions/functions/coalesce.js +8 -4
  11. package/dist/columnExpressions/functions/element.d.ts +5 -0
  12. package/dist/columnExpressions/functions/element.js +11 -0
  13. package/dist/columnExpressions/functions/implode.d.ts +3 -0
  14. package/dist/columnExpressions/functions/implode.js +7 -0
  15. package/dist/columnExpressions/functions/lit.d.ts +3 -1
  16. package/dist/columnExpressions/functions/lit.js +8 -13
  17. package/dist/columnExpressions/functions/repeat.d.ts +47 -0
  18. package/dist/columnExpressions/functions/repeat.js +131 -0
  19. package/dist/columnExpressions/functions/seq_range.d.ts +31 -0
  20. package/dist/columnExpressions/functions/seq_range.js +91 -0
  21. package/dist/columnExpressions/functions/when.js +8 -4
  22. package/dist/columnExpressions/index.d.ts +5 -2
  23. package/dist/columnExpressions/index.js +5 -48
  24. package/dist/columnExpressions/mixins/AggregationExpr.d.ts +25 -50
  25. package/dist/columnExpressions/mixins/AggregationExpr.js +69 -165
  26. package/dist/columnExpressions/mixins/ArithmeticExpr.d.ts +41 -66
  27. package/dist/columnExpressions/mixins/ArithmeticExpr.js +126 -138
  28. package/dist/columnExpressions/mixins/BooleanExpr.d.ts +40 -0
  29. package/dist/columnExpressions/mixins/BooleanExpr.js +67 -0
  30. package/dist/columnExpressions/mixins/ComparisonExpr.d.ts +31 -60
  31. package/dist/columnExpressions/mixins/ComparisonExpr.js +231 -370
  32. package/dist/columnExpressions/mixins/ListExpr.d.ts +15 -39
  33. package/dist/columnExpressions/mixins/ListExpr.js +124 -148
  34. package/dist/columnExpressions/mixins/LogicalExpr.d.ts +7 -33
  35. package/dist/columnExpressions/mixins/LogicalExpr.js +48 -49
  36. package/dist/columnExpressions/mixins/ManipulationExpr.d.ts +6 -31
  37. package/dist/columnExpressions/mixins/ManipulationExpr.js +61 -25
  38. package/dist/columnExpressions/mixins/NumericExpr.d.ts +92 -0
  39. package/dist/columnExpressions/mixins/NumericExpr.js +259 -0
  40. package/dist/columnExpressions/mixins/StringExpr.d.ts +5 -30
  41. package/dist/columnExpressions/mixins/StringExpr.js +37 -49
  42. package/dist/columnExpressions/mixins/StructExpr.d.ts +16 -0
  43. package/dist/columnExpressions/mixins/StructExpr.js +139 -0
  44. package/dist/columnExpressions/mixins/TemporalExpr.d.ts +4 -30
  45. package/dist/columnExpressions/mixins/TemporalExpr.js +28 -29
  46. package/dist/columnExpressions/mixins/WindowExpr.d.ts +27 -49
  47. package/dist/columnExpressions/mixins/WindowExpr.js +144 -274
  48. package/dist/columnExpressions/utils.d.ts +4 -0
  49. package/dist/columnExpressions/utils.js +45 -0
  50. package/dist/dataframe/constants.d.ts +3 -0
  51. package/dist/dataframe/constants.js +4 -1
  52. package/dist/dataframe/dataframe.d.ts +27 -10
  53. package/dist/dataframe/dataframe.js +461 -220
  54. package/dist/dataframe/grouped/grouped.d.ts +2 -3
  55. package/dist/dataframe/grouped/grouped.js +17 -16
  56. package/dist/dataframe/types.d.ts +65 -1
  57. package/dist/dataframe/utils.d.ts +11 -3
  58. package/dist/dataframe/utils.js +101 -9
  59. package/dist/datatypes/DataType.d.ts +2 -1
  60. package/dist/datatypes/DataType.js +29 -0
  61. package/dist/datatypes/index.d.ts +2 -2
  62. package/dist/datatypes/types.d.ts +37 -36
  63. package/dist/datatypes/types.js +15 -11
  64. package/dist/exceptions/index.d.ts +2 -0
  65. package/dist/exceptions/index.js +4 -1
  66. package/dist/exceptions/utils.d.ts +1 -0
  67. package/dist/exceptions/utils.js +19 -0
  68. package/dist/functions/concat.d.ts +1 -2
  69. package/dist/functions/concat.js +24 -12
  70. package/dist/functions/index.d.ts +3 -1
  71. package/dist/functions/index.js +7 -15
  72. package/dist/functions/read_csv.d.ts +8 -0
  73. package/dist/functions/read_csv.js +53 -0
  74. package/dist/functions/read_json.d.ts +10 -0
  75. package/dist/functions/read_json.js +21 -0
  76. package/dist/functions/transpose.d.ts +7 -0
  77. package/dist/functions/transpose.js +84 -0
  78. package/dist/index.js +6 -1
  79. package/dist/types.d.ts +31 -5
  80. package/dist/utils/csv.d.ts +77 -0
  81. package/dist/utils/csv.js +313 -0
  82. package/dist/utils/date.js +5 -6
  83. package/dist/utils/guards.d.ts +0 -2
  84. package/dist/utils/guards.js +0 -31
  85. package/dist/utils/index.d.ts +2 -0
  86. package/dist/utils/index.js +2 -0
  87. package/dist/utils/json.d.ts +120 -2
  88. package/dist/utils/json.js +204 -20
  89. package/dist/utils/list.d.ts +173 -1
  90. package/dist/utils/list.js +255 -22
  91. package/dist/utils/number.d.ts +61 -28
  92. package/dist/utils/number.js +248 -70
  93. package/dist/utils/string.d.ts +5 -0
  94. package/dist/utils/string.js +89 -0
  95. package/package.json +5 -1
@@ -8,7 +8,7 @@ const utils_1 = require("../utils");
8
8
  // ============================================================================
9
9
  class Int8Type extends DataType_1.SignedIntegerType {
10
10
  name = "Int8";
11
- coerce(val) { return (0, utils_1.toValidInt)(val, "Int8"); }
11
+ coerce(val) { return (0, utils_1.toValidInt)(val, { range: "Int8" }); }
12
12
  equals(other) { return other.name === "Int8"; }
13
13
  allocate(size) { return new Int8Array(size); }
14
14
  }
@@ -16,7 +16,7 @@ exports.Int8Type = Int8Type;
16
16
  exports.Int8 = new Int8Type();
17
17
  class Int16Type extends DataType_1.SignedIntegerType {
18
18
  name = "Int16";
19
- coerce(val) { return (0, utils_1.toValidInt)(val, "Int16"); }
19
+ coerce(val) { return (0, utils_1.toValidInt)(val, { range: "Int16" }); }
20
20
  equals(other) { return other.name === "Int16"; }
21
21
  allocate(size) { return new Int16Array(size); }
22
22
  }
@@ -24,7 +24,7 @@ exports.Int16Type = Int16Type;
24
24
  exports.Int16 = new Int16Type();
25
25
  class Int32Type extends DataType_1.SignedIntegerType {
26
26
  name = "Int32";
27
- coerce(val) { return (0, utils_1.toValidInt)(val, "Int32"); }
27
+ coerce(val) { return (0, utils_1.toValidInt)(val, { range: "Int32" }); }
28
28
  equals(other) { return other.name === "Int32"; }
29
29
  allocate(size) { return new Int32Array(size); }
30
30
  }
@@ -32,7 +32,7 @@ exports.Int32Type = Int32Type;
32
32
  exports.Int32 = new Int32Type();
33
33
  class Int64Type extends DataType_1.SignedIntegerType {
34
34
  name = "Int64";
35
- coerce(val) { return (0, utils_1.toValidBigInt)(val); }
35
+ coerce(val) { return (0, utils_1.toValidBigInt)(val, { truncate: true }); }
36
36
  equals(other) { return other.name === "Int64"; }
37
37
  allocate(size) { return new BigInt64Array(size); }
38
38
  }
@@ -40,7 +40,7 @@ exports.Int64Type = Int64Type;
40
40
  exports.Int64 = new Int64Type();
41
41
  class UInt8Type extends DataType_1.UnsignedIntegerType {
42
42
  name = "UInt8";
43
- coerce(val) { return (0, utils_1.toValidInt)(val, "UInt8"); }
43
+ coerce(val) { return (0, utils_1.toValidInt)(val, { range: "UInt8" }); }
44
44
  equals(other) { return other.name === "UInt8"; }
45
45
  allocate(size) { return new Uint8Array(size); }
46
46
  }
@@ -48,7 +48,7 @@ exports.UInt8Type = UInt8Type;
48
48
  exports.UInt8 = new UInt8Type();
49
49
  class UInt16Type extends DataType_1.UnsignedIntegerType {
50
50
  name = "UInt16";
51
- coerce(val) { return (0, utils_1.toValidInt)(val, "UInt16"); }
51
+ coerce(val) { return (0, utils_1.toValidInt)(val, { range: "UInt16" }); }
52
52
  equals(other) { return other.name === "UInt16"; }
53
53
  allocate(size) { return new Uint16Array(size); }
54
54
  }
@@ -56,7 +56,7 @@ exports.UInt16Type = UInt16Type;
56
56
  exports.UInt16 = new UInt16Type();
57
57
  class UInt32Type extends DataType_1.UnsignedIntegerType {
58
58
  name = "UInt32";
59
- coerce(val) { return (0, utils_1.toValidInt)(val, "UInt32"); }
59
+ coerce(val) { return (0, utils_1.toValidInt)(val, { range: "UInt32" }); }
60
60
  equals(other) { return other.name === "UInt32"; }
61
61
  allocate(size) { return new Uint32Array(size); }
62
62
  }
@@ -64,7 +64,7 @@ exports.UInt32Type = UInt32Type;
64
64
  exports.UInt32 = new UInt32Type();
65
65
  class UInt64Type extends DataType_1.UnsignedIntegerType {
66
66
  name = "UInt64";
67
- coerce(val) { return (0, utils_1.toValidBigInt)(val, "UInt64"); }
67
+ coerce(val) { return (0, utils_1.toValidBigInt)(val, { range: "UInt64" }); }
68
68
  equals(other) { return other.name === "UInt64"; }
69
69
  allocate(size) { return new BigUint64Array(size); }
70
70
  }
@@ -72,7 +72,7 @@ exports.UInt64Type = UInt64Type;
72
72
  exports.UInt64 = new UInt64Type();
73
73
  class Float32Type extends DataType_1.FloatDataType {
74
74
  name = "Float32";
75
- coerce(val) { return (0, utils_1.toValidFloat)(val, "Float32"); }
75
+ coerce(val) { return (0, utils_1.toValidFloat)(val, { floatPrecision: "Float32" }); }
76
76
  equals(other) { return other.name === "Float32"; }
77
77
  allocate(size) { return new Float32Array(size); }
78
78
  }
@@ -80,7 +80,7 @@ exports.Float32Type = Float32Type;
80
80
  exports.Float32 = new Float32Type();
81
81
  class Float64Type extends DataType_1.FloatDataType {
82
82
  name = "Float64";
83
- coerce(val) { return (0, utils_1.toValidFloat)(val, "Float64"); }
83
+ coerce(val) { return (0, utils_1.toValidFloat)(val, { floatPrecision: "Float64" }); }
84
84
  equals(other) { return other.name === "Float64"; }
85
85
  allocate(size) { return new Float64Array(size); }
86
86
  }
@@ -261,7 +261,11 @@ class StructType extends DataType_1.NestedDataType {
261
261
  if (!(0, utils_1.isObj)(val))
262
262
  return null;
263
263
  const res = {};
264
- for (const [k, type] of globalThis.Object.entries(this.fields)) {
264
+ const keys = globalThis.Object.keys(this.fields);
265
+ const len = keys.length;
266
+ for (let i = 0; i < len; i++) {
267
+ const k = keys[i];
268
+ const type = this.fields[k];
265
269
  res[k] = type.coerce(val[k]);
266
270
  }
267
271
  return res;
@@ -10,4 +10,6 @@ export declare class SchemaError extends DFScriptError {
10
10
  }
11
11
  export declare class ComputeError extends DFScriptError {
12
12
  }
13
+ export declare class ShapeError extends DFScriptError {
14
+ }
13
15
  export * from "./utils";
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.ComputeError = exports.SchemaError = exports.ColumnNotFoundError = exports.DataFrameError = exports.DFScriptError = void 0;
17
+ exports.ShapeError = exports.ComputeError = exports.SchemaError = exports.ColumnNotFoundError = exports.DataFrameError = exports.DFScriptError = void 0;
18
18
  class DFScriptError extends Error {
19
19
  constructor(message) {
20
20
  super(message);
@@ -40,4 +40,7 @@ exports.SchemaError = SchemaError;
40
40
  class ComputeError extends DFScriptError {
41
41
  }
42
42
  exports.ComputeError = ComputeError;
43
+ class ShapeError extends DFScriptError {
44
+ }
45
+ exports.ShapeError = ShapeError;
43
46
  __exportStar(require("./utils"), exports);
@@ -1,2 +1,3 @@
1
1
  import type { ColumnDict } from "../types";
2
2
  export declare function assertColumnExists(columnName: string, columns: ColumnDict, context: string, suffix?: string): void;
3
+ export declare function assertHeight(columns: ColumnDict, height?: number): number;
@@ -1,9 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.assertColumnExists = assertColumnExists;
4
+ exports.assertHeight = assertHeight;
4
5
  const index_1 = require("./index");
6
+ const utils_1 = require("../utils");
5
7
  function assertColumnExists(columnName, columns, context, suffix = "") {
6
8
  if (!(columnName in columns)) {
7
9
  throw new index_1.ColumnNotFoundError(columnName, `${context} "${columnName}" does not exist${suffix}`);
8
10
  }
9
11
  }
12
+ function assertHeight(columns, height) {
13
+ const keys = Object.keys(columns);
14
+ const numKeys = keys.length;
15
+ let expectedHeight = height;
16
+ for (let i = 0; i < numKeys; i++) {
17
+ const key = keys[i];
18
+ const col = columns[key];
19
+ const len = (0, utils_1.isArrayOrTypedArray)(col) ? col.length : 0;
20
+ if (expectedHeight === undefined) {
21
+ expectedHeight = len;
22
+ }
23
+ else if (len !== expectedHeight) {
24
+ throw new index_1.ShapeError(`Column height mismatch: Column "${key}" has length ${len}, but DataFrame height is ${expectedHeight}`);
25
+ }
26
+ }
27
+ return expectedHeight === undefined ? 0 : expectedHeight;
28
+ }
@@ -1,4 +1,3 @@
1
1
  import { DataFrame } from "../dataframe/dataframe";
2
2
  import type { ConcatOptions, ConcatItem, RowRecord } from "../types";
3
- export declare function normalizeToDataFrames(item: any, context: string, index: number): DataFrame<any>[];
4
- export declare function concat<U extends RowRecord = any>(rawItems: ConcatItem | ConcatItem[], options?: ConcatOptions): DataFrame<U>;
3
+ export declare function concat<U extends RowRecord = any>(rawItems: ConcatItem | ConcatItem[], { how, horizontal }?: ConcatOptions): DataFrame<U>;
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeToDataFrames = normalizeToDataFrames;
4
3
  exports.concat = concat;
5
4
  const dataframe_1 = require("../dataframe/dataframe");
6
5
  const datatypes_1 = require("../datatypes");
@@ -34,7 +33,7 @@ function normalizeToDataFrames(item, context, index) {
34
33
  }
35
34
  throw new exceptions_1.DataFrameError(`Invalid input to ${context} at index ${index}: expected DataFrame, row array, or column dictionary.`);
36
35
  }
37
- function concat(rawItems, options = {}) {
36
+ function concat(rawItems, { how = 'vertical', horizontal } = {}) {
38
37
  if (rawItems == null) {
39
38
  throw new exceptions_1.DataFrameError("Invalid input to concat: rawItems cannot be null or undefined.");
40
39
  }
@@ -43,10 +42,9 @@ function concat(rawItems, options = {}) {
43
42
  for (let i = 0; i < itemsArray.length; i++) {
44
43
  items.push(...normalizeToDataFrames(itemsArray[i], "concat", i));
45
44
  }
46
- const { how = 'vertical' } = options;
47
- const strict = options.horizontal?.strict ?? true;
45
+ const strict = horizontal?.strict ?? true;
48
46
  if (items.length === 0)
49
- return new dataframe_1.DataFrame({}, {}, 0);
47
+ return dataframe_1.DataFrame._createDirect({}, {}, 0);
50
48
  if (items.length === 1 && how !== 'horizontal')
51
49
  return items[0];
52
50
  switch (how) {
@@ -58,7 +56,7 @@ function concat(rawItems, options = {}) {
58
56
  }
59
57
  }
60
58
  if (validItems.length === 0)
61
- return new dataframe_1.DataFrame({}, {}, 0);
59
+ return dataframe_1.DataFrame._createDirect({}, {}, 0);
62
60
  const firstDF = validItems[0];
63
61
  const firstKeys = Object.keys(firstDF._columns);
64
62
  for (let i = 0; i < items.length; i++) {
@@ -85,7 +83,17 @@ function concat(rawItems, options = {}) {
85
83
  for (const item of items) {
86
84
  newHeight += item.height;
87
85
  }
88
- const outSchema = items.find(df => df.height > 0)?.schema || items[0]?.schema || {};
86
+ let outSchema = {};
87
+ const itemsLen = items.length;
88
+ for (let i = 0; i < itemsLen; i++) {
89
+ if (items[i].height > 0) {
90
+ outSchema = items[i].schema;
91
+ break;
92
+ }
93
+ }
94
+ if (Object.keys(outSchema).length === 0 && itemsLen > 0) {
95
+ outSchema = items[0].schema;
96
+ }
89
97
  const newColumns = {};
90
98
  for (const key of firstKeys) {
91
99
  const type = outSchema[key];
@@ -133,7 +141,7 @@ function concat(rawItems, options = {}) {
133
141
  }
134
142
  offset += h;
135
143
  }
136
- return new dataframe_1.DataFrame(newColumns, outSchema, newHeight);
144
+ return dataframe_1.DataFrame._createDirect(newColumns, outSchema, newHeight);
137
145
  }
138
146
  case 'horizontal': {
139
147
  let maxHeight = items[0].height;
@@ -146,7 +154,7 @@ function concat(rawItems, options = {}) {
146
154
  for (let idx = 0; idx < items.length; idx++) {
147
155
  const df = items[idx];
148
156
  if (strict && df.height !== maxHeight) {
149
- throw new exceptions_1.DataFrameError(`[Horizontal] Row count mismatch at index ${idx}. Expected ${maxHeight}, got ${df.height}. Set strict=false to allow padding.`);
157
+ throw new exceptions_1.ShapeError(`[Horizontal] Row count mismatch at index ${idx}. Expected ${maxHeight}, got ${df.height}. Set strict=false to allow padding.`);
150
158
  }
151
159
  for (const key of Object.keys(df._columns)) {
152
160
  if (allColNames.has(key)) {
@@ -160,7 +168,11 @@ function concat(rawItems, options = {}) {
160
168
  for (const df of items) {
161
169
  const h = df.height;
162
170
  Object.assign(outSchema, df.schema);
163
- for (const [key, col] of Object.entries(df._columns)) {
171
+ const keys = Object.keys(df._columns);
172
+ const numKeys = keys.length;
173
+ for (let idxKey = 0; idxKey < numKeys; idxKey++) {
174
+ const key = keys[idxKey];
175
+ const col = df._columns[key];
164
176
  if (h === maxHeight) {
165
177
  newColumns[key] = (0, utils_1.isTypedArray)(col) ? Array.from(col) : col;
166
178
  }
@@ -176,7 +188,7 @@ function concat(rawItems, options = {}) {
176
188
  }
177
189
  }
178
190
  }
179
- return new dataframe_1.DataFrame(newColumns, outSchema, maxHeight);
191
+ return dataframe_1.DataFrame._createDirect(newColumns, outSchema, maxHeight);
180
192
  }
181
193
  case 'diagonal': {
182
194
  const allColumnsSet = new Set();
@@ -242,7 +254,7 @@ function concat(rawItems, options = {}) {
242
254
  }
243
255
  offset += h;
244
256
  }
245
- return new dataframe_1.DataFrame(newColumns, outSchema, newHeight);
257
+ return dataframe_1.DataFrame._createDirect(newColumns, outSchema, newHeight);
246
258
  }
247
259
  }
248
260
  }
@@ -1 +1,3 @@
1
- export * from "./concat";
1
+ export { concat } from "./concat";
2
+ export { read_json } from "./read_json";
3
+ export { read_csv } from "./read_csv";
@@ -1,17 +1,9 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./concat"), exports);
3
+ exports.read_csv = exports.read_json = exports.concat = void 0;
4
+ var concat_1 = require("./concat");
5
+ Object.defineProperty(exports, "concat", { enumerable: true, get: function () { return concat_1.concat; } });
6
+ var read_json_1 = require("./read_json");
7
+ Object.defineProperty(exports, "read_json", { enumerable: true, get: function () { return read_json_1.read_json; } });
8
+ var read_csv_1 = require("./read_csv");
9
+ Object.defineProperty(exports, "read_csv", { enumerable: true, get: function () { return read_csv_1.read_csv; } });
@@ -0,0 +1,8 @@
1
+ import { DataFrame } from "../dataframe/dataframe";
2
+ import type { ReadCSVOptions } from "../dataframe/types";
3
+ import type { RowRecord } from "../types";
4
+ /**
5
+ * Reads a CSV string and constructs a DataFrame.
6
+ * Automatically infers column data types unless an explicit schema is provided.
7
+ */
8
+ export declare function read_csv<T extends RowRecord = any>(content: string, options?: ReadCSVOptions): DataFrame<T>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.read_csv = read_csv;
4
+ const dataframe_1 = require("../dataframe/dataframe");
5
+ const utils_1 = require("../utils");
6
+ /**
7
+ * Reads a CSV string and constructs a DataFrame.
8
+ * Automatically infers column data types unless an explicit schema is provided.
9
+ */
10
+ function read_csv(content, options = {}) {
11
+ const { hasHeader = true, schema, inferSchema = true, } = options;
12
+ const rows = (0, utils_1.parseCSV)(content, options);
13
+ if (rows.length === 0) {
14
+ return new dataframe_1.DataFrame({});
15
+ }
16
+ let headers;
17
+ let dataRows;
18
+ if (hasHeader) {
19
+ headers = rows[0];
20
+ dataRows = rows.slice(1);
21
+ }
22
+ else {
23
+ headers = rows[0].map((_, i) => `column_${i}`);
24
+ dataRows = rows;
25
+ }
26
+ const numCols = headers.length;
27
+ const numRows = dataRows.length;
28
+ const columns = {};
29
+ for (let c = 0; c < numCols; c++) {
30
+ const colName = headers[c];
31
+ const colData = new Array(numRows);
32
+ for (let r = 0; r < numRows; r++) {
33
+ colData[r] = dataRows[r][c] !== undefined ? dataRows[r][c] : "";
34
+ }
35
+ columns[colName] = colData;
36
+ }
37
+ const coercedColumns = {};
38
+ for (let c = 0; c < numCols; c++) {
39
+ const colName = headers[c];
40
+ const rawValues = columns[colName];
41
+ if (schema && schema[colName]) {
42
+ coercedColumns[colName] = rawValues;
43
+ }
44
+ else if (inferSchema) {
45
+ const { values } = (0, utils_1.inferAndCoerceCSVColumn)(rawValues, options);
46
+ coercedColumns[colName] = values;
47
+ }
48
+ else {
49
+ coercedColumns[colName] = rawValues;
50
+ }
51
+ }
52
+ return new dataframe_1.DataFrame(coercedColumns, schema);
53
+ }
@@ -0,0 +1,10 @@
1
+ import { DataFrame } from "../dataframe/dataframe";
2
+ import type { ReadJSONOptions } from "../dataframe/types";
3
+ /**
4
+ * Parses JSON content (JSON or NDJSON) and loads it into a new DataFrame.
5
+ *
6
+ * @param content The JSON or NDJSON content string.
7
+ * @param options Parse and configuration options.
8
+ * @returns A new DataFrame instance populated with the parsed records.
9
+ */
10
+ export declare function read_json(content: string, { format, trimBeforeParse, schema, ...parseOpts }?: ReadJSONOptions): DataFrame<any>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.read_json = read_json;
4
+ const dataframe_1 = require("../dataframe/dataframe");
5
+ const utils_1 = require("../utils");
6
+ const exceptions_1 = require("../exceptions");
7
+ /**
8
+ * Parses JSON content (JSON or NDJSON) and loads it into a new DataFrame.
9
+ *
10
+ * @param content The JSON or NDJSON content string.
11
+ * @param options Parse and configuration options.
12
+ * @returns A new DataFrame instance populated with the parsed records.
13
+ */
14
+ function read_json(content, { format = "json", trimBeforeParse = true, schema, ...parseOpts } = {}) {
15
+ const parsed = (0, utils_1.safeJsonParse)(content, { format, trimBeforeParse, ...parseOpts });
16
+ if (parsed === content) {
17
+ throw new exceptions_1.DataFrameError(`Invalid JSON input: must be a valid, non-empty JSON ${format} string.`);
18
+ }
19
+ const parsedData = Array.isArray(parsed) ? parsed : ((0, utils_1.isObj)(parsed) ? [parsed] : []);
20
+ return new dataframe_1.DataFrame(parsedData, schema);
21
+ }
@@ -0,0 +1,7 @@
1
+ import { DataFrame } from "../dataframe/dataframe";
2
+ import type { RowRecord } from "../types";
3
+ import type { TransposeOptions } from "../dataframe/types";
4
+ /**
5
+ * Transposes the rows and columns of a DataFrame.
6
+ */
7
+ export declare function transpose<T extends RowRecord = any>(df: DataFrame<T>, { include_header: includeHeader, header_name: headerName, column_names: colNamesOpt }?: TransposeOptions): DataFrame<any>;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transpose = transpose;
4
+ const dataframe_1 = require("../dataframe/dataframe");
5
+ const datatypes_1 = require("../datatypes");
6
+ const utils_1 = require("../dataframe/utils");
7
+ const exceptions_1 = require("../exceptions");
8
+ /**
9
+ * Transposes the rows and columns of a DataFrame.
10
+ */
11
+ function transpose(df, { include_header: includeHeader = false, header_name: headerName = "column", column_names: colNamesOpt } = {}) {
12
+ const height = df.height;
13
+ if (height === 0) {
14
+ const cols = {};
15
+ const schema = {};
16
+ if (includeHeader) {
17
+ cols[headerName] = (0, utils_1.coerceColumn)([], datatypes_1.DataTypeRegistry.Utf8, 0);
18
+ schema[headerName] = datatypes_1.DataTypeRegistry.Utf8;
19
+ }
20
+ return dataframe_1.DataFrame._createDirect(cols, schema, 0);
21
+ }
22
+ let dataColumns = df.columns;
23
+ if (typeof colNamesOpt === "string") {
24
+ (0, exceptions_1.assertColumnExists)(colNamesOpt, df._columns, "column_names");
25
+ dataColumns = dataColumns.filter(c => c !== colNamesOpt);
26
+ }
27
+ let newColNames = [];
28
+ if (typeof colNamesOpt === "string") {
29
+ const keyCol = df._columns[colNamesOpt];
30
+ newColNames = new Array(height);
31
+ for (let i = 0; i < height; i++) {
32
+ const val = keyCol[i];
33
+ if (val == null) {
34
+ throw new exceptions_1.DataFrameError(`Transpose column_names column "${colNamesOpt}" contains null/undefined at index ${i}`);
35
+ }
36
+ newColNames[i] = String(val);
37
+ }
38
+ }
39
+ else if (colNamesOpt != null && typeof colNamesOpt !== "string" && Symbol.iterator in Object(colNamesOpt)) {
40
+ const colNamesArr = Array.from(colNamesOpt);
41
+ if (colNamesArr.length !== height) {
42
+ throw new exceptions_1.DataFrameError(`column_names length (${colNamesArr.length}) must match the height of the DataFrame (${height})`);
43
+ }
44
+ newColNames = new Array(height);
45
+ for (let i = 0; i < height; i++) {
46
+ newColNames[i] = String(colNamesArr[i]);
47
+ }
48
+ }
49
+ else {
50
+ newColNames = new Array(height);
51
+ for (let i = 0; i < height; i++) {
52
+ newColNames[i] = `column_${i}`;
53
+ }
54
+ }
55
+ const uniqueNames = new Set();
56
+ if (includeHeader) {
57
+ uniqueNames.add(headerName);
58
+ }
59
+ for (let i = 0; i < newColNames.length; i++) {
60
+ const name = newColNames[i];
61
+ if (uniqueNames.has(name)) {
62
+ throw new exceptions_1.DataFrameError(`Duplicate column name in transposed DataFrame: "${name}"`);
63
+ }
64
+ uniqueNames.add(name);
65
+ }
66
+ const numDataCols = dataColumns.length;
67
+ const newCols = {};
68
+ const newSchema = {};
69
+ if (includeHeader) {
70
+ newCols[headerName] = (0, utils_1.coerceColumn)(dataColumns, datatypes_1.DataTypeRegistry.Utf8, numDataCols);
71
+ newSchema[headerName] = datatypes_1.DataTypeRegistry.Utf8;
72
+ }
73
+ for (let i = 0; i < height; i++) {
74
+ const colName = newColNames[i];
75
+ const rawVals = new Array(numDataCols);
76
+ for (let j = 0; j < numDataCols; j++) {
77
+ rawVals[j] = df._columns[dataColumns[j]][i];
78
+ }
79
+ const type = (0, utils_1.inferColumnType)(rawVals);
80
+ newCols[colName] = (0, utils_1.coerceColumn)(rawVals, type, numDataCols);
81
+ newSchema[colName] = type;
82
+ }
83
+ return dataframe_1.DataFrame._createDirect(newCols, newSchema, numDataCols);
84
+ }