@ohos-ports/nodejs-polars 0.23.4-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +217 -0
- package/bin/cfg.d.ts +156 -0
- package/bin/cfg.js +52 -0
- package/bin/dataframe.d.ts +2135 -0
- package/bin/dataframe.js +783 -0
- package/bin/datatypes/conversion.d.ts +2 -0
- package/bin/datatypes/conversion.js +2 -0
- package/bin/datatypes/datatype.d.ts +285 -0
- package/bin/datatypes/datatype.js +430 -0
- package/bin/datatypes/field.d.ts +26 -0
- package/bin/datatypes/field.js +36 -0
- package/bin/datatypes/index.d.ts +43 -0
- package/bin/datatypes/index.js +120 -0
- package/bin/error.d.ts +7 -0
- package/bin/error.js +17 -0
- package/bin/functions.d.ts +26 -0
- package/bin/functions.js +208 -0
- package/bin/groupby.d.ts +212 -0
- package/bin/groupby.js +156 -0
- package/bin/html.d.ts +4 -0
- package/bin/html.js +18 -0
- package/bin/index.d.ts +256 -0
- package/bin/index.js +267 -0
- package/bin/internals/construction.d.ts +7 -0
- package/bin/internals/construction.js +256 -0
- package/bin/internals/polars_internal.d.ts +2 -0
- package/bin/internals/polars_internal.js +7 -0
- package/bin/io.d.ts +433 -0
- package/bin/io.js +344 -0
- package/bin/lazy/dataframe.d.ts +785 -0
- package/bin/lazy/dataframe.js +427 -0
- package/bin/lazy/expr/datetime.d.ts +8 -0
- package/bin/lazy/expr/datetime.js +27 -0
- package/bin/lazy/expr/index.d.ts +1152 -0
- package/bin/lazy/expr/index.js +632 -0
- package/bin/lazy/expr/list.d.ts +8 -0
- package/bin/lazy/expr/list.js +115 -0
- package/bin/lazy/expr/string.d.ts +534 -0
- package/bin/lazy/expr/string.js +131 -0
- package/bin/lazy/expr/struct.d.ts +126 -0
- package/bin/lazy/expr/struct.js +26 -0
- package/bin/lazy/functions.d.ts +612 -0
- package/bin/lazy/functions.js +687 -0
- package/bin/lazy/groupby.d.ts +29 -0
- package/bin/lazy/groupby.js +30 -0
- package/bin/lazy/index.d.ts +4 -0
- package/bin/lazy/index.js +20 -0
- package/bin/lazy/whenthen.d.ts +67 -0
- package/bin/lazy/whenthen.js +81 -0
- package/bin/native-polars.d.ts +2 -0
- package/bin/native-polars.js +267 -0
- package/bin/series/datetime.d.ts +8 -0
- package/bin/series/datetime.js +34 -0
- package/bin/series/index.d.ts +1135 -0
- package/bin/series/index.js +700 -0
- package/bin/series/list.d.ts +8 -0
- package/bin/series/list.js +87 -0
- package/bin/series/string.d.ts +300 -0
- package/bin/series/string.js +125 -0
- package/bin/series/struct.d.ts +36 -0
- package/bin/series/struct.js +35 -0
- package/bin/shared_traits.d.ts +1458 -0
- package/bin/shared_traits.js +2 -0
- package/bin/sql.d.ts +244 -0
- package/bin/sql.js +63 -0
- package/bin/types.d.ts +287 -0
- package/bin/types.js +2 -0
- package/bin/utils.d.ts +38 -0
- package/bin/utils.js +69 -0
- package/package.json +45 -0
package/bin/functions.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.repeat = repeat;
|
|
7
|
+
exports.concat = concat;
|
|
8
|
+
const dataframe_1 = require("./dataframe");
|
|
9
|
+
const construction_1 = require("./internals/construction");
|
|
10
|
+
const polars_internal_1 = __importDefault(require("./internals/polars_internal"));
|
|
11
|
+
const dataframe_2 = require("./lazy/dataframe");
|
|
12
|
+
const series_1 = require("./series");
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
/**
|
|
15
|
+
* _Repeat a single value n times and collect into a Series._
|
|
16
|
+
* @param value - Value to repeat.
|
|
17
|
+
* @param n - Number of repeats
|
|
18
|
+
* @param name - Optional name of the Series
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* > const s = pl.repeat("a", 5)
|
|
24
|
+
* > s.toArray()
|
|
25
|
+
* ["a", "a", "a", "a", "a"]
|
|
26
|
+
*
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function repeat(value, n, name = "") {
|
|
30
|
+
const dtype = (0, construction_1.jsTypeToPolarsType)(value);
|
|
31
|
+
const s = polars_internal_1.default.JsSeries.repeat(name, value, n, dtype);
|
|
32
|
+
return (0, series_1._Series)(s);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Aggregate all the Dataframes/Series in a List of DataFrames/Series to a single DataFrame/Series.
|
|
36
|
+
* @param items DataFrames/Series/LazyFrames to concatenate.
|
|
37
|
+
* @param options.rechunk Make sure that the result data is in contiguous memory.
|
|
38
|
+
* @param options.how Only used if the items are DataFrames. *Defaults to 'vertical'*
|
|
39
|
+
* - vertical: Applies multiple `vstack` operations.
|
|
40
|
+
* - verticalRelaxed: Same as `vertical`, but additionally coerces columns to their common supertype *if* they are mismatched (eg: Int32 → Int64).
|
|
41
|
+
* - horizontal: Stacks Series horizontally and fills with nulls if the lengths don't match.
|
|
42
|
+
* - diagonal: Finds a union between the column schemas and fills missing column values with ``null``.
|
|
43
|
+
* - diagonalRelaxed: Same as `diagonal`, but additionally coerces columns to their common supertype *if* they are mismatched (eg: Int32 → Int64).
|
|
44
|
+
* - align, alignFull, alignLeft, alignRight: Combines frames horizontally,
|
|
45
|
+
* auto-determining the common key columns and aligning rows using the same
|
|
46
|
+
* logic as `alignFrames` (note that "align" is an alias for "alignFull").
|
|
47
|
+
* The "align" strategy determines the type of join used to align the frames,
|
|
48
|
+
* equivalent to the "how" parameter on `alignFrames`. Note that the common join columns are automatically coalesced, but other column collisions
|
|
49
|
+
* will raise an error (if you need more control over this you should use a suitable `join` method directly).
|
|
50
|
+
* @param options.parallel - Only relevant for LazyFrames. This determines if the concatenated lazy computations may be executed in parallel.
|
|
51
|
+
* @example
|
|
52
|
+
* > const df1 = pl.DataFrame({"a": [1], "b": [3]});
|
|
53
|
+
* > const df2 = pl.DataFrame({"a": [2], "b": [4]});
|
|
54
|
+
* > pl.concat([df1, df2]);
|
|
55
|
+
* shape: (2, 2)
|
|
56
|
+
* ┌─────┬─────┐
|
|
57
|
+
* │ a ┆ b │
|
|
58
|
+
* │ --- ┆ --- │
|
|
59
|
+
* │ i64 ┆ i64 │
|
|
60
|
+
* ╞═════╪═════╡
|
|
61
|
+
* │ 1 ┆ 3 │
|
|
62
|
+
* ├╌╌╌╌╌┼╌╌╌╌╌┤
|
|
63
|
+
* │ 2 ┆ 4 │
|
|
64
|
+
* └─────┴─────┘
|
|
65
|
+
*
|
|
66
|
+
* > const a = pl.DataFrame({ a: ["a", "b"], b: [1, 2] });
|
|
67
|
+
* > const b = pl.DataFrame({ c: [5, 6], d: [7, 8], e: [9, 10]});
|
|
68
|
+
* > pl.concat([a, b], { how: "horizontal" });
|
|
69
|
+
*
|
|
70
|
+
* shape: (2, 5)
|
|
71
|
+
* ┌─────┬─────┬─────┬─────┬──────┐
|
|
72
|
+
* │ a ┆ b ┆ c ┆ d ┆ e │
|
|
73
|
+
* │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
|
|
74
|
+
* │ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
|
|
75
|
+
* ╞═════╪═════╪═════╪═════╪══════╡
|
|
76
|
+
* │ a ┆ 1.0 ┆ 5.0 ┆ 7.0 ┆ 9.0 │
|
|
77
|
+
* │ b ┆ 2.0 ┆ 6.0 ┆ 8.0 ┆ 10.0 │
|
|
78
|
+
* └─────┴─────┴─────┴─────┴──────┘
|
|
79
|
+
*
|
|
80
|
+
* > const df_d1 = pl.DataFrame({"a": [1], "b": [3]});
|
|
81
|
+
* > const df_d2 = pl.DataFrame({"a": [2], "c": [4]});
|
|
82
|
+
* > pl.concat([df_d1, df_d2], { how: "diagonal" });
|
|
83
|
+
*
|
|
84
|
+
* shape: (2, 3)
|
|
85
|
+
* ┌─────┬──────┬──────┐
|
|
86
|
+
* │ a ┆ b ┆ c │
|
|
87
|
+
* │ --- ┆ --- ┆ --- │
|
|
88
|
+
* │ i64 ┆ i64 ┆ i64 │
|
|
89
|
+
* ╞═════╪══════╪══════╡
|
|
90
|
+
* │ 1 ┆ 3 ┆ null │
|
|
91
|
+
* │ 2 ┆ null ┆ 4 │
|
|
92
|
+
* └─────┴──────┴──────┘
|
|
93
|
+
*
|
|
94
|
+
* The "align" strategies require at least one common column to align on:
|
|
95
|
+
|
|
96
|
+
>>> const df1 = pl.DataFrame({"id": [1, 2], "x": [3, 4]});
|
|
97
|
+
>>> const df2 = pl.DataFrame({"id": [2, 3], "y": [5, 6]});
|
|
98
|
+
>>> const df3 = pl.DataFrame({"id": [1, 3], "z": [7, 8]});
|
|
99
|
+
>>> pl.concat([df1, df2, df3], how="align"); // equivalent to "alignFull"
|
|
100
|
+
shape: (3, 4)
|
|
101
|
+
┌─────┬──────┬──────┬──────┐
|
|
102
|
+
│ id ┆ x ┆ y ┆ z │
|
|
103
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
104
|
+
│ i64 ┆ i64 ┆ i64 ┆ i64 │
|
|
105
|
+
╞═════╪══════╪══════╪══════╡
|
|
106
|
+
│ 1 ┆ 3 ┆ null ┆ 7 │
|
|
107
|
+
│ 2 ┆ 4 ┆ 5 ┆ null │
|
|
108
|
+
│ 3 ┆ null ┆ 6 ┆ 8 │
|
|
109
|
+
└─────┴──────┴──────┴──────┘
|
|
110
|
+
>>> pl.concat([df1, df2, df3], how="alignLeft");
|
|
111
|
+
shape: (2, 4)
|
|
112
|
+
┌─────┬─────┬──────┬──────┐
|
|
113
|
+
│ id ┆ x ┆ y ┆ z │
|
|
114
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
115
|
+
│ i64 ┆ i64 ┆ i64 ┆ i64 │
|
|
116
|
+
╞═════╪═════╪══════╪══════╡
|
|
117
|
+
│ 1 ┆ 3 ┆ null ┆ 7 │
|
|
118
|
+
│ 2 ┆ 4 ┆ 5 ┆ null │
|
|
119
|
+
└─────┴─────┴──────┴──────┘
|
|
120
|
+
>>> pl.concat([df1, df2, df3], how="alignRight");
|
|
121
|
+
shape: (2, 4)
|
|
122
|
+
┌─────┬──────┬──────┬─────┐
|
|
123
|
+
│ id ┆ x ┆ y ┆ z │
|
|
124
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
125
|
+
│ i64 ┆ i64 ┆ i64 ┆ i64 │
|
|
126
|
+
╞═════╪══════╪══════╪═════╡
|
|
127
|
+
│ 1 ┆ null ┆ null ┆ 7 │
|
|
128
|
+
│ 3 ┆ null ┆ 6 ┆ 8 │
|
|
129
|
+
└─────┴──────┴──────┴─────┘
|
|
130
|
+
>>> pl.concat([df1, df2, df3], how="alignInner");
|
|
131
|
+
shape: (0, 4)
|
|
132
|
+
┌─────┬─────┬─────┬─────┐
|
|
133
|
+
│ id ┆ x ┆ y ┆ z │
|
|
134
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
135
|
+
│ i64 ┆ i64 ┆ i64 ┆ i64 │
|
|
136
|
+
╞═════╪═════╪═════╪═════╡
|
|
137
|
+
└─────┴─────┴─────┴─────┘
|
|
138
|
+
*/
|
|
139
|
+
const computeAlignParams = (items, how) => {
|
|
140
|
+
const commonCols = (0, utils_1.commonValue)(...items.map((c) => c.columns));
|
|
141
|
+
const uniqueCols = [...new Set(items.flatMap((c) => c.columns))].sort();
|
|
142
|
+
// Join methods allowed: "full" | "left" | "inner" | "semi" | "anti" | undefined
|
|
143
|
+
const joinMethod = how === "align"
|
|
144
|
+
? "full"
|
|
145
|
+
: how.replace("align", "").toLocaleLowerCase();
|
|
146
|
+
return { commonCols, uniqueCols, joinMethod };
|
|
147
|
+
};
|
|
148
|
+
function concat(items, options = { rechunk: true, parallel: true, how: "vertical" }) {
|
|
149
|
+
const { rechunk, how, parallel } = options;
|
|
150
|
+
if (!items.length) {
|
|
151
|
+
throw new RangeError("cannot concat empty list");
|
|
152
|
+
}
|
|
153
|
+
// short-circuit common single-item cases early for all types
|
|
154
|
+
if (items.length === 1)
|
|
155
|
+
return items[0];
|
|
156
|
+
if ((0, utils_1.isDataFrameArray)(items)) {
|
|
157
|
+
let df;
|
|
158
|
+
switch (how) {
|
|
159
|
+
case "align":
|
|
160
|
+
case "alignFull":
|
|
161
|
+
case "alignInner":
|
|
162
|
+
case "alignLeft":
|
|
163
|
+
case "alignRight": {
|
|
164
|
+
const { commonCols, uniqueCols, joinMethod } = computeAlignParams(items, how);
|
|
165
|
+
df = (0, dataframe_1._DataFrame)(items.reduce((acc, curr) => acc.join(curr, { on: commonCols, how: joinMethod, coalesce: true })))
|
|
166
|
+
._df.sort(commonCols)
|
|
167
|
+
.select(uniqueCols);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "vertical":
|
|
171
|
+
df = items.reduce((acc, curr) => acc.vstack(curr));
|
|
172
|
+
break;
|
|
173
|
+
case "verticalRelaxed":
|
|
174
|
+
case "diagonalRelaxed":
|
|
175
|
+
df = (0, dataframe_2._LazyDataFrame)(polars_internal_1.default.concatLf(items.map((i) => i.inner().lazy()), how, rechunk ?? false, parallel ?? true, true, // to_supertypes
|
|
176
|
+
true)).collectSync();
|
|
177
|
+
break;
|
|
178
|
+
case "horizontal":
|
|
179
|
+
df = (0, dataframe_1._DataFrame)(polars_internal_1.default.horizontalConcat(items.map((i) => i.inner())));
|
|
180
|
+
break;
|
|
181
|
+
case "diagonal":
|
|
182
|
+
df = (0, dataframe_1._DataFrame)(polars_internal_1.default.diagonalConcat(items.map((i) => i.inner())));
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
throw new TypeError("unknown concat how option");
|
|
186
|
+
}
|
|
187
|
+
return rechunk ? df.rechunk() : df;
|
|
188
|
+
}
|
|
189
|
+
if ((0, utils_1.isLazyDataFrameArray)(items)) {
|
|
190
|
+
let df;
|
|
191
|
+
if (how?.startsWith("align")) {
|
|
192
|
+
const { commonCols, uniqueCols, joinMethod } = computeAlignParams(items, how);
|
|
193
|
+
df = (0, dataframe_2._LazyDataFrame)(items.reduce((acc, curr) => acc.join(curr, { on: commonCols, how: joinMethod, coalesce: true })))
|
|
194
|
+
._ldf.sort(commonCols)
|
|
195
|
+
.select(uniqueCols);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
df = (0, dataframe_2._LazyDataFrame)(polars_internal_1.default.concatLf(items.map((i) => i.inner()), how, rechunk ?? false, parallel ?? true, true, // to_supertypes
|
|
199
|
+
true));
|
|
200
|
+
}
|
|
201
|
+
return df;
|
|
202
|
+
}
|
|
203
|
+
if ((0, utils_1.isSeriesArray)(items)) {
|
|
204
|
+
const s = items.reduce((acc, curr) => acc.concat(curr));
|
|
205
|
+
return rechunk ? s.rechunk() : s;
|
|
206
|
+
}
|
|
207
|
+
throw new TypeError("can only concat series and dataframes");
|
|
208
|
+
}
|
package/bin/groupby.d.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { type DataFrame } from "./dataframe";
|
|
2
|
+
import type { Expr } from "./lazy/expr";
|
|
3
|
+
import type { ColumnsOrExpr, StartBy } from "./utils";
|
|
4
|
+
declare const inspect: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* Starts a new GroupBy operation.
|
|
7
|
+
*/
|
|
8
|
+
export interface GroupBy {
|
|
9
|
+
[inspect](): string;
|
|
10
|
+
/**
|
|
11
|
+
* Aggregate the groups into Series.
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
aggList(): DataFrame;
|
|
15
|
+
/**
|
|
16
|
+
* Compute aggregations for each group of a group by operation.
|
|
17
|
+
* ___
|
|
18
|
+
* @param columns - Aggregations to compute for each group of the group by operation, specified as positional arguments.
|
|
19
|
+
* @example
|
|
20
|
+
* ```
|
|
21
|
+
* const df = pl.DataFrame({
|
|
22
|
+
foo: [1, 2, 2, 3, 3],
|
|
23
|
+
ham: [6.0, 6, 7, 8.0, 8.0],
|
|
24
|
+
bar: ["a", "b", "c", "c", "c"],
|
|
25
|
+
spam: ["a", "b", "c", "c", "c"],
|
|
26
|
+
});
|
|
27
|
+
* // use lazy api rest parameter style
|
|
28
|
+
* > df.groupBy('foo', 'bar').agg(pl.count('ham'), pl.col('spam')).sort(["foo", "bar"]);
|
|
29
|
+
* shape: (4, 4)
|
|
30
|
+
┌─────┬─────┬─────┬────────────┐
|
|
31
|
+
│ foo ┆ bar ┆ ham ┆ spam │
|
|
32
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
33
|
+
│ f64 ┆ str ┆ u32 ┆ list[str] │
|
|
34
|
+
╞═════╪═════╪═════╪════════════╡
|
|
35
|
+
│ 1.0 ┆ a ┆ 1 ┆ ["a"] │
|
|
36
|
+
│ 2.0 ┆ b ┆ 1 ┆ ["b"] │
|
|
37
|
+
│ 2.0 ┆ c ┆ 1 ┆ ["c"] │
|
|
38
|
+
│ 3.0 ┆ c ┆ 2 ┆ ["c", "c"] │
|
|
39
|
+
└─────┴─────┴─────┴────────────┘
|
|
40
|
+
*
|
|
41
|
+
* > df.groupBy("bar").agg(pl.col("foo"), pl.col("ham"), pl.col("spam") ).sort("bar");
|
|
42
|
+
* shape: (3, 4)
|
|
43
|
+
┌─────┬─────────────────┬─────────────────┬─────────────────┐
|
|
44
|
+
│ bar ┆ foo ┆ ham ┆ spam │
|
|
45
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
46
|
+
│ str ┆ list[f64] ┆ list[f64] ┆ list[str] │
|
|
47
|
+
╞═════╪═════════════════╪═════════════════╪═════════════════╡
|
|
48
|
+
│ a ┆ [1.0] ┆ [6.0] ┆ ["a"] │
|
|
49
|
+
│ b ┆ [2.0] ┆ [6.0] ┆ ["b"] │
|
|
50
|
+
│ c ┆ [2.0, 3.0, 3.0] ┆ [7.0, 8.0, 8.0] ┆ ["c", "c", "c"] │
|
|
51
|
+
└─────┴─────────────────┴─────────────────┴─────────────────┘
|
|
52
|
+
* > const h = pl.col("ham");
|
|
53
|
+
* > df.groupBy("bar").agg(h.sum().as("sum_ham"), h.min().as("min_ham"), h.max().as("max_ham"));
|
|
54
|
+
* shape: (3, 4)
|
|
55
|
+
┌─────┬─────────┬─────────┬─────────┐
|
|
56
|
+
│ bar ┆ sum_ham ┆ min_ham ┆ max_ham │
|
|
57
|
+
│ --- ┆ --- ┆ --- ┆ --- │
|
|
58
|
+
│ str ┆ f64 ┆ f64 ┆ f64 │
|
|
59
|
+
╞═════╪═════════╪═════════╪═════════╡
|
|
60
|
+
│ a ┆ 6.0 ┆ 6.0 ┆ 6.0 │
|
|
61
|
+
│ b ┆ 6.0 ┆ 6.0 ┆ 6.0 │
|
|
62
|
+
│ c ┆ 23.0 ┆ 7.0 ┆ 8.0 │
|
|
63
|
+
└─────┴─────────┴─────────┴─────────┘
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
agg(...columns: Expr[]): DataFrame;
|
|
67
|
+
agg(columns: Record<string, keyof Expr | (keyof Expr)[]>): DataFrame;
|
|
68
|
+
/**
|
|
69
|
+
* Return the number of rows in each group.
|
|
70
|
+
* @example
|
|
71
|
+
* > df = pl.DataFrame({a: ["Apple", "Apple", "Orange"], b: [1, None, 2]});
|
|
72
|
+
> df.groupBy("a").len()
|
|
73
|
+
shape: (2, 2)
|
|
74
|
+
┌────────┬─────┐
|
|
75
|
+
│ a ┆ len │
|
|
76
|
+
│ --- ┆ --- │
|
|
77
|
+
│ str ┆ u32 │
|
|
78
|
+
╞════════╪═════╡
|
|
79
|
+
│ Apple ┆ 2 │
|
|
80
|
+
│ Orange ┆ 1 │
|
|
81
|
+
└────────┴─────┘
|
|
82
|
+
*/
|
|
83
|
+
len(): DataFrame;
|
|
84
|
+
/**
|
|
85
|
+
* Aggregate the first values in the group.
|
|
86
|
+
*/
|
|
87
|
+
first(): DataFrame;
|
|
88
|
+
/**
|
|
89
|
+
* Return a `DataFrame` with:
|
|
90
|
+
* - the groupby keys
|
|
91
|
+
* - the group indexes aggregated as lists
|
|
92
|
+
*/
|
|
93
|
+
groups(): DataFrame;
|
|
94
|
+
/**
|
|
95
|
+
* Return first n rows of each group.
|
|
96
|
+
* @param n -Number of values of the group to select
|
|
97
|
+
* @example
|
|
98
|
+
* ```
|
|
99
|
+
* > df = pl.DataFrame({
|
|
100
|
+
* > "letters": ["c", "c", "a", "c", "a", "b"],
|
|
101
|
+
* > "nrs": [1, 2, 3, 4, 5, 6]
|
|
102
|
+
* > })
|
|
103
|
+
* > df
|
|
104
|
+
* shape: (6, 2)
|
|
105
|
+
* ╭─────────┬─────╮
|
|
106
|
+
* │ letters ┆ nrs │
|
|
107
|
+
* │ --- ┆ --- │
|
|
108
|
+
* │ str ┆ i64 │
|
|
109
|
+
* ╞═════════╪═════╡
|
|
110
|
+
* │ "c" ┆ 1 │
|
|
111
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
112
|
+
* │ "c" ┆ 2 │
|
|
113
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
114
|
+
* │ "a" ┆ 3 │
|
|
115
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
116
|
+
* │ "c" ┆ 4 │
|
|
117
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
118
|
+
* │ "a" ┆ 5 │
|
|
119
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
120
|
+
* │ "b" ┆ 6 │
|
|
121
|
+
* ╰─────────┴─────╯
|
|
122
|
+
* > df.groupby("letters")
|
|
123
|
+
* > .head(2)
|
|
124
|
+
* > .sort("letters");
|
|
125
|
+
* > >>
|
|
126
|
+
* shape: (5, 2)
|
|
127
|
+
* ╭─────────┬─────╮
|
|
128
|
+
* │ letters ┆ nrs │
|
|
129
|
+
* │ --- ┆ --- │
|
|
130
|
+
* │ str ┆ i64 │
|
|
131
|
+
* ╞═════════╪═════╡
|
|
132
|
+
* │ "a" ┆ 3 │
|
|
133
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
134
|
+
* │ "a" ┆ 5 │
|
|
135
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
136
|
+
* │ "b" ┆ 6 │
|
|
137
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
138
|
+
* │ "c" ┆ 1 │
|
|
139
|
+
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
140
|
+
* │ "c" ┆ 2 │
|
|
141
|
+
* ╰─────────┴─────╯
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
head(n?: number): DataFrame;
|
|
145
|
+
/**
|
|
146
|
+
* Aggregate the last values in the group.
|
|
147
|
+
*/
|
|
148
|
+
last(): DataFrame;
|
|
149
|
+
/**
|
|
150
|
+
* Reduce the groups to the maximal value.
|
|
151
|
+
*/
|
|
152
|
+
max(): DataFrame;
|
|
153
|
+
/**
|
|
154
|
+
* Reduce the groups to the mean values.
|
|
155
|
+
*/
|
|
156
|
+
mean(): DataFrame;
|
|
157
|
+
/**
|
|
158
|
+
* Return the median per group.
|
|
159
|
+
*/
|
|
160
|
+
median(): DataFrame;
|
|
161
|
+
/**
|
|
162
|
+
* Reduce the groups to the minimal value.
|
|
163
|
+
*/
|
|
164
|
+
min(): DataFrame;
|
|
165
|
+
/**
|
|
166
|
+
* Count the unique values per group.
|
|
167
|
+
*/
|
|
168
|
+
nUnique(): DataFrame;
|
|
169
|
+
/**
|
|
170
|
+
* Do a pivot operation based on the group key, a pivot column and an aggregation function on the values column.
|
|
171
|
+
* @param pivotCol - Column to pivot.
|
|
172
|
+
* @param valuesCol - Column that will be aggregated.
|
|
173
|
+
*
|
|
174
|
+
*/
|
|
175
|
+
pivot(pivotCol: string, valuesCol: string): PivotOps;
|
|
176
|
+
pivot({ pivotCol, valuesCol, }: {
|
|
177
|
+
pivotCol: string;
|
|
178
|
+
valuesCol: string;
|
|
179
|
+
}): PivotOps;
|
|
180
|
+
/**
|
|
181
|
+
* Compute the quantile per group.
|
|
182
|
+
*/
|
|
183
|
+
quantile(quantile: number): DataFrame;
|
|
184
|
+
/**
|
|
185
|
+
* Reduce the groups to the sum.
|
|
186
|
+
*/
|
|
187
|
+
sum(): DataFrame;
|
|
188
|
+
tail(n?: number): DataFrame;
|
|
189
|
+
toString(): string;
|
|
190
|
+
}
|
|
191
|
+
export type PivotOps = Pick<GroupBy, "len" | "first" | "max" | "mean" | "median" | "min" | "sum"> & {
|
|
192
|
+
[inspect](): string;
|
|
193
|
+
};
|
|
194
|
+
/** @ignore */
|
|
195
|
+
export declare function _GroupBy(df: any, by: string[], maintainOrder?: boolean): GroupBy;
|
|
196
|
+
/**
|
|
197
|
+
* intermediate state of a rolling groupby
|
|
198
|
+
*/
|
|
199
|
+
export interface RollingGroupBy {
|
|
200
|
+
agg(column: ColumnsOrExpr, ...columns: ColumnsOrExpr[]): DataFrame;
|
|
201
|
+
}
|
|
202
|
+
/** @ignore */
|
|
203
|
+
export declare function RollingGroupBy(df: any, indexColumn: ColumnsOrExpr, period: string, offset?: string, closed?: any, by?: ColumnsOrExpr): RollingGroupBy;
|
|
204
|
+
/**
|
|
205
|
+
* intermediate state of a dynamic groupby
|
|
206
|
+
*/
|
|
207
|
+
export interface DynamicGroupBy {
|
|
208
|
+
agg(column: ColumnsOrExpr, ...columns: ColumnsOrExpr[]): DataFrame;
|
|
209
|
+
}
|
|
210
|
+
/** @ignore */
|
|
211
|
+
export declare function DynamicGroupBy(df: any, indexColumn: string, every: string, period?: string, offset?: string, label?: string, includeBoundaries?: boolean, closed?: string, by?: ColumnsOrExpr, startBy?: StartBy): DynamicGroupBy;
|
|
212
|
+
export {};
|
package/bin/groupby.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports._GroupBy = _GroupBy;
|
|
40
|
+
exports.RollingGroupBy = RollingGroupBy;
|
|
41
|
+
exports.DynamicGroupBy = DynamicGroupBy;
|
|
42
|
+
const node_util_1 = __importDefault(require("node:util"));
|
|
43
|
+
const dataframe_1 = require("./dataframe");
|
|
44
|
+
const functions_1 = require("./lazy/functions");
|
|
45
|
+
const utils = __importStar(require("./utils"));
|
|
46
|
+
const inspect = Symbol.for("nodejs.util.inspect.custom");
|
|
47
|
+
const inspectOpts = { colors: true, depth: null };
|
|
48
|
+
/** @ignore */
|
|
49
|
+
function _GroupBy(df, by, maintainOrder = false) {
|
|
50
|
+
const customInspect = () => node_util_1.default.formatWithOptions(inspectOpts, "GroupBy {by: %O}", by);
|
|
51
|
+
const pivot = (opts, valuesCol) => {
|
|
52
|
+
if (typeof opts === "string") {
|
|
53
|
+
if (valuesCol) {
|
|
54
|
+
return pivot({ pivotCol: opts, valuesCol });
|
|
55
|
+
}
|
|
56
|
+
throw new Error("must specify both pivotCol and valuesCol");
|
|
57
|
+
}
|
|
58
|
+
return PivotOps((0, dataframe_1._DataFrame)(df), by, opts.pivotCol, opts.valuesCol);
|
|
59
|
+
};
|
|
60
|
+
const agg = (...aggs) => {
|
|
61
|
+
if (utils.isExprArray(aggs)) {
|
|
62
|
+
aggs = [aggs].flat(2);
|
|
63
|
+
return (0, dataframe_1._DataFrame)(df)
|
|
64
|
+
.lazy()
|
|
65
|
+
.groupBy(by, maintainOrder)
|
|
66
|
+
.agg(...aggs)
|
|
67
|
+
.collectSync({ noOptimization: true });
|
|
68
|
+
}
|
|
69
|
+
const pairs = Object.entries(aggs[0]).flatMap(([key, values]) => {
|
|
70
|
+
return [values].flat(2).map((v) => (0, functions_1.col)(key)[v]());
|
|
71
|
+
});
|
|
72
|
+
return (0, dataframe_1._DataFrame)(df)
|
|
73
|
+
.lazy()
|
|
74
|
+
.groupBy(by, maintainOrder)
|
|
75
|
+
.agg(...pairs)
|
|
76
|
+
.collectSync({ noOptimization: true });
|
|
77
|
+
};
|
|
78
|
+
return Object.seal({
|
|
79
|
+
[inspect]: customInspect,
|
|
80
|
+
agg,
|
|
81
|
+
pivot,
|
|
82
|
+
aggList: () => agg((0, functions_1.exclude)(by)),
|
|
83
|
+
len() {
|
|
84
|
+
return (0, dataframe_1._DataFrame)(df.groupby([by].flat(), by, "count"));
|
|
85
|
+
},
|
|
86
|
+
first: () => agg((0, functions_1.exclude)(by).first()),
|
|
87
|
+
groups() {
|
|
88
|
+
return (0, dataframe_1._DataFrame)(df.groupby([by].flat(), null, "groups"));
|
|
89
|
+
},
|
|
90
|
+
head: (n = 5) => agg((0, functions_1.exclude)(by).head(n)),
|
|
91
|
+
last: () => agg((0, functions_1.exclude)(by).last()),
|
|
92
|
+
max: () => agg((0, functions_1.exclude)(by).max()),
|
|
93
|
+
mean: () => agg((0, functions_1.exclude)(by).mean()),
|
|
94
|
+
median: () => agg((0, functions_1.exclude)(by).median()),
|
|
95
|
+
min: () => agg((0, functions_1.exclude)(by).min()),
|
|
96
|
+
nUnique: () => agg((0, functions_1.exclude)(by).nUnique()),
|
|
97
|
+
quantile: (q) => agg((0, functions_1.exclude)(by).quantile(q)),
|
|
98
|
+
sum: () => agg((0, functions_1.exclude)(by).sum()),
|
|
99
|
+
tail: (n = 5) => agg((0, functions_1.exclude)(by).tail(n)),
|
|
100
|
+
toString: () => "GroupBy",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function PivotOps(df, by, pivotCol, valueCol) {
|
|
104
|
+
const pivot = (agg) => () => df.pivot(valueCol, { on: pivotCol, index: by, aggregateFunc: agg });
|
|
105
|
+
const customInspect = () => node_util_1.default.formatWithOptions(inspectOpts, "PivotOps {by: %O}", by);
|
|
106
|
+
return {
|
|
107
|
+
[inspect]: customInspect,
|
|
108
|
+
first: pivot("first"),
|
|
109
|
+
sum: pivot("sum"),
|
|
110
|
+
min: pivot("min"),
|
|
111
|
+
max: pivot("max"),
|
|
112
|
+
mean: pivot("mean"),
|
|
113
|
+
len: pivot("len"),
|
|
114
|
+
median: pivot("median"),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** @ignore */
|
|
118
|
+
function RollingGroupBy(df, indexColumn, period, offset, closed, by) {
|
|
119
|
+
return {
|
|
120
|
+
agg(column, ...columns) {
|
|
121
|
+
return df
|
|
122
|
+
.lazy()
|
|
123
|
+
.groupByRolling({
|
|
124
|
+
indexColumn,
|
|
125
|
+
period,
|
|
126
|
+
offset,
|
|
127
|
+
closed,
|
|
128
|
+
by,
|
|
129
|
+
})
|
|
130
|
+
.agg(column, ...columns)
|
|
131
|
+
.collectSync();
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** @ignore */
|
|
136
|
+
function DynamicGroupBy(df, indexColumn, every, period, offset, label, includeBoundaries, closed, by, startBy) {
|
|
137
|
+
return {
|
|
138
|
+
agg(column, ...columns) {
|
|
139
|
+
return df
|
|
140
|
+
.lazy()
|
|
141
|
+
.groupByDynamic({
|
|
142
|
+
indexColumn,
|
|
143
|
+
every,
|
|
144
|
+
period,
|
|
145
|
+
offset,
|
|
146
|
+
label,
|
|
147
|
+
includeBoundaries,
|
|
148
|
+
closed,
|
|
149
|
+
by,
|
|
150
|
+
startBy,
|
|
151
|
+
})
|
|
152
|
+
.agg(column, ...columns)
|
|
153
|
+
.collectSync({ noOptimizations: true });
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
package/bin/html.d.ts
ADDED
package/bin/html.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.escapeHTML = escapeHTML;
|
|
4
|
+
const rawToEntityEntries = [
|
|
5
|
+
["&", "&"],
|
|
6
|
+
["<", "<"],
|
|
7
|
+
[">", ">"],
|
|
8
|
+
['"', """],
|
|
9
|
+
["'", "'"],
|
|
10
|
+
];
|
|
11
|
+
const rawToEntity = new Map(rawToEntityEntries);
|
|
12
|
+
const rawRe = new RegExp(`[${[...rawToEntity.keys()].join("")}]`, "g");
|
|
13
|
+
/**
|
|
14
|
+
* Escapes text for safe interpolation into HTML text content and quoted attributes
|
|
15
|
+
*/
|
|
16
|
+
function escapeHTML(str) {
|
|
17
|
+
return str.replaceAll(rawRe, (m) => rawToEntity.get(m) ?? m);
|
|
18
|
+
}
|