Skip to content

Commit

Permalink
Adding DF upsample (#163)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Bidek56 and universalmind303 authored Feb 27, 2024
1 parent 208000f commit 8ecf9bd
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 195 deletions.
76 changes: 75 additions & 1 deletion __tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,6 @@ describe("meta", () => {
expect(dfString).toStrictEqual(expected);
});
});

test("Jupyter.display", () => {
const df = pl.DataFrame({
os: ["apple", "linux"],
Expand Down Expand Up @@ -2329,4 +2328,79 @@ describe("additional", () => {
const actual = df.toRecords();
expect(actual).toEqual(rows);
});
test("upsample", () => {
const df = pl
.DataFrame({
date: [
new Date(2024, 1, 1),
new Date(2024, 3, 1),
new Date(2024, 4, 1),
new Date(2024, 5, 1),
],
groups: ["A", "B", "A", "B"],
values: [0, 1, 2, 3],
})
.withColumn(pl.col("date").cast(pl.Date).alias("date"))
.sort("date");

let actual = df
.upsample("date", "1mo", "0ns", "groups", true)
.select(pl.col("*").forwardFill());

let expected = pl
.DataFrame({
date: [
new Date(2024, 1, 1),
new Date(2024, 2, 1),
new Date(2024, 3, 1),
new Date(2024, 4, 1),
new Date(2024, 3, 1),
new Date(2024, 4, 1),
new Date(2024, 5, 1),
],
groups: ["A", "A", "A", "A", "B", "B", "B"],
values: [0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 3.0],
})
.withColumn(pl.col("date").cast(pl.Date).alias("date"));

expect(actual).toFrameEqual(expected);

actual = df
.upsample({
timeColumn: "date",
every: "1mo",
offset: "0ns",
by: "groups",
maintainOrder: true,
})
.select(pl.col("*").forwardFill());

expect(actual).toFrameEqual(expected);

actual = df
.upsample({ timeColumn: "date", every: "1mo" })
.select(pl.col("*").forwardFill());

expected = pl
.DataFrame({
date: [
new Date(2024, 1, 1),
new Date(2024, 2, 1),
new Date(2024, 3, 1),
new Date(2024, 4, 1),
new Date(2024, 5, 1),
],
groups: ["A", "A", "B", "A", "B"],
values: [0.0, 0.0, 1.0, 2.0, 3.0],
})
.withColumn(pl.col("date").cast(pl.Date).alias("date"));

expect(actual).toFrameEqual(expected);

actual = df
.upsample({ timeColumn: "date", every: "1m" })
.select(pl.col("*").forwardFill());

expect(actual.shape).toEqual({ height: 174_241, width: 3 });
});
});
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@
"precommit": "yarn lint && yarn test"
},
"devDependencies": {
"@biomejs/biome": "^1.5.1",
"@napi-rs/cli": "^2.17.0",
"@biomejs/biome": "^1.5.3",
"@napi-rs/cli": "^2.18.0",
"@types/chance": "^1.1.6",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.8",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.17",
"chance": "^1.1.11",
"jest": "^29.7.0",
"source-map-support": "^0.5.21",
"ts-jest": "^29.1.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typedoc": "^0.25.7",
"typedoc": "^0.25.8",
"typescript": "5.3.3"
},
"packageManager": "[email protected]",
Expand Down
Loading

0 comments on commit 8ecf9bd

Please sign in to comment.