forked from pola-rs/nodejs-polars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: serde for lazyframe * add ser/de for series,df,ldf & expr
- Loading branch information
1 parent
56df87e
commit 8da6c04
Showing
15 changed files
with
559 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import pl from "@polars"; | ||
describe("serde", () => { | ||
test("lazyframe:json", () => { | ||
const df = pl.scanCSV("../examples/datasets/foods1.csv"); | ||
const buf = df.serialize("json"); | ||
const deserde = pl.LazyDataFrame.deserialize(buf, "json"); | ||
const expected = df.collectSync(); | ||
const actual = deserde.collectSync(); | ||
expect(actual).toFrameEqual(expected); | ||
}); | ||
test("lazyframe:bincode", () => { | ||
const df = pl.scanCSV("../examples/datasets/foods1.csv"); | ||
const buf = df.serialize("bincode"); | ||
const deserde = pl.LazyDataFrame.deserialize(buf, "bincode"); | ||
const expected = df.collectSync(); | ||
const actual = deserde.collectSync(); | ||
expect(actual).toFrameEqual(expected); | ||
}); | ||
test("expr:json", () => { | ||
const expr = pl.cols("foo", "bar").sortBy("other"); | ||
|
||
const buf = expr.serialize("json"); | ||
const actual = pl.Expr.deserialize(buf, "json"); | ||
|
||
expect(actual.toString()).toEqual(expr.toString()); | ||
}); | ||
test("expr:bincode", () => { | ||
const expr = pl.cols("foo", "bar").sortBy("other"); | ||
const buf = expr.serialize("bincode"); | ||
const actual = pl.Expr.deserialize(buf, "bincode"); | ||
|
||
expect(actual.toString()).toEqual(expr.toString()); | ||
}); | ||
test("dataframe:json", () => { | ||
const df = pl.DataFrame({ | ||
foo: [1, 2], | ||
bar: [2, 3] | ||
}); | ||
const buf = df.serialize("json"); | ||
const expected = pl.DataFrame.deserialize(buf, "json"); | ||
expect(df).toFrameEqual(expected); | ||
}); | ||
test("dataframe:bincode", () => { | ||
const df = pl.DataFrame({ | ||
foo: [1, 2], | ||
bar: [2, 3] | ||
}); | ||
const buf = df.serialize("bincode"); | ||
const expected = pl.DataFrame.deserialize(buf, "bincode"); | ||
expect(df).toFrameEqual(expected); | ||
}); | ||
|
||
test("dataframe:unsupported", () => { | ||
const df = pl.DataFrame({ | ||
foo: [1, 2], | ||
bar: [2, 3] | ||
}); | ||
const ser = () => df.serialize("yaml" as any); | ||
const buf = df.serialize("bincode"); | ||
const de = () => pl.DataFrame.deserialize(buf, "yaml" as any); | ||
const mismatch = () => pl.DataFrame.deserialize(buf, "json"); | ||
expect(ser).toThrow(); | ||
expect(de).toThrow(); | ||
expect(mismatch).toThrow(); | ||
}); | ||
test("series:json", () => { | ||
const s = pl.Series("foo", [1, 2, 3]); | ||
|
||
const buf = s.serialize("json"); | ||
const expected = pl.Series.deserialize(buf, "json"); | ||
expect(s).toSeriesEqual(expected); | ||
}); | ||
test("series:bincode", () => { | ||
const s = pl.Series("foo", [1, 2, 3]); | ||
|
||
const buf = s.serialize("bincode"); | ||
const expected = pl.Series.deserialize(buf, "bincode"); | ||
expect(s).toSeriesEqual(expected); | ||
}); | ||
|
||
test("series:unsupported", () => { | ||
const s = pl.Series("foo", [1, 2, 3]); | ||
const ser = () => s.serialize("yaml" as any); | ||
const buf = s.serialize("bincode"); | ||
const de = () => pl.Series.deserialize(buf, "yaml" as any); | ||
const mismatch = () => pl.Series.deserialize(buf, "json"); | ||
expect(ser).toThrow(); | ||
expect(de).toThrow(); | ||
expect(mismatch).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,50 @@ | ||
|
||
import pl from "@polars"; | ||
import {DataType} from "@polars/datatypes"; | ||
|
||
describe("struct", () => { | ||
describe("series", () => { | ||
test("series <--> array round trip", () => { | ||
const data = [ | ||
{utf8: "a", f64: 1, }, | ||
{utf8: "b", f64: 2, } | ||
]; | ||
const name = "struct"; | ||
const s = pl.Series(name, data); | ||
expect(s.name).toEqual(name); | ||
expect(s.toArray()).toEqual(data); | ||
test("series <--> array round trip", () => { | ||
const data = [ | ||
{utf8: "a", f64: 1, }, | ||
{utf8: "b", f64: 2, } | ||
]; | ||
const name = "struct"; | ||
const s = pl.Series(name, data); | ||
expect(s.name).toEqual(name); | ||
expect(s.toArray()).toEqual(data); | ||
}); | ||
test("pli.struct", () => { | ||
const expected = pl.DataFrame({ | ||
foo: [1], | ||
bar: [2] | ||
}).toStruct("foo"); | ||
const foo = pl.Series("foo", [1]); | ||
const bar = pl.Series("bar", [2]); | ||
const actual = pl.struct([foo, bar]).rename("foo"); | ||
expect(actual).toSeriesEqual(expected); | ||
}); | ||
test("pli.struct dataframe", () => { | ||
const df = pl.DataFrame({ | ||
foo: [1], | ||
bar: [2] | ||
}); | ||
const actual = df | ||
.select(pl.struct(pl.cols("foo", "bar")).alias("s")) | ||
.toSeries(); | ||
expect(actual).toSeriesEqual(df.toStruct("s")); | ||
}); | ||
test("struct toArray", () => { | ||
const actual = pl.DataFrame({ | ||
foo: [1, 10, 100], | ||
bar: [2, null, 200] | ||
}) | ||
.toStruct("foobar") | ||
.toArray(); | ||
|
||
const expected = [ | ||
{foo: 1, bar: 2}, | ||
{foo: 10, bar: null}, | ||
{foo: 100, bar: 200} | ||
]; | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.