Skip to content

Commit 7ea98a8

Browse files
authored
feat: switch DataFrame generic to its schema (#307)
This is a breaking change. It feels more natural to express the genericness of a DataFrame through its schema rather than its similarity to a Record of Series. For example, joins then can be computed more easily, as we don't need to unpack each Series to rename them.
1 parent d1686a6 commit 7ea98a8

File tree

11 files changed

+712
-268
lines changed

11 files changed

+712
-268
lines changed

__tests__/type.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { expectType } from "ts-expect";
2+
3+
import pl from "@polars/index";
4+
5+
describe("type tests", () => {
6+
it("types the DataFrame from the input", () => {
7+
const df = pl.DataFrame({
8+
bools: [false, true, false],
9+
bools_nulls: [null, true, false],
10+
int: [1, 2, 3],
11+
int_nulls: [1, null, 3],
12+
bigint: [1n, 2n, 3n],
13+
bigint_nulls: [1n, null, 3n],
14+
floats: [1.0, 2.0, 3.0],
15+
floats_nulls: [1.0, null, 3.0],
16+
strings: ["foo", "bar", "ham"],
17+
strings_nulls: ["foo", null, "ham"],
18+
date: [new Date(), new Date(), new Date()],
19+
datetime: [13241324, 12341256, 12341234],
20+
});
21+
22+
expectType<
23+
pl.DataFrame<{
24+
bools: pl.Bool;
25+
bools_nulls: pl.Bool;
26+
int: pl.Float64;
27+
int_nulls: pl.Float64;
28+
bigint: pl.UInt64;
29+
bigint_nulls: pl.UInt64;
30+
floats: pl.Float64;
31+
floats_nulls: pl.Float64;
32+
strings: pl.String;
33+
strings_nulls: pl.String;
34+
date: pl.Datetime;
35+
datetime: pl.Float64;
36+
}>
37+
>(df);
38+
});
39+
40+
it("produces the expected types", () => {
41+
const a = pl.DataFrame({
42+
id: [1n, 2n],
43+
age: [18n, 19n],
44+
name: ["A", "B"],
45+
});
46+
const b = pl.DataFrame({
47+
id: [1n, 2n],
48+
age: [18n, 19n],
49+
fl: [1.3, 3.4],
50+
});
51+
expectType<pl.Series<pl.UInt64, "age">>(a.getColumn("age"));
52+
expectType<
53+
(
54+
| pl.Series<pl.UInt64, "id">
55+
| pl.Series<pl.UInt64, "age">
56+
| pl.Series<pl.String, "name">
57+
)[]
58+
>(a.getColumns());
59+
expectType<pl.DataFrame<{ id: pl.UInt64; age: pl.UInt64 }>>(a.drop("name"));
60+
expectType<pl.DataFrame<{ id: pl.UInt64 }>>(a.drop(["name", "age"]));
61+
expectType<pl.DataFrame<{ id: pl.UInt64 }>>(a.drop("name", "age"));
62+
expectType<
63+
pl.DataFrame<{
64+
id: pl.UInt64;
65+
age: pl.UInt64;
66+
age_right: pl.UInt64;
67+
name: pl.String;
68+
fl: pl.Float64;
69+
}>
70+
>(a.join(b, { on: ["id"] }));
71+
expectType<
72+
pl.DataFrame<{
73+
id: pl.UInt64;
74+
age: pl.UInt64;
75+
ageRight: pl.UInt64;
76+
name: pl.String;
77+
fl: pl.Float64;
78+
}>
79+
>(a.join(b, { on: ["id"], suffix: "Right" }));
80+
expectType<
81+
pl.DataFrame<{
82+
id: pl.UInt64;
83+
id_right: pl.UInt64;
84+
age: pl.UInt64;
85+
name: pl.String;
86+
fl: pl.Float64;
87+
}>
88+
>(a.join(b, { leftOn: "id", rightOn: ["age"] }));
89+
expectType<
90+
pl.DataFrame<{
91+
id: pl.UInt64;
92+
id_right: pl.UInt64;
93+
age: pl.UInt64;
94+
age_right: pl.UInt64;
95+
name: pl.String;
96+
fl: pl.Float64;
97+
}>
98+
>(a.join(b, { how: "cross" }));
99+
});
100+
101+
it("folds", () => {
102+
const df = pl.DataFrame({
103+
a: [2, 1, 3],
104+
b: [1, 2, 3],
105+
c: [1.0, 2.0, 3.0],
106+
});
107+
expectType<pl.Series<pl.Float64, string>>(df.fold((s1, s2) => s1.plus(s2)));
108+
});
109+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"chance": "^1.1.13",
6363
"jest": "^29.7.0",
6464
"source-map-support": "^0.5.21",
65+
"ts-expect": "^1.3.0",
6566
"ts-jest": "^29.3.4",
6667
"ts-node": "^10.9.2",
6768
"typedoc": "^0.28.4",

0 commit comments

Comments
 (0)