Skip to content

Commit 8ecf9bd

Browse files
Adding DF upsample (#163)
* Adding DF upsample * Update polars/dataframe.ts Co-authored-by: universalmind303 <[email protected]> * Adding semi * Updating packages Simplifying test Fixing documentation * Replacing pl.all with pl.col(*) * Updating yarn lock file * Removing toString from DF test --------- Co-authored-by: universalmind303 <[email protected]>
1 parent 208000f commit 8ecf9bd

File tree

5 files changed

+376
-195
lines changed

5 files changed

+376
-195
lines changed

__tests__/dataframe.test.ts

+75-1
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,6 @@ describe("meta", () => {
22492249
expect(dfString).toStrictEqual(expected);
22502250
});
22512251
});
2252-
22532252
test("Jupyter.display", () => {
22542253
const df = pl.DataFrame({
22552254
os: ["apple", "linux"],
@@ -2329,4 +2328,79 @@ describe("additional", () => {
23292328
const actual = df.toRecords();
23302329
expect(actual).toEqual(rows);
23312330
});
2331+
test("upsample", () => {
2332+
const df = pl
2333+
.DataFrame({
2334+
date: [
2335+
new Date(2024, 1, 1),
2336+
new Date(2024, 3, 1),
2337+
new Date(2024, 4, 1),
2338+
new Date(2024, 5, 1),
2339+
],
2340+
groups: ["A", "B", "A", "B"],
2341+
values: [0, 1, 2, 3],
2342+
})
2343+
.withColumn(pl.col("date").cast(pl.Date).alias("date"))
2344+
.sort("date");
2345+
2346+
let actual = df
2347+
.upsample("date", "1mo", "0ns", "groups", true)
2348+
.select(pl.col("*").forwardFill());
2349+
2350+
let expected = pl
2351+
.DataFrame({
2352+
date: [
2353+
new Date(2024, 1, 1),
2354+
new Date(2024, 2, 1),
2355+
new Date(2024, 3, 1),
2356+
new Date(2024, 4, 1),
2357+
new Date(2024, 3, 1),
2358+
new Date(2024, 4, 1),
2359+
new Date(2024, 5, 1),
2360+
],
2361+
groups: ["A", "A", "A", "A", "B", "B", "B"],
2362+
values: [0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 3.0],
2363+
})
2364+
.withColumn(pl.col("date").cast(pl.Date).alias("date"));
2365+
2366+
expect(actual).toFrameEqual(expected);
2367+
2368+
actual = df
2369+
.upsample({
2370+
timeColumn: "date",
2371+
every: "1mo",
2372+
offset: "0ns",
2373+
by: "groups",
2374+
maintainOrder: true,
2375+
})
2376+
.select(pl.col("*").forwardFill());
2377+
2378+
expect(actual).toFrameEqual(expected);
2379+
2380+
actual = df
2381+
.upsample({ timeColumn: "date", every: "1mo" })
2382+
.select(pl.col("*").forwardFill());
2383+
2384+
expected = pl
2385+
.DataFrame({
2386+
date: [
2387+
new Date(2024, 1, 1),
2388+
new Date(2024, 2, 1),
2389+
new Date(2024, 3, 1),
2390+
new Date(2024, 4, 1),
2391+
new Date(2024, 5, 1),
2392+
],
2393+
groups: ["A", "A", "B", "A", "B"],
2394+
values: [0.0, 0.0, 1.0, 2.0, 3.0],
2395+
})
2396+
.withColumn(pl.col("date").cast(pl.Date).alias("date"));
2397+
2398+
expect(actual).toFrameEqual(expected);
2399+
2400+
actual = df
2401+
.upsample({ timeColumn: "date", every: "1m" })
2402+
.select(pl.col("*").forwardFill());
2403+
2404+
expect(actual.shape).toEqual({ height: 174_241, width: 3 });
2405+
});
23322406
});

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@
5454
"precommit": "yarn lint && yarn test"
5555
},
5656
"devDependencies": {
57-
"@biomejs/biome": "^1.5.1",
58-
"@napi-rs/cli": "^2.17.0",
57+
"@biomejs/biome": "^1.5.3",
58+
"@napi-rs/cli": "^2.18.0",
5959
"@types/chance": "^1.1.6",
60-
"@types/jest": "^29.5.11",
61-
"@types/node": "^20.10.8",
60+
"@types/jest": "^29.5.12",
61+
"@types/node": "^20.11.17",
6262
"chance": "^1.1.11",
6363
"jest": "^29.7.0",
6464
"source-map-support": "^0.5.21",
65-
"ts-jest": "^29.1.1",
65+
"ts-jest": "^29.1.2",
6666
"ts-node": "^10.9.2",
67-
"typedoc": "^0.25.7",
67+
"typedoc": "^0.25.8",
6868
"typescript": "5.3.3"
6969
},
7070
"packageManager": "[email protected]",

0 commit comments

Comments
 (0)