Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generic DataFrame #293

Merged
merged 11 commits into from
Nov 21, 2024
182 changes: 95 additions & 87 deletions __tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ describe("dataframe", () => {
expect(actual.columns).toEqual(["foo_new", "bar_new", "ham_new"]);
});
test("replaceAtIdx", () => {
const actual = pl.DataFrame({
const actual: pl.DataFrame = pl.DataFrame({
foo: [1, 2, 3],
bar: [6, 7, 8],
ham: ["a", "b", "c"],
Expand Down Expand Up @@ -1315,101 +1315,109 @@ describe("dataframe", () => {
expect(actual).toFrameEqual(expected);
});
test("pivot", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for removing this? Thx

Copy link
Contributor Author

@scarf005 scarf005 Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gah, accidently removed when adding scopes, will amend and push again
EDIT: force-pushed on a675894 (#293)

let df = pl.DataFrame({
a: pl.Series([1, 2, 3]).cast(pl.Int32),
b: pl
.Series([
[1, 1],
[2, 2],
[3, 3],
])
.cast(pl.List(pl.Int32)),
});

let expected = pl
.DataFrame({
{
const df = pl.DataFrame({
a: pl.Series([1, 2, 3]).cast(pl.Int32),
"1": pl.Series([[1, 1], null, null]).cast(pl.List(pl.Int32)),
"2": pl.Series([null, [2, 2], null]).cast(pl.List(pl.Int32)),
"3": pl.Series([null, null, [3, 3]]).cast(pl.List(pl.Int32)),
})
.select("a", "1", "2", "3");
b: pl
.Series([
[1, 1],
[2, 2],
[3, 3],
])
.cast(pl.List(pl.Int32)),
});

let actual = df.pivot("b", {
index: "a",
on: "a",
aggregateFunc: "first",
sortColumns: true,
});
const expected = pl
.DataFrame({
a: pl.Series([1, 2, 3]).cast(pl.Int32),
"1": pl.Series([[1, 1], null, null]).cast(pl.List(pl.Int32)),
"2": pl.Series([null, [2, 2], null]).cast(pl.List(pl.Int32)),
"3": pl.Series([null, null, [3, 3]]).cast(pl.List(pl.Int32)),
})
.select("a", "1", "2", "3");

expect(actual).toFrameEqual(expected, true);
const actual = df.pivot("b", {
index: "a",
on: "a",
aggregateFunc: "first",
sortColumns: true,
});

df = pl.DataFrame({
a: ["beep", "bop"],
b: ["a", "b"],
c: ["s", "f"],
d: [7, 8],
e: ["x", "y"],
});
actual = df.pivot(["a", "e"], {
index: "b",
on: ["b"],
aggregateFunc: "first",
separator: "|",
maintainOrder: true,
});
expect(actual).toFrameEqual(expected, true);
}

expected = pl.DataFrame({
b: ["a", "b"],
"a|a": ["beep", null],
"a|b": [null, "bop"],
"e|a": ["x", null],
"e|b": [null, "y"],
});
expect(actual).toFrameEqual(expected, true);
{
const df = pl.DataFrame({
a: ["beep", "bop"],
b: ["a", "b"],
c: ["s", "f"],
d: [7, 8],
e: ["x", "y"],
});
const actual = df.pivot(["a", "e"], {
index: "b",
on: ["b"],
aggregateFunc: "first",
separator: "|",
maintainOrder: true,
});

df = pl.DataFrame({
foo: ["A", "A", "B", "B", "C"],
N: [1, 2, 2, 4, 2],
bar: ["k", "l", "m", "n", "o"],
});
actual = df.pivot(["N"], {
index: "foo",
on: "bar",
aggregateFunc: "first",
});
expected = pl.DataFrame({
foo: ["A", "B", "C"],
k: [1, null, null],
l: [2, null, null],
m: [null, 2, null],
n: [null, 4, null],
o: [null, null, 2],
});
expect(actual).toFrameEqual(expected, true);
const expected = pl.DataFrame({
b: ["a", "b"],
"a|a": ["beep", null],
"a|b": [null, "bop"],
"e|a": ["x", null],
"e|b": [null, "y"],
});
expect(actual).toFrameEqual(expected, true);
}
{
const df = pl.DataFrame({
foo: ["A", "A", "B", "B", "C"],
N: [1, 2, 2, 4, 2],
bar: ["k", "l", "m", "n", "o"],
});
const actual = df.pivot(["N"], {
index: "foo",
on: "bar",
aggregateFunc: "first",
});

df = pl.DataFrame({
ix: [1, 1, 2, 2, 1, 2],
col: ["a", "a", "a", "a", "b", "b"],
foo: [0, 1, 2, 2, 7, 1],
bar: [0, 2, 0, 0, 9, 4],
});
const expected = pl.DataFrame({
foo: ["A", "B", "C"],
k: [1, null, null],
l: [2, null, null],
m: [null, 2, null],
n: [null, 4, null],
o: [null, null, 2],
});

actual = df.pivot(["foo", "bar"], {
index: "ix",
on: "col",
aggregateFunc: "sum",
separator: "/",
});
expect(actual).toFrameEqual(expected, true);
}
{
const df = pl.DataFrame({
ix: [1, 1, 2, 2, 1, 2],
col: ["a", "a", "a", "a", "b", "b"],
foo: [0, 1, 2, 2, 7, 1],
bar: [0, 2, 0, 0, 9, 4],
});

expected = pl.DataFrame({
ix: [1, 2],
"foo/a": [1, 4],
"foo/b": [7, 1],
"bar/a": [2, 0],
"bar/b": [9, 4],
});
expect(actual).toFrameEqual(expected, true);
const actual = df.pivot(["foo", "bar"], {
index: "ix",
on: "col",
aggregateFunc: "sum",
separator: "/",
});

const expected = pl.DataFrame({
ix: [1, 2],
"foo/a": [1, 4],
"foo/b": [7, 1],
"bar/a": [2, 0],
"bar/b": [9, 4],
});
expect(actual).toFrameEqual(expected, true);
}
});
});
describe("join", () => {
Expand Down
Loading